PHP code example of siberfx / x-autopost

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

    

siberfx / x-autopost example snippets


// app/Providers/AppServiceProvider.php → boot()
Gate::define('manage-x-autopost', fn (User $user) => $user->is_admin);

// app/Models/Article.php
class Article extends Model implements ShareableOnX
{
    use PostsToX;

    public function isLiveForX(): bool
    {
        return $this->status === 'published';
    }

    public function toXPost(): XPost
    {
        return XPost::make(route('blog.show', $this), $this->title)
            ->description(Str::limit(strip_tags($this->body), 200));
    }
}

Gate::define('manage-x-autopost', fn (User $user) => $user->hasRole('marketing'));

'routes' => [
    'enabled' => true,
    'prefix' => 'admin/x',               // → /admin/x/redirect, /admin/x/callback …
    'name' => 'x-autopost.',
    'middleware' => ['web', 'auth:admin', 'can:manage-x-autopost'],
    'after_connect' => '/admin/settings',
],

use Siberfx\XAutopost\Facades\X;

if (X::isConnected()) {
    $connection = X::connection();

    $connection->name;        // "Ada Lovelace"
    $connection->username;    // "ada"
    $connection->status;      // active | expired | revoked | unknown
    $connection->toArray();   // everything above as an array, never a token
}

use Illuminate\Support\Str;
use Siberfx\XAutopost\Concerns\PostsToX;
use Siberfx\XAutopost\Contracts\ShareableOnX;
use Siberfx\XAutopost\Posts\XPost;

class Article extends Model implements ShareableOnX
{
    use PostsToX;

    public function isLiveForX(): bool
    {
        return $this->status === 'published';
    }

    public function toXPost(): XPost
    {
        return XPost::make(url: route('blog.show', $this), title: $this->title)
            ->description(Str::limit(strip_tags($this->body), 200))
            ->commentary("{title}\n\n{description}\n\n{url}");
    }
}

Article::create(['title' => 'Hello', 'status' => 'published']);  // queued
$draft->update(['status' => 'published']);                        // queued (draft → published)
$published->update(['title' => 'Typo fixed']);                    // not queued: it was already live
Article::where('id', 5)->update(['status' => 'published']);       // not queued: no model events

'autopost' => [
    'enabled' => (bool) env('X_AUTOPOST', false),
    'in_console' => false,
    'queue_connection' => 'redis',   // null = your default connection
    'queue' => 'x',                  // null = the default queue
],

use Siberfx\XAutopost\Facades\X;

X::share($article);           // now; returns the XSharedPost record (re-sharing is allowed)
X::queue($article);           // in the background
X::wasPosted($article);       // bool, same as $article->wasPostedToX()
$article->wasPostedToX();     // bool
$article->xPosts;             // history: post_id, status, trigger, error, posted_at

X::share($article);
X::connection()->toArray();

use Illuminate\Support\Facades\Http;
use Siberfx\XAutopost\Facades\X;

Http::fake(['https://api.x.com/2/tweets' => Http::response(['data' => ['id' => '1']], 201)]);
bash
php artisan vendor:publish --tag=x-autopost-config
php artisan vendor:publish --tag=x-autopost-migrations
php artisan vendor:publish --tag=x-autopost-views     # only when the UI is enabled
php artisan vendor:publish --tag=x-autopost-stubs
bash
php artisan queue:work redis --queue=x,default