The most popular PHP library for use with the Twitter OAuth REST API.
The recommended and easy as pie method is Composer. Setup require in your projects composer.json file. Latest release:
composer require abraham/twitteroauth
Import the TwitterOAuth class.
require "vendor/autoload.php"; use Abraham\TwitterOAuth\TwitterOAuth;
Start making API requests.
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token, $access_token_secret); $content = $connection->get("account/verify_credentials");
Versions listed as "active support" or "security fixes" are supported.
This site is a working example. to see the flow.
Unlike many Twitter API libraries, TwitterOAuth doesn't provide a custom function for every API method. Instead there are a couple of generic functions so that as Twitter adds features to the API you don't need to update the library. Here is an example of GET statuses/home_timeline.
HTTPGET https://api.twitter.com/1.1/statuses/home_timeline.json?count=25&exclude_replies=trueTwitterOAuth
$statuses = $connection->get("statuses/home_timeline", ["count" => 25, "exclude_replies" => true]);
v2 API methods are supported by setting the API Version. E.g. GET /2/users.
HTTPGET https://api.twitter.com/2/users?id=12TwitterOAuth
$connection = new TwitterOAuth(...); $connection->setApiVersion('2'); $response = $connection->get('users', ['ids' => 12]);
TwitterOAuth provides a couple of minimalist wrappers around Twitter's API methods.
Only used when authorizing access to a users account. Includes API methods like POST oauth/request_token and POST oauth/access_token.
Example$access_token = $connection->oauth("oauth/access_token", ["oauth_verifier" => "nMznkpFRTMCuNMsmALzel9FgPlmWQDWg"]);
This is a special wrapper that doesn't hit the API. It builds the URL where users will authorize access to their account at. Only used for GET oauth/authorize and GET oauth/authenticate.
Example$url = $connection->url("oauth/authorize", ["oauth_token" => "EaQLH34YD8pgKkUiSp8RbjjOgNxIYVh7"]);
API methods that are HTTP GET requests. E.g. GET search/tweets.
HTTPGET https://api.twitter.com/1.1/search/tweets.json?q=twitterapiTwitterOAuth
$statuses = $connection->get("search/tweets", ["q" => "twitterapi"]);
API methods that are HTTP POST requests. E.g. POST statuses/update.
HTTPPOST https://api.twitter.com/1.1/statuses/update.json?status=hello%20worldTwitterOAuth
$statues = $connection->post("statuses/update", ["status" => "hello world"]);
Upload images using POST media/upload. This uses the v1.1 API as there is no v2 equivalent.
Note: When creating the application in the Twitter Developer Portal, you must enable Read and Write privileges under Settings > User authentication settings, as otherwise Tweet creation will fail.
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token, $access_token_secret); $connection->setApiVersion(1.1); $media1 = $connection->upload('media/upload', ['media' => '/path/to/file/kitten1.jpg']); $media2 = $connection->upload('media/upload', ['media' => '/path/to/file/kitten2.jpg'], ['chunkedUpload' => true]); $connection->setApiVersion(2); $parameters = [ 'text' => 'Meow Meow Meow', 'media' => ['media_ids' => [$media1->media_id_string, $media2->media_id_string]] ]; $result = $connection->post('tweets', $parameters, ['jsonPayload' => true]);
Send JSON data to POST direct_messages/events/new.
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token, $access_token_secret); $data = [ 'event' => [ 'type' => 'message_create', 'message_create' => [ 'target' => [ 'recipient_id' => $userId ], 'message_data' => [ 'text' => 'Hello World!' ] ] ] ]; $result = $connection->post('direct_messages/events/new', $data, ['jsonPayload' => true]);
Streaming is not currently supported.
HTTP proxy support can be enabled like this.
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token, $access_token_secret); $connection->setProxy([ 'CURLOPT_PROXY' => '127.0.0.0', 'CURLOPT_PROXYUSERPWD' => '', 'CURLOPT_PROXYPORT' => 8080, ]);
After every request you should validate it was a success.
$statues = $connection->post("statuses/update", ["status" => "hello world"]); if (in_array ($connection->getLastHttpCode(), [200, 201])) { // Tweet posted successfully } else { // Handle error case }
If you experience any timeout errors you can change the default timeout settings for cURL.
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token, $access_token_secret); $connection->setTimeouts(10, 15);
The first parameter corresponds to the timeout for the connect phase and the second to the maximum time the request is allowed to take.
If you are looking for an easy way to render user profiles on websites, check out twitter-user.
If you are looking for an easy way to render tweets on websites, check out twitter-status.
Easy TypeScript types for Twitter API objects with twitter-d.ts.
Easily mock test Twitter data with Faker::Twitter .