A simple PHP wrapper library for Twitter API v1.1 calls.
MIT License
Twitter-API-PHP is a simple PHP wrapper library for Twitter API v1.1 calls. It has been designed for making the Twitter API as simple to use as possible for developers (on the server or on the client).
It supports:
To install via Composer, add the following to your composer.json file and run composer install
:
{
"require": {
"abhishekbhardwaj/twitter-api-php": "~1.1"
}
}
"php": ">=5.4.0",
"guzzlehttp/guzzle": "~5.0",
"guzzlehttp/oauth-subscriber": "~0.2"
Some examples are located in the examples/
directory:
Detailed explanation can be found below:
Twitter\Config\AppCredentials
class with appropriate API keys.$credentials = new AppCredentials($consumerKey, $consumerSecret);
Twitter\Client
with the AppCredentials
object you just created.$client = new Client($credentials);
$app = $client->connect();
createBearerToken()
function.$app->createBearerToken();
//to access bearer token (for storing in db for later use), you can use:
$app->getCredentials()->getBearerToken();
//getting @abhishekwebin's recent timeline.
$response = $app->get('users/show.json', array('screen_name' => 'abhishekwebin'));
$response
object is an instance of GuzzleHttp\Message\ResponseInterface
and thus, it has many helpful functions for example:$response->getHeaders(); //gets the response headers
$response->getStatusCode(); //gets the status code
$response->json(); //parse response as JSON and return decoded data as assoc-array
$response->getBody(); //gets the response body
For more on Guzzle, refer to its official documentation here.
this section shows you how to login as a user
Twitter\Config\UserCredentials
class with appropriate API keys.$credentials = new UserCredentials($consumerKey, $consumerSecret, $callbackUrl);
Twitter\Client
with the UserCredentials
object you just created.$client = new Client($credentials);
$app = $client->connect();
$redirectUrl = $app->getRedirectUrlForAuth();
Redirect the user to that URL.
Once the user logs in, Twitter will redirect the user back to the callbackUrl you specified in your app settings and in the credentials above.
The callback url will get a temporary oauthToken and oauthVerifier as query parameters. Use them to generate an access token (token and secret):
$accessTokens = $app->getAccessToken($oauthToken, $oauthVerifier);
$accessTokens
is an assoc-array which contains oauth_token
and oauth_token_secret
which basically never expire. You can store these in the db.
Now you send either GET or POST requests to Twitter on behalf of the authenticated user to the endpoints.
GET
ting from an endpoint://getting @abhishekwebin's recent timeline.
$response = $app->get('users/show.json', array('screen_name' => 'abhishekwebin'));
POST
ing to an endpoint://post a new tweet: 'Test status!' as the currently authenticated users.
$response = $app->post('statuses/update.json', array('status' => 'Test status!'));
A maximum of 4 pictures can be attached to a tweet. To do so:
//list of pictures to upload. Input array items shouldn't be more than 4.
$media = $app->uploadMedia(array(
'{{FULL PATH TO PICTURE}}',
'{{FULL PATH TO PICTURE}}',
'{{FULL PATH TO PICTURE}}',
'{{FULL PATH TO PICTURE}}'
));
//post a new status
$response = $app->post('statuses/update.json', array(
'status' => 'Test status!',
'media_ids' => $media
));
The list of pictures that is passed on to uploadMedia()
can be URL's or absolute file paths.
Twitter\*
Version 1.1.2
- Parameters are now optional on GET requests.Version 1.1.*
- more stable and also lets you upload images to Twitter.Version 1.0.*
- very first version of the library, didn't have support for media uploads.If you find any bugs, either post an issue or pull request are always welcome! :)
This library is licensed under the MIT License. See the LICENSE
file for more details!