PHP code example of cjrasmussen / bluesky-api
1. Go to this page and download the library: Download cjrasmussen/bluesky-api library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cjrasmussen / bluesky-api example snippets
use cjrasmussen\BlueskyApi\BlueskyApi;
$bluesky = new BlueskyApi();
try {
$bluesky->auth($handle, $app_password);
} catch (Exception $e) {
// TODO: Handle the exception however you want
}
$refresh_token = $bluesky->getRefreshToken();
try {
$bluesky->auth($refresh_token);
} catch (Exception $e) {
// TODO: Handle the exception however you want
}
$args = [
'collection' => 'app.bsky.feed.post',
'repo' => $bluesky->getAccountDid(),
'record' => [
'text' => 'Testing #TestingInProduction',
'langs' => ['en'],
'createdAt' => date('c'),
'$type' => 'app.bsky.feed.post',
],
];
$data = $bluesky->request('POST', 'com.atproto.repo.createRecord', $args);
$args = [
'collection' => 'app.bsky.feed.post',
'repo' => $bluesky->getAccountDid(),
'record' => [
'text' => 'Testing #TestingInProduction',
'facets' => [
[
'index' => [
'byteStart' => 8,
'byteEnd' => 28,
],
'features' => [
[
'$type' => 'app.bsky.richtext.facet#tag',
'tag' => 'TestingInProduction',
],
],
],
],
'langs' => ['en'],
'createdAt' => date('c'),
'$type' => 'app.bsky.feed.post',
],
];
$data = $bluesky->request('POST', 'com.atproto.repo.createRecord', $args);
$args = [
'collection' => 'app.bsky.feed.post',
'repo' => $bluesky->getAccountDid(),
'record' => [
'text' => 'Testing https://cjr.dev',
'facets' => [
[
'index' => [
'byteStart' => 8,
'byteEnd' => 23,
],
'features' => [
[
'$type' => 'app.bsky.richtext.facet#link',
'uri' => 'https://cjr.dev',
],
],
],
],
'langs' => ['en'],
'createdAt' => date('c'),
'$type' => 'app.bsky.feed.post',
],
];
$data = $bluesky->request('POST', 'com.atproto.repo.createRecord', $args);
$body = file_get_contents($file);
$response = $bluesky->request('POST', 'com.atproto.repo.uploadBlob', [], $body, 'image/png');
$image = $response->blob;
$args = [
'collection' => 'app.bsky.feed.post',
'repo' => $bluesky->getAccountDid(),
'record' => [
'text' => 'Testing with an image #TestingInProduction',
'langs' => ['en'],
'createdAt' => date('c'),
'$type' => 'app.bsky.feed.post',
'embed' => [
'$type' => 'app.bsky.embed.images',
'images' => [
[
'alt' => 'A test image',
'image' => $image,
],
],
],
],
];
$response = $bluesky->request('POST', 'com.atproto.repo.createRecord', $args);
use cjrasmussen\BlueskyApi\BlueskyApi;
use cjrasmussen\BlueskyApi\BlueskyApiSessionHelper;
$blueskyApi = new BlueskyApi();
$blueskyApiSessionHelper = new BlueskyApiSessionHelper($blueskyApi);
try {
$blueskyApiSessionHelper->auth($refresh_token_path, $handle, $password);
} catch (Exception $e) {
// TODO: Handle the exception however you want
}
$blueskyApi->getLastResponseHeader();