In this series of posts we have looked at:
In this final post we are looking at putting it all together. This includes a worked example plus a complete library of functions that will allow you to easily interact with the Bluesky API.
The Prerequisites
What this project has predominately been about is the correct formatting of the parameters that need to be passed to the Bluesky API. This code does not directly call the API but uses BlueskyApi by Clark Rasmussen to do that.
Here’s an overview of everything you will need:
- PHP (I used 8.1 so cannot guarantee this will work with earlier versions)
- Composer
- Git
- BlueskyApi by Clark Rasmussen
- My function library and examples.
I am going to assume that you have a working knowledge of composer and git but the basic installation is as follows:
git clone https://github.com/williamsdb/php2Bluesky.git
Now install composer and require the necessary libraries:
bin/composer.phar require cjrasmussen/bluesky-api
At this point you should have everything you need.
A Worked Example
Provided as part of the files in my repository there is one called index.php which gives an example of how you can send different types of post to Bluesky. Open this up and add in your Bluesky handle, API password that you created (see here for how to do that) and change the image, link and text variables as you see fit.
<?php
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/functions.php';
$handle = '<your Bluesky handle>';
$password = '<your API password>';
$filename = '<local or remote path to image>';
$link = '<url of page for the link card>';
$text = 'Hello World! https://spokenlikeageek.com';
// connect to Bluesky API
$connection = bluesky_connect($handle, $password);
// send a text only post with link
$response = post_to_bluesky($connection, $text);
print_r($response);
// send an image with text and a link
$image = upload_media_to_bluesky($connection, $filename);
$response = post_to_bluesky($connection, $text, $image);
print_r($response);
// send text with link for a link card
// if you specifiy both media and link the latter takes precedence
$response = post_to_bluesky($connection, $text, '', $link);
print_r($response);
?>
Running this will create three different posts on Bluesky:
- text (with highlighted link)
- text and an image
- text and a link card
All the heavy lifting is done by the post_to_bluesky
function which takes four parameters as follows:
- $connection – the connection string returned from the function
bluesky_connect
- $text – the body of the post that you want to send to Bluesky. If you include link(s) these will be parsed
- $media – location of an image to post. This can be either a local file or remote url
- $link – the url of the link card that you want attached to the post.
One point to note is that a link card takes precedence over an image so if you supply both parameters only the link card will be posted.
And that’s it! There isn’t any error handling in these example so you will want to expand your code to include that checking. The response from post_to_bluesky
will give you what you need. If you have any comments or questions put them in the comments.
Thx for your guidance, While the options
– text (with highlighted link)
– text and an image
work like a charm, I cannot get
– text and a link card
to work. When running the script (for “text (with highlighted link)” or “text and an image”) I get a response in the browser like
stdClass Object ( [uri] => at://did:plc:sbly25uodq4c6uuctdwime6l/app.bsky.feed.post/3l7bzshmsb42j [cid] => bafyreigegmlllvk77vygacnkbnekvllnklnwuyfh5jd7x5d7pao44pvl7u [commit] => stdClass Object ( [cid] => bafyreib5x7hfmgqhk2mqst5s2idanpxofwwiku2xbzcrw3sa3rt7giz2li [rev] => 3l7bzshmz3u2j ) [validationStatus] => valid )
Also the post shows on my Bluesky.
When running the script for “text and a link card” there is no response in the browser and no post on Bluesky. I set $link = ‘https://example.com/’;
For responses to this issue and updates to the software please see here: https://github.com/williamsdb/php2Bluesky/issues/8