Skip to content

Instantly share code, notes, and snippets.

@geertw
Created August 10, 2023 19:54
Show Gist options
  • Save geertw/b68f67dc97bcb1f6d47f9146a1459d61 to your computer and use it in GitHub Desktop.
Save geertw/b68f67dc97bcb1f6d47f9146a1459d61 to your computer and use it in GitHub Desktop.
Posting something to Bluesky in PHP

Posting to Bluesky in PHP

Small example how to post something to Bluesky using PHP.

This example is based on the library cjrasmussen/bluesky-api.

composer require cjrasmussen/bluesky-api

This is the result of the snippet below:

image

<?php
require 'vendor/autoload.php';
use cjrasmussen\BlueskyApi\BlueskyApi;
// full handle without the @-sign:
$handle = 'yourhandle.bsky.social';
// retrieve app password here: https://bsky.app/settings/app-passwords
$app_password = 'xxxx-xxxx-xxxx-xxxx';
$bluesky = new BlueskyApi($handle, $app_password);
// Post a message
$args = [
'collection' => 'app.bsky.feed.post',
'repo' => $bluesky->getAccountDid(),
'record' => [
// The actual text of the post
'text' => 'This is a test message! With a link!',
// The facets below are optional, but you need them to create a link in your message
'facets' => [
[
"index" => [
"byteStart" => 30, // where the link should start. Character 30 here is from 'a link'
"byteEnd" => 36, // and where the link should end - just before the exclamation mark.
],
"features" => [
[
'$type' => "app.bsky.richtext.facet#link",
'uri' => 'https://geert.ninja/', // URL to link to
],
],
],
],
'langs' => ['en'], // language (English here)
'createdAt' => date('c'), // just the current timestamp
'$type' => 'app.bsky.feed.post',
],
];
// Actually post it to Bluesky
$data = $bluesky->request('POST', 'com.atproto.repo.createRecord', $args);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment