Last active
November 26, 2024 09:24
-
-
Save cchana/0317b97aba7e757a7e3ea1598a38cb2b to your computer and use it in GitHub Desktop.
A brief script that will log you into Bluesky and post 'Hello, world!'. It saves the session info to bsky.tokens so you can continue to post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# Login info, set an app password at https://bsky.app/settings/app-passwords | |
$password = 'p4ssw0rd'; | |
$handle = 'your.handle'; | |
# URL and data for creating a session | |
$apiKeyUrl ='https://bsky.social/xrpc/com.atproto.server.createSession'; | |
$apiKeyPostData='{"identifier": "'.$handle.'", "password": "'.$password.'" }'; | |
# If the details have been saved, used them | |
if (file_exists('bsky.tokens')) { | |
$apiData = json_decode(file_get_contents('bsky.tokens'), true); | |
# Else, fetch and set the data | |
} else { | |
# cURL to login | |
$ch = curl_init(); | |
$allHeaders = array(); | |
curl_setopt($ch, CURLOPT_HEADER, false); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $apiKeyPostData); | |
curl_setopt($ch, CURLOPT_URL, $apiKeyUrl); | |
$apiData = json_decode(curl_exec($ch), true); | |
curl_close($ch); | |
# Store the token content so we don't have to keep logging in | |
file_put_contents('bsky.tokens', $apiData); | |
} | |
# Store the DID and JWT | |
$did = $apiData['did']; | |
$key = $apiData['accessJwt']; | |
# Created at date | |
$date = date('Y-m-d\TH:i:s.Z\Z'); | |
# URL and data for creating a post | |
$feedPostUrl='https://bsky.social/xrpc/com.atproto.repo.createRecord'; | |
$feedPostData='{"collection":"app.bsky.feed.post", "repo":"'.$did.'", "record":{"text":"Hello, world!", "createdAt":"'.$date.'", "type":"app.bsky.feed.post"}}'; | |
# cURL to post | |
$ch = curl_init(); | |
$allHeaders = array(); | |
curl_setopt($ch, CURLOPT_HEADER, false); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Authorization: Bearer '.$key)); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $feedPostData); | |
curl_setopt($ch, CURLOPT_URL, $feedPostUrl); | |
$postData = json_decode(curl_exec($ch), true); | |
curl_close($ch); | |
# If it was posted OK | |
if ($postData['validationStatus'] == 'valid') { | |
$postIdParts = explode('/', $postData['uri']); | |
$postId = end($postIdParts); | |
# Display a link to the post | |
echo '<a href="https://bsky.app/profile/'.$handle.'/post/'.$postId.'">View post on Bluesky</a>'; | |
} |
Yes, you need to submit the image to https://bsky.social/xrpc/com.atproto.repo.uploadBlob first, so that it can be attached to your post as an embed. But the principle is the same. It's covered in the Bluesky API documentation here: https://docs.bsky.app/docs/advanced-guides/posts#images-embeds
Good luck!
Hello,
I'm sorry but my knowledge is limited and only with PHP.
I can't add libraries to my server either, so the only way I have is the one you explain.
Could you modify this PHP to be able to include images and also how to add line breaks.
Thanks and apologies.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
It's possible post an image and/or url with this simple system?
Best regards,