| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Small and easy-to-use library for the X (formerly Twitter) API v2.
Install via Composer:
composer require dg/twitter-phpRequirements: PHP 8.2 or higher.
Go to X Developer Portal
Sign in and create a Developer account (Free tier is sufficient)
Create a new Project and App
Set up permissions (important!):
Generate keys:
⚠️ Note: After changing permissions, you must regenerate the Access Token!
Use case description (required by X, min. 250 characters):
I am building a personal tool for publishing my own content to X. The application will only be used to post text updates and images from my own account. It will not read, collect, or analyze any data from other users. There is no automation of likes, retweets, follows, or any other interactions. This is strictly a single-user tool for my personal use to streamline my social media posting workflow.
Create the client using your API credentials:
use DG\X\Client;
$x = new Client($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);Note: The Free tier of the X API only allows sending and deleting tweets and reading your own profile (authenticate()). Reading timelines, searching, user info, followers and other read endpoints require the Basic tier or higher.
$x->sendTweet('I am fine today.');With an image:
$x->sendTweet('Check this out!', '/path/to/image.jpg');With multiple images:
$x->sendTweet('Photos!', ['/path/to/img1.jpg', '/path/to/img2.jpg']);Get your own tweets:
$tweets = $x->getMyTweets();Get your home timeline (you and people you follow):
$tweets = $x->getTimeline();Get your mentions:
$tweets = $x->getMentions();The static method Client::clickable() makes links, mentions and hashtags in tweets clickable:
foreach ($tweets as $tweet) {
echo Client::clickable($tweet);
}$results = $x->search('#php');$user = $x->getUser('elonmusk');
$user = $x->getUserById('44196397');
$followers = $x->getFollowers('44196397');$x->follow('44196397');
$x->sendDirectMessage('44196397', 'Hello!');Test if user credentials are valid:
if (!$x->authenticate()) {
die('Invalid credentials');
}You can call any X API v2 endpoint directly:
$result = $x->request('tweets', 'POST', ['text' => 'Hello from raw API!']);
$result = $x->request('users/me', 'GET', ['user.fields' => 'description,profile_image_url']);All methods throw DG\X\Exception on error:
try {
$x->sendTweet('Hello!');
} catch (DG\X\Exception $e) {
echo 'Error: ', $e->getMessage();
}The old class names DG\Twitter\Twitter, DG\Twitter\Exception, Twitter and TwitterException are available as aliases for DG\X\Client and DG\X\Exception.
Thank you!
v5.0 (2/2026)
v4.1 (11/2019)
v4.0 (2/2019)
v3.8 (2/2019)
v3.7 (3/2018)
v3.6 (8/2016)
v3.5 (12/2014)
v3.4 (11/2014)
v3.3 (3/2014)
v3.2 (1/2014)
v3.1 (3/2013)
v3.0 (12/2012)
v2.0 (8/2012)
v1.0 (7/2008)
(c) David Grudl, 2008, 2026 (https://davidgrudl.com)
| Back | FazBrowse Home | New Git URL |