Using php2Bluesky without Composer

I have been asked how to use my php2Bluesky library without having to install it using Composer. Given that this is a common request I thought that I would document it here.

Pros and Cons of Composer

Before we get into the specifics of using php2Bluesky without Composer let’s take a quick look at the pros and cons of using Composer. Feel free to skip this bit and go straight to how to use php2Bluesky without Composer!

Using Composer to install a package has several advantages over manually downloading the code and requiring it. Composer automates dependency management, ensuring that all required libraries are installed with the correct versions and avoiding conflicts. It also makes updates simple, as you only need to run composer update. Additionally, it maintains a consistent project structure and provides a composer.lock file for reproducibility across environments.

However, Composer does introduce some overhead, such as requiring internet access and Composer itself to be set up. Conversely, downloading and manually requiring code offers more control and avoids reliance on Composer, but it is prone to versioning issues, missing dependencies, and increased maintenance effort as updates and compatibility checks must be handled manually.

Pros of Using Composer

  • Dependency Management: Automatically resolves and installs dependencies, including sub-dependencies.
  • Version Control: Ensures the correct versions of packages are installed, avoiding compatibility issues.
  • Ease of Updates: Simplifies package updates with a single command (composer update).
  • Reproducibility: Provides a composer.lock file to ensure consistent dependencies across environments.
  • Community Standards: Encourages adherence to best practices and standardised project structures.
  • Automation: Reduces manual effort for downloading, installing, and maintaining packages.

Cons of Using Composer

  • Setup Requirements: Requires Composer to be installed and configured on the system.
  • Internet Dependency: Needs internet access to fetch packages and updates.
  • Overhead: Adds an extra layer of tooling that may be unnecessary for very simple projects.
  • Learning Curve: New users might need time to understand Composer’s workflow and commands.

Pros of Downloading and Requiring Code Manually

  • Control: Provides complete control over the installation and management of the code.
  • Independence: Does not require Composer or an internet connection after downloading.
  • Simplicity: May be sufficient for small, one-off projects with minimal dependencies.

Cons of Downloading and Requiring Code Manually

  • Manual Maintenance: Requires manual tracking of updates and compatibility issues.
  • Risk of Conflicts: Increases the likelihood of version conflicts when managing multiple dependencies.
  • Time-Consuming: More effort is needed to download, configure, and update dependencies.
  • No Dependency Resolution: Dependencies of the package are not automatically handled, leading to potential errors.

Using php2Bluesky without Composer

That comparison out of the way, let’s take a look at how we can set up php2Bluesky without Composer. As php2Bluesky relies on the BlueskyApi code we also need to get that in order for it all to operate.

Grab the Library Code

The first step is to download the code and store it with your project. Go to the BlueskyApi page and download both BlueskyApi.php and BlueskyApiSessionHelper.php as shown below:

Now go to the php2Bluesky page and download functions.php in the same way. Put all the files in the same folder as your own code.

NOTE: very shortly functions.php will be renamed php2Bluesky.php so if you are coming to this late and can’t find functions.php that’ll be why.

Link the Libraries to your Code

Finally, add the libraries to the header of your code as follows:

    include __DIR__ . '/BlueskyApi.php';
    include __DIR__ . '/php2Bluesky.php';

    use williamsdb\php2bluesky\php2Bluesky;

    $php2Bluesky = new php2Bluesky();

Remember to adjust the locations if you put your code in a subfolder. And that should be it. Here’s a full example:

    include __DIR__ . '/BlueskyApi.php';
    include __DIR__ . '/php2Bluesky.php';

    use williamsdb\php2bluesky\php2Bluesky;

    $php2Bluesky = new php2Bluesky();

    $handle = '<your Bluesky handle>';
    $password = '<your Bluesky app password>';

    // connect to Bluesky API
    $connection = $php2Bluesky->bluesky_connect($handle, $password);

    // send some text and a link to Bluesky
    $response=$php2Bluesky->post_to_bluesky($connection, $text, "", "https://www.spokenlikeageek.com/tag/php2bluesky/");
    print_r($response);

    // get the permament link for the post
    if (!isset($response->error)){
        $url = $php2Bluesky->permalink_from_response($response, $handle);
        echo $url.PHP_EOL;            
    }    
    
    // print out the current rate limits
    $rates = $php2Bluesky->get_rate_limits($connection);
    print_r($rates);

Leave a Reply

Your email address will not be published. Required fields are marked *