Skip to content

Examples

Eldar Shahmaliyev edited this page Jan 28, 2025 · 2 revisions

Create a Simple Post

Easily create and send a text-based post to BlueSky.

// Forge a new post with simple text
$post = bskyFacade($client)->post()->text("Hello, BlueSky!");

// Send the post to the server
$createdRecord = bskyFacade()->createRecord()
    ->record($post) // Include the forged post
    ->repo($client->authenticated()->did()) // Specify the authenticated user's DID
    ->collection($post->nsid()) // Use the appropriate collection namespace
    ->send();

// Output the URI of the created post
echo $createdRecord->uri();

Create a Post with an Image

Embed images into your posts for enhanced visual appeal.

use \Atproto\Lexicons\App\Bsky\Embed\Collections\ImageCollection;
use \Atproto\Lexicons\App\Bsky\Embed\Image;

// Upload an image to the server
$uploadedBlob = bskyFacade($client)->uploadBlob()
    ->blob('/path/to/image.jpg') // Specify the image path
    ->send()
    ->blob(); // Retrieve the uploaded blob metadata

// Forge a post embedding the uploaded image
$post = bskyFacade()->post()
    ->text("Hello, BlueSky with an image!")
    ->embed(bskyFacade()->imagesEmbed(
       bskyFacade()->image($uploadedBlob, 'Image description')
    ));

// Send the post and log the URI
$createdRecord = bskyFacade()->createRecord()
    ->record($post)
    ->repo($client->authenticated()->did())
    ->collection($post->nsid())
    ->send();

echo $createdRecord->uri();

Create a Post with External Links

Add external links with thumbnails for informative posts.

// Upload an image for the link preview
$uploadedBlob = bskyFacade($client)->uploadBlob()
    ->blob('/path/to/image.jpg')
    ->send()
    ->blob();

// Forge external link details
$external = bskyFacade()->externalEmbed(
    'https://example.com', // Link URL
    'Example Website',    // Link title
    'A description of the website.' // Link description
)->thumb($uploadedBlob); // Add the uploaded image as a thumbnail

// Forge a post including the external link
$post = bskyFacade()->post()
    ->text("Check out this website!")
    ->embed($external);

// Send the post and retrieve the URI
$createdRecord = bskyFacade()->createRecord()
    ->record($post)
    ->repo($client->authenticated()->did())
    ->collection($post->nsid())
    ->send();

echo $createdRecord->uri();

Search Posts

Search for posts on BlueSky using keywords or filters.

// Perform a keyword search on posts
$response = bskyFacade()->searchPosts('keyword')
    ->send();

// Loop through and display the search results
foreach ($response->posts() as $post) {
    echo $post->record()->text() . "\n";
}

GetTimeline

Get a view of the requesting account's home timeline.

$feed = bskyFacade($client)->getTimeline()
    ->limit(10)
    ->send()
    ->feed();

foreach($feed as $entry) {
    echo sprintf("Created by %s at %s" . PHP_EOL,
        $entry->post()->author()->handle(),
        $entry->post()->indexedAt()->format('d/m/Y H:i:s')
    );
}

Video Upload

require_once("vendor/autoload.php");

use Atproto\Client;
use Atproto\DataModel\Blob\Blob;
use Atproto\Lexicons\App\Bsky\Embed\Video;
use Atproto\Responses\App\Bsky\Video\UploadVideoResponse;
use Atproto\Support\FileSupport;

$pdsEndpoint = $client->authenticated()->didDoc()->service()
    ->firstWhere('id', '#atproto_pds')['serviceEndpoint'];

$pdsHost = str_replace(
    ["https://", "http://"],
    "did:web:",
    $pdsEndpoint
);

// Generate a temporary token for uploading the video
$token = bskyFacade($client)->getServiceAuth()
    ->aud($pdsHost)
    ->lxm(bskyFacade()->uploadBlob()->nsid())
    ->exp(\Carbon\Carbon::now()->addMinutes(15)->timestamp)
    ->send()
    ->token();

$filePath = '/home/shahmal1yev/Videos/Screencasts/shortvideo.mp4';
$file = new FileSupport($filePath);

// Upload video
$uploadedVideo = bskyFacade()->uploadVideo(basename($filePath), $file, $token)->send();

// Check if the video upload returned a blob
if ($videoBlob = $uploadedVideo->has('blob')) {
    $videoBlob = Blob::viaArray($uploadedVideo->blob());
}

// Wait for the job to complete if the video blob is not immediately available
while (! $videoBlob) {
    $jobStatusResponse = bskyFacade()->getJobStatus($uploadedVideo->jobId()) // Use the job ID from the upload response
        ->send();

    // Check if the job has completed successfully
    if ($jobStatusResponse->jobStatus()->state() === 'JOB_STATE_COMPLETED') {
        // Convert the video blob to an object
        $videoBlob = Blob::viaArray($jobStatusResponse->jobStatus()->blob());
    }

    // Wait for 0.5 seconds before checking again
    sleep(0.5);
}

// Create a post with the uploaded video embedded
$post = bskyFacade()->post()
    ->text("Hello, BlueSky! This is a video.")
    ->embed(new Video($videoBlob));

// Send the post to the server and log the created record's URI
$createdRecordRes = bskyFacade()->createRecord()
    ->repo($client->authenticated()->handle())
    ->collection($post->nsid())
    ->record($post)
    ->send();

// Output the URI of the created post
echo $createdRecordRes->uri();
  • 🏠 Home
    Introduction to the SDK and its features

  • 🚀 Quick Start
    Get started quickly with the basics of using the Bluesky SDK

  • ⚙️ Installation
    Step-by-step guide to installing the SDK

  • 🛠️ Architecture Overview
    Learn about the SDK's structure and design principles

  • 📖 BskyFacade
    Simplify API interactions with the facade

  • 💾 Serialization
    Serialize and deserialize data effectively

  • 🔄 Session Management
    Manage authentication and session reuse

  • 🧹 Casting
    Explore type-safe response casting in the SDK

  • 💡 Examples
    Practical usage examples for various features

Clone this wiki locally