Twitter provides a streaming api with which you can do interesting things such as listening for tweets with specific strings or action a user might take (such as liking a tweet, following someone, ...). Using this package it's very easy to work with the API.
Here's a quick example:
PublicStream::create(
$accessToken,
$accessTokenSecret,
$consumerKey,
$consumerSecret
)->whenHears('@spatie_be', function(array $tweet) {
echo "We got mentioned by {$tweet['user']['screen_name']} who tweeted {$tweet['text']}";
})
->startListening();
There's no polling involved. The package will keep an open http connection with Twitter, events will be delivered in real time.
Under the hood the Phirehose package is used.
You're free to use this package (it's MIT-licensed), but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.
The best postcards will get published on the open source page on our website.
You can install the package via composer:
composer require spatie/twitter-streaming-api
In order to use this package you'll need to get some credentials from Twitter. Head over to the Application management on Twitter to get started. On that site you must create an application.
Once you've created your application, click on the Keys and access tokens
tab to retrieve your consumer_key
, consumer_secret
, access_token
and access_token_secret
.
This package currently can work with the public stream and the user stream. Both the PublicStream
and UserStream
classes provide a startListening
function that kicks of the listening process. Unless you cancel it your PHP process will executed that function forever. No code after the function will be run.
The public stream can be used to listen for specific words that are being tweeted.
The first parameter of whenHears
must be a string or an array containing the word or words you want to listen for. The second parameter should be a callable that will be execute when one of your words get used on Twitter.
PublicStream::create(
$accessToken,
$accessTokenSecret,
$consumerKey,
$consumerSecret
)->whenHears('@spatie_be', function(array $tweet) {
echo "We got mentioned by {$tweet['user']['screen_name']} who tweeted {$tweet['text']}";
})
->startListening();
UserStream::create(
$accessToken,
$accessTokenSecret,
$consumerKey,
$consumerSecret
)->onEvent(function(array $event) {
if ($event['event'] === 'favorite') {
echo "Our tweet {$event['target_object']['text']} got favorited by {$event['source']['screen_name']}";
}
})
->startListening();
These API's work in realtime. It's possible that they will report back a lot of activity. If you need to do some heavy work processing that activity it's best to put that work in a queue so your listening process stays fast.
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
The MIT License (MIT). Please see License File for more information.