mineur / twitter-stream-api
Collector of streaming twitter data
Installs: 66
Dependents: 1
Suggesters: 0
Security: 0
Stars: 11
Watchers: 0
Forks: 2
Open Issues: 0
Type:php-library
Requires
- php: >=7.1
- guzzlehttp/guzzle: ^6.2
- guzzlehttp/oauth-subscriber: ^0.3.0
Requires (Dev)
- fzaninotto/faker: ^1.6
- mmoreram/php-formatter: ^1.3
- mockery/mockery: ^0.9.9
- phpunit/phpunit: ^6.1
- symfony/var-dumper: ^3.2
This package is not auto-updated.
Last update: 2025-03-23 06:08:40 UTC
README
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