PHP code example of spatie / laravel-twitter-streaming-api

1. Go to this page and download the library: Download spatie/laravel-twitter-streaming-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

spatie / laravel-twitter-streaming-api example snippets


TwitterStreamingApi::publicStream()
->whenHears('#laravel', function(array $tweet) {
    echo "{$tweet['user']['screen_name']} tweeted {$tweet['text']}";
})
->startListening();

return [

    /*
     * To work with Twitter's Streaming API you'll need some credentials.
     *
     * If you don't have credentials yet, head over to https://developers.twitter.com/
     */

    'handle' => env('TWITTER_HANDLE'),

    'api_key' => env('TWITTER_API_KEY'),

    'api_secret_key' => env('TWITTER_API_SECRET_KEY'),

    'bearer_token' => env('TWITTER_BEARER_TOKEN'),
];

app(Spatie\LaravelTwitterStreamingApi\TwitterStreamingApi::class)

use TwitterStreamingApi;

TwitterStreamingApi::publicStream()
->whenHears('#laravel', function(array $tweet) {
    echo "{$tweet['user']['screen_name']} tweeted {$tweet['text']}";
})
->startListening();

use TwitterStreamingApi;

TwitterStreamingApi::userStream()
->onEvent(function(array $event) {
    if ($event['event'] === 'favorite') {
        echo "Our tweet {$event['target_object']['text']} got favorited by {$event['source']['screen_name']}";
    }
})
->startListening();
bash
php artisan vendor:publish --provider="Spatie\LaravelTwitterStreamingApi\TwitterStreamingApiServiceProvider" --tag="config"