PHPackages                             mutantlabs/twitter-rest - 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. mutantlabs/twitter-rest

ActiveLibrary

mutantlabs/twitter-rest
=======================

A simple Class to interface with Twitter API v1.1 using OAuth, and return tweets as JSON via a get method. Uses RestService and twitteroauth

1141PHP

Since Jun 28Pushed 12y ago1 watchersCompare

[ Source](https://github.com/mutantlabs/TwitterRest)[ Packagist](https://packagist.org/packages/mutantlabs/twitter-rest)[ RSS](/packages/mutantlabs-twitter-rest/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

twitter-rest
============

[](#twitter-rest)

A simple Class to interface with Twitter API v1.1 using OAuth, and return tweets as JSON via a get method. Uses RestService and twitteroauth

\#Install

Requires

Install twitter-rest with Composer:

-
- More information available under .

Create a `composer.json`:

```
{
    "require": {
        "mutantlabs/twitter-rest": "dev-master"
    }
}
```

and run

```
$ wget http://getcomposer.org/composer.phar
$ php composer.phar install
```

Requirements
------------

[](#requirements)

- PHP 5.3 and above.
- PHPUnit to execute the test suite.
- Setup PATH\_INFO in mod\_rewrite (.htaccess) or other webserver configuration

Example:

```
//apache .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

```

\#Config

create config.php as follows:

Set your config.php (uses twitteroauth from )

```
define('CONSUMER_KEY', 'CONSUMER_KEY_HERE');
define('CONSUMER_SECRET', 'CONSUMER_SECRET_HERE');
define('OAUTH_TOKEN', 'OAUTH_TOKEN_HERE');
define('OAUTH_TOKEN_SECRET', 'OAUTH_TOKEN_SECRET_HERE');
define('OAUTH_CALLBACK', 'http://example.com/twitteroauth/callback.php');
```

\#Example use of TwitterRestAPI

include the vendor/autoload.php to make the class in your script available.

```
include 'vendor/autoload.php';
```

\##Basic use:

Construct new TwitterRestAPI

```
use TwitterRest\TwitterRestAPI;
require_once('config.php');
$twitterRestApi = new TwitterRestAPI();
```

Get some tweets

```
$tweets = $twitterRestApi->getCachedUserStatus();
$arrTweets = array();
foreach($tweets as $key => $status) {
    $arrTweets[$key] = array(
        'created_at' => $status->created_at,
        'text' => $status->text
    );
}
echo json_encode($arrTweets);
```

Note - to use getCachedUserStatus() - you need to make your apache server the owner of twitter\_result.data:

```
sudo chown -R www-data:www-data twitter_result.data
```

\#Create a REST API Method using php-rest-service
-------------------------------------------------

[](#create-a-rest-api-method-using-php-rest-service)

```
use TwitterRest\TwitterRestAPI;
require_once('config.php');

TwitterRestAPI::create('/')
    ->addGetRoute('', function(){
        $twitterRestApi = new TwitterRestAPI();
        $tweets = $twitterRestApi->getCachedUserStatus();
        $arrTweets = array();
        foreach($tweets as $key => $status) {
            $arrTweets[$key] = array(
                'created_at' => $status->created_at,
                'text' => $status->text
            );
        }
        return $arrTweets;
    })
    ->run();
```

Extended Auth flow taken from

```
    ->addGetRoute('authenticate', function(){
            //When a user lands on /authenticate we build a new TwitterOAuth object using the client credentials.
            $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);

            //Using the built $connection object you will ask Twitter for temporary credentials. The oauth_callback value is required.
            $temporary_credentials = $connection->getRequestToken(OAUTH_CALLBACK);

            //Once we have temporary credentials the user has to go to Twitter and authorize the app to access and updates their data.
            $redirect_url = $connection->getAuthorizeURL($temporary_credentials, FALSE);

            return array($temporary_credentials,$redirect_url);
        })
```

The user is now on twitter.com and may have to login. Once authenticated with Twitter they will will either have to click on allow/deny, or will be automatically redirected back to the callback. in this case its example.domain.com/success

Once the user has returned to /success and allowed access we need to build a new TwitterOAuth object using the temporary credentials.

```
        ->addGetRoute('success', function(){
            $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_GET['oauth_token'],
                $_GET['oauth_verifier']);

            //Now we ask Twitter for long lasting token credentials. These are specific to the application and user and will act like password to make future requests.
            $token_credentials = $connection->getAccessToken($_REQUEST['oauth_verifier']);

            Here we can from our token credentials back to the application (i'd suggest a more secure method of sending these than encoded JSON. but here for example):
            return array($token_credentials);
        })
```

From here with the token credentials we build a new TwitterOAuth object.

```
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $token_credentials['oauth_token'],
$token_credentials['oauth_token_secret']);
```

And make requests authenticated as the user:

```
$status = $connection->post('statuses/update', array('status' => 'Text of status here', 'in_reply_to_status_id' => 123456));
```

License
-------

[](#license)

- Licensed under the MIT License. See the LICENSE file for more details.
- marcj/php-rest-service is Licensed under the MIT License. See  for more details
- abraham/twitteroauth

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/12ae7295eb43363471a03a3f9e62c27991bfdcccb5f873f978e5f9a76119a10d?d=identicon)[mutantlabs](/maintainers/mutantlabs)

---

Top Contributors

[![mijahn-ben](https://avatars.githubusercontent.com/u/857085?v=4)](https://github.com/mijahn-ben "mijahn-ben (17 commits)")

### Embed Badge

![Health badge](/badges/mutantlabs-twitter-rest/health.svg)

```
[![Health](https://phpackages.com/badges/mutantlabs-twitter-rest/health.svg)](https://phpackages.com/packages/mutantlabs-twitter-rest)
```

PHPackages © 2026

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