/twitter-stream-api

:baby_chick: Another Twitter stream PHP library to retrieve filtered tweets on hot.

Primary LanguagePHPMIT LicenseMIT

Twitter Streaming API

License Build Status Code Climate Latest Unstable Version Total Downloads

Another Twitter Stream PHP library. For now it just works on public stream, using the filter method.

Index

Installation

composer require mineur/twitter-stream-api:dev-master

Basic initialization

Instantiate the GuzzleHttpClient adapter with your Twitter api tokens. And start consuming Twitter's Stream with some keywords! :)
If you don't have your Twitter API credentials, check this: How to get your twitter access tokens

use Mineur\TwitterStreamApi\Http\GuzzleStreamClient;
use Mineur\TwitterStreamApi\PublicStream;

$streamClient = new GuzzleStreamClient(
    'consumer_key',
    'consumer_secret',
    'access_token',
    'access_token_secret'
);
PublicStream::open($streamClient)
    ->listenFor([
        'your',
        'keywords',
        'list'
    ])
    ->consume();

Working with the Twitter Stream you cannot open two stream lines with the same account. You should create another app account and raise a new instance of this library.

Callback method

You can also use a callback instead, if you want to modify the original output:

use Mineur\TwitterStreamApi\Tweet;

PublicStream::open($streamClient)
    ->listenFor([
        'your',
        'keywords',
        'list'
    ])
    ->do(function(Tweet $tweet) {
        echo "$tweet->getUser() tweeted: $tweet->getText()";
    });

Filtering tweets by user ID

In this example you'll only get the tweets of a user corresponding to its ID.

$myTwitterId = '1234567';
PublicStream::open($streamClient)
    ->tweetedBy([
        $myTwitterId
    ])
    ->consume();

Filtering keywords by language

In this example you'll only get the tweets on your keywords list write in spanish language.

PublicStream::open($streamClient)
    ->listenFor([
        'keywords',
        'list'
    ])
    ->setLanguage('es')
    ->consume();

The Tweet object

Once you receive the output from the PublicStream, let's say when you are using the do callback function, the output will always be an hydrated Tweet value object.
You can access it with the following methods:

  • Using getters:
// Get specific data from the object
$tweet->getText();
  • As an array:
// Transform object to an array, then access to the data
$aTweet = $tweet->toArray();
$aTweet['text'];
  • Serialized:
// A complete serialized object to enqueue it, for example
$tweet->serialized();

Integrations

For Symfony integrations you can refer to this bundle: Mineur Twitter Stream Api Bundle

Run tests

composer install
./bin/phpunit

To check the coverage just add:

./bin/phpunit --coverage-text

Todo's

  • STREAMING_ENDPOINT should be changed by client, using simple string injection
  • Add links of the filters in documentation
  • Test the first version of this library using Unit testing
  • Handle when a user removes a tweet on the fly.
  • Add filter_level feature
  • Add track location feature