PHP code example of hojabbr / laravel-social

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

    

hojabbr / laravel-social example snippets


$driver = Social::driver('instagram');

$result = $driver->publish(new PublishRequest(
    destination: $driver->destination('fa', Placement::Reel),
    body: 'A short caption.',
    tags: ['bourse', 'tehran'],
    media: [Media::video(url: 'https://example.com/reel.mp4')],
));

$result->isSent();    // true
$result->externalId;  // '17901234567890123'
$result->url;         // the post's permalink

'networks' => [
    'instagram' => ['driver' => 'instagram', 'enabled' => true, 'app_id' => '…', 'app_secret' => '…', 'queue' => 'shares'],
],

'accounts' => [
    'instagram' => [
        'fa' => ['id' => '17841400000000000', 'handle' => 'myaccount', 'token' => 'IGAA…'],
        'en' => ['id' => '17841400000000001', 'handle' => 'myaccount_en', 'token' => 'IGAA…'],
    ],
    'youtube' => [
        'fa' => ['id' => 'UC…', 'handle' => 'mychannel', 'refresh_token' => '1//…'],
    ],
],

$driver = Social::driver('youtube');

$result = $driver->publish(new PublishRequest(
    destination: $driver->destination('fa', Placement::Reel),
    title: 'How a bond auction works',
    body: "The long description.",
    tags: ['finance', 'education'],
    media: [Media::video('/path/to/short.mp4', thumbnailPath: '/path/to/cover.jpg')],
));

$caps = Social::driver('instagram')->capabilities();

$caps->bodyLimit;             // text without media
$caps->captionLimit;          // text with media
$caps->tagLimit;              // 5
$caps->maxItemsPerMessage;    // 10
$caps->maxVideoBytes;
$caps->maxImageBytes;
$caps->accepts('image/png');  // false: Instagram takes JPEG
$caps->supports(Placement::Story);
$caps->pullsMedia;            // true
$caps->thumbnailAspect;       // 16 / 9 on YouTube, null elsewhere

$driver->mediaMetrics($account, $mediaId)->get('reach');
$driver->accountMetrics($account)->values;
$driver->publishingLimit($account)->get('used');

$credentials = $driver->refresh($account);
$driver->credentials($account)?->daysRemaining();

Social::forget('instagram');

Sleep::for($driver->rateProfile()->pauseMsFor($messages))->milliseconds();

$health = $driver->health();   // never throws
$health->isUsable();
$health->details;
$health->error;

use Hojabbr\Social\Testing\FakeDriver;

$fake = FakeDriver::fake('instagram');
$fake->willReturn(PublishResult::rejected('Caption too long.'));

// … run the code under test …

expect($fake->requests)->toHaveCount(1)
    ->and($fake->lastRequest()?->body)->toContain('#bourse');

Social::extend('mastodon', fn (array $network, array $accounts, string $name) =>
    new MastodonDriver($network, $accounts, $name));