Another Twitter Stream PHP library. For now it just works on public stream, using the filter method.
composer require mineur/twitter-stream-api:dev-master
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.
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()";
});
In this example you'll only get the tweets of a user corresponding to its ID.
$myTwitterId = '1234567';
PublicStream::open($streamClient)
->tweetedBy([
$myTwitterId
])
->consume();
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();
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();
For Symfony integrations you can refer to this bundle: Mineur Twitter Stream Api Bundle
composer install
./bin/phpunit
To check the coverage just add:
./bin/phpunit --coverage-text
- 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