Coder Social home page Coder Social logo

twitter-streaming-api's Introduction

Easily work with the Twitter Streaming API

Latest Version on Packagist Software License run-tests Total Downloads

Twitter provides a streaming API with which you can do interesting things such as listening for tweets that contain specific strings or actions a user might take (e.g. liking a tweet, following someone,...). This package makes it very easy to work with the API.

Here's a quick example:

PublicStream::create(
    $bearerToken,
    $apiKey,
    $apiSecretKey
)->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 https connection with Twitter, events will be delivered in real time.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Postcardware

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, Kruikstraat 22, 2018 Antwerp, Belgium.

The best postcards will get published on the open source page on our website.

Installation

You can install the package via composer:

composer require spatie/twitter-streaming-api

Getting credentials

In order to use this package you'll need to get some credentials from Twitter. Head over to the Developer Portal on Twitter to create an application.

Once you've created your application, click on the Keys and tokens tab to retrieve your bearer_token , api_key, api_secret_key. Keys and tokens tab on Twitter

Usage

Currently, this package works 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 execute that function forever. No code after the function will be run.

The public stream

The public stream can be used to listen for specific words that are being tweeted, receive Tweets that are being sent from specific locations or to follow one or more users tweets.

Listen for Tweets containing specific words

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 executed when one of your words is used on Twitter.

PublicStream::create(
    $bearerToken,
    $apiKey,
    $apiSecretKey
)->whenHears('@spatie_be', function(array $tweet) {
    echo "We got mentioned by {$tweet['user']['screen_name']} who tweeted {$tweet['text']}";
})->startListening();

Listen for Tweets from specific locations

The first parameter of whenFrom must be an array containing one or more bounding boxes, each as an array of 4 element lon/lat pairs (looking like [<south-west point longitude>, <south-west point latitude>, <north-east point longitude>, <north-east point latitude>]) . The second parameter should be a callable that will be executed when a Tweet from one of your tracked locations is being sent.

Track all tweets from San Francisco or New York:

PublicStream::create(
    $bearerToken,
    $apiKey,
    $apiSecretKey
)->whenFrom([
    [-122.75, 36.8, -121.75, 37.8], // San Francisco
    [-74, 40, -73, 41],             // New York
], function(array $tweet) {
        echo "{$tweet['user']['screen_name']} just tweeted {$tweet['text']} from SF or NYC";
})->startListening();

Track all tweets with a location (from all over the world):

PublicStream::create(
    $bearerToken,
    $apiKey,
    $apiSecretKey
)->whenFrom([
        [-180, -90, 180, 90] // Whole world
], function(array $tweet) {
    echo "{$tweet['user']['screen_name']} just tweeted {$tweet['text']} with a location attached";
})->startListening();

Listen for Tweets from specific users

The first parameter of whenTweets must be a string or an array containing the Twitter user ID or IDs you wish to follow. The second parameter should be a callable that will be executed when one of your followed users tweets. Only public information relating to the Twitter user will be available.

PublicStream::create(
    $bearerToken,
    $apiKey,
    $apiSecretKey
)->whenTweets('92947501', function(array $tweet) {
    echo "{$tweet['user']['screen_name']} just tweeted {$tweet['text']}";
})->startListening();

The user stream

UserStream::create(
    'your_handle',
    $bearerToken,
    $apiKey,
    $apiSecretKey
)->onEvent(function(array $event) {
    if ($event['event'] === 'favorite') {
        echo "Our tweet {$event['target_object']['text']} got favorited by {$event['source']['screen_name']}";
    }
})->startListening();

A word to the wise

These APIs work in realtime, so they could report 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 to keep your listening process fast.

If you need more advanced functionalities, consider checking out redwebcreation/twitter-streaming-api.

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you've found a bug regarding security please mail [email protected] instead of using the issue tracker.

Credits

About Spatie

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

License

The MIT License (MIT). Please see License File for more information.

twitter-streaming-api's People

Contributors

adrianmrn avatar blueclock avatar felixdorn avatar freekmurze avatar ilukecurtis avatar jonasdoebertin avatar lorenzbausch avatar sebastiandedeyne avatar votintsev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

twitter-streaming-api's Issues

Event isn't an array

When I'm using UserStream I am not getting an array back. Rather it looks like some string with tweet information. Did the Twitter response format change?

More Examples

can you provide more examples on using this library.
Im trying to figure out how to catch new followers.

Connection keeps failing once a day

Instead of just always being connected, I find myself waking up in the morning and seeing that I am no longer pulling in tweets and have to restart my artisan command for this. Now I am pulling in like 30,000 tweets a day. But that shouldn't be the issue...

Here is the error I have.

[2018-05-25 04:29:26] local.ERROR: fgets(): SSL: Connection reset by peer {"exception":"[object] (ErrorException(code: 0): fgets(): SSL: Connection reset by peer at /home/174711.cloudwaysapps.com/dhewyfpjgy/public_html/vendor/fennb/phirehose/lib/Phirehose.php:443) [stacktrace] #0 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'fgets(): SSL: C...', '/home/174711.cl...', 443, Array) #1 /home/174711.cloudwaysapps.com/dhewyfpjgy/public_html/vendor/fennb/phirehose/lib/Phirehose.php(443): fgets(Resource id #391) #2 /home/174711.cloudwaysapps.com/dhewyfpjgy/public_html/app/Console/Commands/ConnectToStreamingAPI.php(61): Phirehose->consume() #3 [internal function]: App\Console\Commands\ConnectToStreamingAPI->handle() #4 /home/174711.cloudwaysapps.com/dhewyfpjgy/public_html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(29): call_user_func_array(Array, Array) #5 /home/174711.cloudwaysapps.com/dhewyfpjgy/public_html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}() #6 /home/174711.cloudwaysapps.com/dhewyfpjgy/public_html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure)) #7 /home/174711.cloudwaysapps.com/dhewyfpjgy/public_html/vendor/laravel/framework/src/Illuminate/Container/Container.php(564): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL) #8 /home/174711.cloudwaysapps.com/dhewyfpjgy/public_html/vendor/laravel/framework/src/Illuminate/Console/Command.php(183): Illuminate\Container\Container->call(Array) #9 /home/174711.cloudwaysapps.com/dhewyfpjgy/public_html/vendor/symfony/console/Command/Command.php(251): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle)) #10 /home/174711.cloudwaysapps.com/dhewyfpjgy/public_html/vendor/laravel/framework/src/Illuminate/Console/Command.php(170): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle)) #11 /home/174711.cloudwaysapps.com/dhewyfpjgy/public_html/vendor/symfony/console/Application.php(865): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #12 /home/174711.cloudwaysapps.com/dhewyfpjgy/public_html/vendor/symfony/console/Application.php(241): Symfony\Component\Console\Application->doRunCommand(Object(App\Console\Commands\ConnectToStreamingAPI), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #13 /home/174711.cloudwaysapps.com/dhewyfpjgy/public_html/vendor/symfony/console/Application.php(143): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #14 /home/174711.cloudwaysapps.com/dhewyfpjgy/public_html/vendor/laravel/framework/src/Illuminate/Console/Application.php(89): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #15 /home/174711.cloudwaysapps.com/dhewyfpjgy/public_html/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(122): Illuminate\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #16 /home/174711.cloudwaysapps.com/dhewyfpjgy/public_html/artisan(37): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #17 {main} "}

Hears from string and not just on hashtags

Hi,

First of all, thanks for this great package.
I faced with an issue, it seems whenHears function just search characters in hashtags, but I would like to search in all characters besides hashtags.
Could you give me some tricks?
Does this function support what I mentioned in above?

Thank in advance.

Get event for multiple users.

I want to get all event of multiple users.
Using User stream API will get the event for the single user but want to get the event for multiple users.

This code is working fine for the single user.

TwitterStreamingApi::userStream()
->onEventUsers(function(array $event) {
if ($event['event'] === 'favorite') {
echo "Our tweet {$event['target_object']['text']} got favourited by {$event['source']['screen_name']}";
}
})
->startListening();

filters

how to exlude replies or retweets?

Close Connection

I was wondering if there is a way to close the connection if you are not tracking anything. Might be useful for dynamic services. I can't access the clse connection method because is protected.

Run on older PHP versions

Hi, thanks for this. Only problem is, I am using PHP 5.6.32 - and I see PHP 7 is required. Your code will run on older PHP too, or it is not possible ?

Thanks

Fix PHP 7.2 warnings

The underlying phirehose package issues a warning in PHP 7.2.

There is a PR opened to fix this, but it is not merged for 4 months: fennb/phirehose#113

What is the workflow in this case, to be able to use your package in PHP 7.2?

Thanks!

multiple job on queue

Hi all,
Please forgive it it's not a problem with this package but...I'm trying to debug where the same tweet is being added to the redis queue multiple times and wonder if it's a know issue with this or any other part of the stack?

latest versions of...
Laravel, Redis, Horizon, Laradock, Docker

I have set up a listener for a mention, I add it to a queue to process but it is added at least twice and on some reboots 3 times.
my listener command is..

public function handle()
    {
        $handle = config('platform.twitter.listener_handle');
        TwitterStreamingApi::publicStream()
            ->whenHears($handle, function (array $tweet) {
                $job = (new ProcessTweet($tweet))->onQueue('incoming');
                $this->dispatch($job);
            })
            ->startListening();
    }

everything works, i get and process every tweet perfectly. it just adds multiple tweets to the queue.

EDIT:
I have replace the contents of whenHears to Log the tweet and it is logging it twice too. From this I can discount redis/queue system but Laradock php-worker may still be a factor.

I can post answers to any questions you may have.
any thoughts?

Multi user ids not working in whenTweets

Hi whenTweets method not working with array of ids. Its working fine with single id but when i use array of ids i am getting nothing.

\Spatie\TwitterStreamingApi\PublicStream::create("AAAAAAAAAAAAAAAAAAAAABLGZwEAAA........", "zyiZ4J..............", "DJzgE....................."
        )->whenTweets(['15004825485......','1492193579........'], function(array $tweet) {

            print("Tweet ". $tweet["data"]["text"]) . PHP_EOL;
            
        })->startListening();

install without composer

Hi,
would you please tell me how I can install this package without composer?

I added this code:
require_once ('twitter-streaming-api-master/src/BaseStream.php'); require_once ('twitter-streaming-api-master/src/PublicStream.php'); require_once ('twitter-streaming-api-master/src/UserStream.php'); require_once ('twitter-streaming-api-master/src/PhirehoseWrapper.php');

but I got this error:

Class 'OauthPhirehose' not found

Error in PHP 7.2

This package or rather the dependency library has an issue with PHP 7.2 and a PR is already created. However, the package maintainer doesn't seem to be active: fennb/phirehose#113

Perhaps you can consider rewriting this with something like ReatPHP/Amphp? Or maybe use another library.

Thanks!

Expose the phirehose class

Hey, how can I access the underlying Phirehose class to set custom options e.g location coordinates?

As the package as is right now, this is not easily doable

Add a new user to array of when hears

How can we add a user to the user id of the whenHears() while the scheduler is running so I don't need to stop the process and restart but call a method to add user id to the array. Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.