PHP code example of jkudish / plume

1. Go to this page and download the library: Download jkudish/plume 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/ */

    

jkudish / plume example snippets


use Plume\Facades\X;

// Post a tweet
$post = X::createPost('Hello from Plume!');

// Search recent tweets
$results = X::searchRecent('laravel');
foreach ($results->data as $post) {
    echo "{$post->text}\n";
}

// Get your profile
$me = X::me();
echo $me->publicMetrics->followersCount;

$post = X::getPost('123', tweetFields: [TweetField::PublicMetrics]);
echo $post->publicMetrics->likeCount;

// DTOs carry action methods
$post->like('user-id');
$post->reply('Nice post!');
$post->bookmark('user-id');
$post->delete();

$page = X::userTimeline('user-id', maxResults: 100);
while ($page !== null) {
    foreach ($page->data as $post) {
        process($post);
    }
    $page = $page->nextPage();
}

// From credentials array or a model implementing HasXCredentials
$client = X::forUser($user);

// Inject user ID directly to skip the /me API call
$client = X::forUser($credentials)->withUser('12345');
// Or pass a User DTO
$client = X::forUser($credentials)->withUser($userDto);

// All calls auto-resolve the user ID
$client->like('tweet-id');
$client->bookmark('tweet-id');
$client->followers();
$client->userTimeline(maxResults: 20);

use Plume\Contracts\HasXCredentials;

class User extends Authenticatable implements HasXCredentials
{
    public function toXCredentials(): array
    {
        return [
            'access_token' => $this->x_access_token,
            'refresh_token' => $this->x_refresh_token,
            'expires_at' => $this->x_token_expires_at,
        ];
    }
}

// In AppServiceProvider::register()
$this->app->bind('x.token_refreshed', fn () => function (array $credentials) {
    auth()->user()->update([
        'x_access_token' => $credentials['access_token'],
        'x_refresh_token' => $credentials['refresh_token'],
        'x_token_expires_at' => $credentials['expires_at'],
    ]);
});

use Plume\Exceptions\RateLimitException;

try {
    $results = X::searchRecent('laravel');
} catch (RateLimitException $e) {
    $seconds = $e->retryAfterSeconds(); // seconds until rate limit resets
    $timestamp = $e->resetTimestamp;     // unix timestamp of reset

    sleep($seconds);
    // retry...
}

// Simple upload
$media = X::uploadMedia('/path/to/image.jpg', 'image/jpeg');
X::createPost('Check this out!', [
    'media' => ['media_ids' => [$media['media_id']]],
]);

// Chunked upload for large files
$init = X::initChunkedUpload($totalBytes, 'video/mp4', 'tweet_video');
X::appendChunk($init['media_id'], 0, $chunkData);
X::finalizeUpload($init['media_id']);

use Plume\Facades\X;

it('creates a post', function () {
    $fake = X::fake();

    X::createPost('Hello from tests!');

    $fake->assertPostCreated('Hello');
    $fake->assertCalledTimes('createPost', 1);
});

it('tracks interactions', function () {
    $fake = X::fake();

    X::like('user-1', 'tweet-1');
    X::follow('user-1', 'target-1');

    $fake->assertLiked('tweet-1');
    $fake->assertFollowed('target-1');
});

it('stubs return values', function () {
    $fake = X::fake();
    $fake->shouldReturn('searchRecent', new PaginatedResult(
        data: [new Post(id: '1', text: 'Stubbed')],
        resultCount: 1,
    ));

    $results = X::searchRecent('test');
    expect($results->data[0]->text)->toBe('Stubbed');
});
bash
# Your profile
php artisan plume:me

# Post a tweet
php artisan plume:post --text="Hello from the CLI!"

# Search
php artisan plume:search "laravel" --max-results=20

# Your home timeline
php artisan plume:home --max-results=10 --format=json