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/ */
// 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));
}
}
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
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