Skip to content

Instantly share code, notes, and snippets.

@adactio
Last active November 27, 2024 11:42
Show Gist options
  • Save adactio/1a850920a0554a49f36daf79d776a440 to your computer and use it in GitHub Desktop.
Save adactio/1a850920a0554a49f36daf79d776a440 to your computer and use it in GitHub Desktop.
A PHP function that returns the token you need to post something to Bluesky
<?php
/*
Use this function whenever you need a token from Bluesky e.g.
$my_token = getBlueskyToken();
*/
function getBlueskyToken() {
$url = "https://bsky.social/xrpc/com.atproto.server.createSession";
$payload = array(
"identifier" => 'YOUR-BLUESKY-HANDLE',
"password" => 'YOUR-APP-PASSWORD',
);
// Generate an app password at https://bsky.app/settings/app-passwords
$headers = array(
"Content-Type: application/json"
);
$options = array(
CURLOPT_URL => $url,
CURLOPT_USERAGENT => "adactio.com",
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POST => TRUE,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_TIMEOUT => 5
);
$curl = curl_init();
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
curl_close($curl);
$data = json_decode($response, true);
return $data['accessJwt'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment