PHPackages                             abhishekbhardwaj/twitter-api-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. abhishekbhardwaj/twitter-api-php

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

abhishekbhardwaj/twitter-api-php
================================

Simple PHP wrapper for Twitter v1.1 API. Uses Guzzle!

v1.1.5(11y ago)178453[1 issues](https://github.com/abhishekbhardwaj/Twitter-API-PHP/issues)MITPHPPHP &gt;=5.4.0

Since Nov 8Pushed 11y ago2 watchersCompare

[ Source](https://github.com/abhishekbhardwaj/Twitter-API-PHP)[ Packagist](https://packagist.org/packages/abhishekbhardwaj/twitter-api-php)[ Docs](https://github.com/abhishekbhardwaj/Twitter-API-PHP)[ RSS](/packages/abhishekbhardwaj-twitter-api-php/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (13)Used By (0)

Simple PHP Wrapper for Twitter API v1.1
---------------------------------------

[](#simple-php-wrapper-for-twitter-api-v11)

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:

- [Application-Only Authentication](https://dev.twitter.com/oauth/application-only) (Oauth 2.0).
- [Single-User OAuth](https://dev.twitter.com/oauth/overview/single-user) (Oauth 1.0a).
- [3-legged Authorization](https://dev.twitter.com/oauth/3-legged) (Oauth 1.0a).

### Installation

[](#installation)

To install via [Composer](https://getcomposer.org), add the following to your composer.json file and run `composer install`:

```
    {
        "require": {
            "abhishekbhardwaj/twitter-api-php": "~1.1"
        }
    }

```

### Dependencies

[](#dependencies)

```
"php": ">=5.4.0",
"guzzlehttp/guzzle": "~5.0",
"guzzlehttp/oauth-subscriber": "~0.2"

```

### Basic Usage

[](#basic-usage)

Some examples are located in the `examples/` directory:

- appExample.php: Example of Application-Only Authentication
- userExample.php: Example of 3-legged Authorization

Detailed explanation can be found below:

#### To use Twitter as an application:

[](#to-use-twitter-as-an-application)

- Create an instance of `Twitter\Config\AppCredentials` class with appropriate API keys.

```
$credentials = new AppCredentials($consumerKey, $consumerSecret);
```

- Create an instance of `Twitter\Client` with the `AppCredentials` object you just created.

```
$client = new Client($credentials);
```

- Create an Application Connection to Twitter.

```
$app = $client->connect();
```

- When using Twitter as an application, you need to use a Bearer Token which can be generated by calling the `createBearerToken()` function.

```
$app->createBearerToken();

//to access bearer token (for storing in db for later use), you can use:
$app->getCredentials()->getBearerToken();
```

- Now you can send GET requests (since you can only GET as an application from Twitter) to Twitter. \[Details available here!\]{[https://dev.twitter.com/oauth/application-only}](https://dev.twitter.com/oauth/application-only%7D)

```
//getting @abhishekwebin's recent timeline.
$response = $app->get('users/show.json', array('screen_name' => 'abhishekwebin'));
```

- Since we use Guzzle internally to talk to Twitter, the `$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](http://guzzle.readthedocs.org/en/latest/quickstart.html).

#### To use Twitter as a user:

[](#to-use-twitter-as-a-user)

*this section shows you how to login as a user*

- create an instance of `Twitter\Config\UserCredentials` class with appropriate API keys.

```
$credentials = new UserCredentials($consumerKey, $consumerSecret, $callbackUrl);
```

- Create an instance of `Twitter\Client` with the `UserCredentials` object you just created.

```
$client = new Client($credentials);
```

- Create a User Connection to Twitter.

```
$app = $client->connect();
```

- Get Authorization URL.

```
$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-from-an-endpoint)

```
//getting @abhishekwebin's recent timeline.
$response = $app->get('users/show.json', array('screen_name' => 'abhishekwebin'));
```

#### `POST`ing to an endpoint:

[](#posting-to-an-endpoint)

```
//post a new tweet: 'Test status!' as the currently authenticated users.
$response = $app->post('statuses/update.json', array('status' => 'Test status!'));
```

#### Uploading pictures with a Tweet:

[](#uploading-pictures-with-a-tweet)

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.

### Todo

[](#todo)

- Change namespace to something other than `Twitter\*`
- More documentation.

### Changelog

[](#changelog)

- November 12, 2014: `Version 1.1.2` - Parameters are now optional on GET requests.
- November 9, 2014: `Version 1.1.*` - more stable and also lets you upload images to Twitter.
- November 8, 2014: `Version 1.0.*` - very first version of the library, didn't have support for media uploads.

### Important Links

[](#important-links)

1. All Twitter API Endpoints can be found [here](https://dev.twitter.com/rest/public).
2. All Twitter Authentication related stuff can be found [here](https://dev.twitter.com/oauth).

### Contribution

[](#contribution)

If you find any bugs, either post an issue or pull request are always welcome! :)

### License

[](#license)

This library is licensed under the MIT License. See the `LICENSE` file for more details!

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

12

Last Release

4184d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/00c266bb1f44f79804f2da3972668bd48e8127d911eeb6a60aec579b0790e751?d=identicon)[abhishekbhardwaj](/maintainers/abhishekbhardwaj)

---

Top Contributors

[![abhishekbhardwaj](https://avatars.githubusercontent.com/u/1156779?v=4)](https://github.com/abhishekbhardwaj "abhishekbhardwaj (42 commits)")

---

Tags

authenticationoauthphptwitter-apiphpapisdktwitter

### Embed Badge

![Health badge](/badges/abhishekbhardwaj-twitter-api-php/health.svg)

```
[![Health](https://phpackages.com/badges/abhishekbhardwaj-twitter-api-php/health.svg)](https://phpackages.com/packages/abhishekbhardwaj-twitter-api-php)
```

###  Alternatives

[bocharsky-bw/vkontakte-php-sdk

Vkontakte PHP SDK

3259.2k1](/packages/bocharsky-bw-vkontakte-php-sdk)[kinde-oss/kinde-auth-php

Kinde PHP SDK for authentication

2369.5k3](/packages/kinde-oss-kinde-auth-php)[surfoo/geocaching-php-sdk

Geocaching PHP SDK

143.4k1](/packages/surfoo-geocaching-php-sdk)[ory/hydra-client-php

Documentation for all of Ory Hydra's APIs.

1710.8k](/packages/ory-hydra-client-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
