├── .github ├── FUNDING.yml ├── ISSUE_TEMPLATE │ └── config.yml └── workflows │ ├── php-cs-fixer.yml │ └── run-tests.yml ├── docs └── tokens.png ├── .phpunit.result.cache ├── .editorconfig ├── config └── laravel-twitter-streaming-api.php ├── src ├── TwitterStreamingApiFacade.php ├── TwitterStreamingApiServiceProvider.php └── TwitterStreamingApi.php ├── CHANGELOG.md ├── LICENSE.md ├── .php-cs-fixer.dist.php ├── composer.json └── README.md /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: spatie 2 | -------------------------------------------------------------------------------- /docs/tokens.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spatie/laravel-twitter-streaming-api/HEAD/docs/tokens.png -------------------------------------------------------------------------------- /.phpunit.result.cache: -------------------------------------------------------------------------------- 1 | {"version":1,"defects":[],"times":{"Spatie\\LaravelTwitterStreamingApi\\Test\\FacadeTest::it_can_return_a_public_stream":0.105,"Spatie\\LaravelTwitterStreamingApi\\Test\\FacadeTest::it_can_return_a_user_stream":0.005}} -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | ; This file is for unifying the coding style for different editors and IDEs. 2 | ; More information at http://editorconfig.org 3 | 4 | root = true 5 | 6 | [*] 7 | charset = utf-8 8 | indent_size = 4 9 | indent_style = space 10 | end_of_line = lf 11 | insert_final_newline = true 12 | trim_trailing_whitespace = true 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: true 2 | contact_links: 3 | - name: Feature Request 4 | url: https://github.com/spatie/laravel-twitter-streaming-api/discussions/new?category=ideas 5 | about: Share ideas for new features 6 | - name: Ask a Question 7 | url: https://github.com/spatie/laravel-twitter-streaming-api/discussions/new?category=q-a 8 | about: Ask the community for help 9 | -------------------------------------------------------------------------------- /config/laravel-twitter-streaming-api.php: -------------------------------------------------------------------------------- 1 | env('TWITTER_HANDLE'), 12 | 13 | 'api_key' => env('TWITTER_API_KEY'), 14 | 15 | 'api_secret_key' => env('TWITTER_API_SECRET_KEY'), 16 | 17 | 'bearer_token' => env('TWITTER_BEARER_TOKEN'), 18 | ]; 19 | -------------------------------------------------------------------------------- /src/TwitterStreamingApiFacade.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 12 | $this->publishes([ 13 | __DIR__.'/../config/laravel-twitter-streaming-api.php' => config_path('laravel-twitter-streaming-api.php'), 14 | ], 'config'); 15 | } 16 | } 17 | 18 | public function register() 19 | { 20 | $this->mergeConfigFrom(__DIR__.'/../config/laravel-twitter-streaming-api.php', 'laravel-twitter-streaming-api'); 21 | 22 | $this->app->bind('laravel-twitter-streaming-api', TwitterStreamingApi::class); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-twitter-streaming-api` will be documented in this file 4 | 5 | ## 2.1.0 - 2022-02-09 6 | 7 | - add support for Laravel 9 8 | 9 | ## 2.0.1 - 2021-05-31 10 | 11 | - fix unauthorized error due to params order (#38) 12 | 13 | ## 2.0.0 - 2021-05-17 14 | 15 | - Twitter API v2 compatibility (#36) 16 | 17 | ## 1.4.1 - 2020-12-12 18 | 19 | - allow PHP 8 20 | 21 | ## 1.4.0 - 2020-09-09 22 | 23 | - add support for Laravel 8 24 | 25 | ## 1.3.0 - 2020-03-03 26 | 27 | - add support for Laravel 7 28 | 29 | ## 1.2.0 - 2019-09-04 30 | 31 | - add support for Laravel 6 32 | - dropped support for older Laravel versions 33 | 34 | ## 1.1.1 - 2019-03-08 35 | 36 | - allow dev dependenices (required to install Phirehose dev-master for PHP 7.2 and above) 37 | 38 | ## 1.1.0 - 2017-10-30 39 | 40 | - support L5.5 autodiscovery 41 | 42 | ## 1.0.1 - 2017-03-13 43 | 44 | - fix dependencies 45 | 46 | ## 1.0.0 - 2017-02-01 47 | 48 | - initial release 49 | -------------------------------------------------------------------------------- /.github/workflows/php-cs-fixer.yml: -------------------------------------------------------------------------------- 1 | name: Check & fix styling 2 | 3 | on: [push] 4 | 5 | jobs: 6 | style: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - name: Checkout code 11 | uses: actions/checkout@v1 12 | 13 | - name: Fix style 14 | uses: docker://oskarstark/php-cs-fixer-ga 15 | with: 16 | args: --config=.php-cs-fixer.dist.php --allow-risky=yes 17 | 18 | - name: Extract branch name 19 | shell: bash 20 | run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})" 21 | id: extract_branch 22 | 23 | - name: Commit changes 24 | uses: stefanzweifel/git-auto-commit-action@v2.3.0 25 | with: 26 | commit_message: Fix styling 27 | branch: ${{ steps.extract_branch.outputs.branch }} 28 | env: 29 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 30 | -------------------------------------------------------------------------------- /src/TwitterStreamingApi.php: -------------------------------------------------------------------------------- 1 | config = $config['laravel-twitter-streaming-api']; 17 | } 18 | 19 | public function publicStream(): PublicStream 20 | { 21 | return new PublicStream( 22 | $this->config['bearer_token'], 23 | $this->config['api_key'], 24 | $this->config['api_secret_key'] 25 | ); 26 | } 27 | 28 | public function userStream(): UserStream 29 | { 30 | return new UserStream( 31 | $this->config['handle'], 32 | $this->config['bearer_token'], 33 | $this->config['api_key'], 34 | $this->config['api_secret_key'] 35 | ); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Spatie bvba 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /.php-cs-fixer.dist.php: -------------------------------------------------------------------------------- 1 | in([ 5 | __DIR__ . '/src', 6 | __DIR__ . '/tests', 7 | ]) 8 | ->name('*.php') 9 | ->notName('*.blade.php') 10 | ->ignoreDotFiles(true) 11 | ->ignoreVCS(true); 12 | 13 | return (new PhpCsFixer\Config()) 14 | ->setRules([ 15 | '@PSR2' => true, 16 | 'array_syntax' => ['syntax' => 'short'], 17 | 'ordered_imports' => ['sort_algorithm' => 'alpha'], 18 | 'no_unused_imports' => true, 19 | 'not_operator_with_successor_space' => true, 20 | 'trailing_comma_in_multiline' => true, 21 | 'phpdoc_scalar' => true, 22 | 'unary_operator_spaces' => true, 23 | 'binary_operator_spaces' => true, 24 | 'blank_line_before_statement' => [ 25 | 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], 26 | ], 27 | 'phpdoc_single_line_var_spacing' => true, 28 | 'phpdoc_var_without_name' => true, 29 | 'method_argument_space' => [ 30 | 'on_multiline' => 'ensure_fully_multiline', 31 | 'keep_multiple_spaces_after_comma' => true, 32 | ], 33 | 'single_trait_insert_per_statement' => true, 34 | ]) 35 | ->setFinder($finder); 36 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "spatie/laravel-twitter-streaming-api", 3 | "description": "Easily work with the Twitter Streaming API in a Laravel app", 4 | "keywords": [ 5 | "spatie", 6 | "laravel-twitter-streaming-api" 7 | ], 8 | "homepage": "https://github.com/spatie/laravel-twitter-streaming-api", 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "Freek Van der Herten", 13 | "email": "freek@spatie.be", 14 | "homepage": "https://spatie.be", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.0", 20 | "illuminate/support": "^8.0|^9.0|^10.0|^11.0|^12.0", 21 | "spatie/twitter-streaming-api": "^2.0.0" 22 | }, 23 | "require-dev": { 24 | "friendsofphp/php-cs-fixer": "^3.0", 25 | "orchestra/testbench": "^6.0|^7.0|^8.0|^9.0|^10.0", 26 | "phpunit/phpunit": "^9.0|^10.5|^11.5.3" 27 | }, 28 | "autoload": { 29 | "psr-4": { 30 | "Spatie\\LaravelTwitterStreamingApi\\": "src" 31 | } 32 | }, 33 | "autoload-dev": { 34 | "psr-4": { 35 | "Spatie\\LaravelTwitterStreamingApi\\Test\\": "tests" 36 | } 37 | }, 38 | "scripts": { 39 | "lint": "./vendor/bin/php-cs-fixer fix", 40 | "test": "./vendor/bin/phpunit", 41 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 42 | }, 43 | "minimum-stability": "dev", 44 | "prefer-stable": true, 45 | "config": { 46 | "sort-packages": true 47 | }, 48 | "extra": { 49 | "laravel": { 50 | "providers": [ 51 | "Spatie\\LaravelTwitterStreamingApi\\TwitterStreamingApiServiceProvider" 52 | ], 53 | "aliases": { 54 | "TwitterStreamingApi": "Spatie\\LaravelTwitterStreamingApi\\TwitterStreamingApiFacade" 55 | } 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /.github/workflows/run-tests.yml: -------------------------------------------------------------------------------- 1 | name: run-tests 2 | 3 | on: 4 | - push 5 | - pull_request 6 | 7 | jobs: 8 | test: 9 | runs-on: ${{ matrix.os }} 10 | 11 | strategy: 12 | fail-fast: false 13 | matrix: 14 | os: [ubuntu-latest] 15 | php: [8.0, 8.1, 8.2] 16 | laravel: ['9.*', '10.*', '11.*', '12.*'] 17 | dependency-version: [prefer-stable] 18 | include: 19 | - laravel: 10.* 20 | testbench: 8.* 21 | - laravel: 9.* 22 | testbench: 7.* 23 | - laravel: 11.* 24 | testbench: 9.* 25 | - laravel: 12.* 26 | testbench: 10.* 27 | exclude: 28 | - laravel: 10.* 29 | php: 8.0 30 | - laravel: 11.* 31 | php: 8.0 32 | - laravel: 11.* 33 | php: 8.1 34 | - laravel: 12.* 35 | php: 8.0 36 | - laravel: 12.* 37 | php: 8.1 38 | 39 | name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} 40 | 41 | steps: 42 | - name: Checkout code 43 | uses: actions/checkout@v3 44 | 45 | - name: Cache dependencies 46 | uses: actions/cache@v2 47 | with: 48 | path: ~/.composer/cache/files 49 | key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} 50 | 51 | - name: Setup PHP 52 | uses: shivammathur/setup-php@v2 53 | with: 54 | php-version: ${{ matrix.php }} 55 | extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick 56 | coverage: none 57 | 58 | - name: Install dependencies 59 | run: | 60 | composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update 61 | composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest 62 | 63 | - name: Execute tests 64 | run: vendor/bin/phpunit 65 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Easily work with the Twitter Streaming API in a Laravel app 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-twitter-streaming-api.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-twitter-streaming-api) 4 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) 5 | [![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-twitter-streaming-api.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-twitter-streaming-api) 6 | 7 | Twitter provides a streaming API with which you can do interesting things such as listening for tweets that contain 8 | specific strings or actions a user might take (e.g. liking a tweet, following someone,...). This package makes it very 9 | easy to work with the API. 10 | 11 | ```php 12 | TwitterStreamingApi::publicStream() 13 | ->whenHears('#laravel', function(array $tweet) { 14 | echo "{$tweet['user']['screen_name']} tweeted {$tweet['text']}"; 15 | }) 16 | ->startListening(); 17 | ``` 18 | 19 | Here's [an example Laravel application](https://github.com/spatie/laravel-twitter-streaming-api-example-app) with the 20 | package pre-installed. It 21 | contains [an artisan command](https://github.com/spatie/laravel-twitter-streaming-api-example-app/blob/master/app/Console/Commands/ListenForHashTags.php) 22 | to kick off the listening process. 23 | 24 | ## Support us 25 | 26 | [](https://spatie.be/github-ad-click/laravel-twitter-streaming-api) 27 | 28 | We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can 29 | support us by [buying one of our paid products](https://spatie.be/open-source/support-us). 30 | 31 | We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. 32 | You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards 33 | on [our virtual postcard wall](https://spatie.be/open-source/postcards). 34 | 35 | ## Installation 36 | 37 | You can install the package via composer: 38 | 39 | ``` bash 40 | composer require spatie/laravel-twitter-streaming-api 41 | ``` 42 | 43 | The config file must be published with this command: 44 | 45 | ```bash 46 | php artisan vendor:publish --provider="Spatie\LaravelTwitterStreamingApi\TwitterStreamingApiServiceProvider" --tag="config" 47 | ``` 48 | 49 | It will be published in `config/laravel-twitter-streaming-api.php` 50 | 51 | ```php 52 | return [ 53 | 54 | /* 55 | * To work with Twitter's Streaming API you'll need some credentials. 56 | * 57 | * If you don't have credentials yet, head over to https://developers.twitter.com/ 58 | */ 59 | 60 | 'handle' => env('TWITTER_HANDLE'), 61 | 62 | 'api_key' => env('TWITTER_API_KEY'), 63 | 64 | 'api_secret_key' => env('TWITTER_API_SECRET_KEY'), 65 | 66 | 'bearer_token' => env('TWITTER_BEARER_TOKEN'), 67 | ]; 68 | ``` 69 | 70 | ## Getting credentials 71 | 72 | In order to use this package you'll need to get some credentials from Twitter. Head over to 73 | the [Developer Portal on Twitter](https://developers.twitter.com/) to create an application. 74 | 75 | Once you've created your application, click on the `Keys and tokens` tab to retrieve your `bearer_token`, `api_key` 76 | and `api_secret_key`. 77 | 78 | ![Keys and tokens tab on Twitter](docs/tokens.png) 79 | 80 | ## Usage 81 | 82 | Currently, this package works with the public stream and the user stream. Both the `PublicStream` and `UserStream` 83 | classes provide a `startListening` function that kicks of the listening process. Unless you cancel it your PHP process 84 | will execute that function forever. No code after the function will be run. 85 | 86 | In the example below a facade is used. If you don't like facades you can replace them with 87 | 88 | ```php 89 | app(Spatie\LaravelTwitterStreamingApi\TwitterStreamingApi::class) 90 | ``` 91 | 92 | ### The public stream 93 | 94 | The public stream can be used to listen for specific words that are being tweeted. 95 | 96 | The first parameter of `whenHears` must be a string, or an array containing the word or words you want to listen for. The 97 | second parameter should be a callable that will be executed when one of your words is used on Twitter. 98 | 99 | ```php 100 | use TwitterStreamingApi; 101 | 102 | TwitterStreamingApi::publicStream() 103 | ->whenHears('#laravel', function(array $tweet) { 104 | echo "{$tweet['user']['screen_name']} tweeted {$tweet['text']}"; 105 | }) 106 | ->startListening(); 107 | ``` 108 | 109 | ### The user stream 110 | 111 | ```php 112 | use TwitterStreamingApi; 113 | 114 | TwitterStreamingApi::userStream() 115 | ->onEvent(function(array $event) { 116 | if ($event['event'] === 'favorite') { 117 | echo "Our tweet {$event['target_object']['text']} got favorited by {$event['source']['screen_name']}"; 118 | } 119 | }) 120 | ->startListening(); 121 | ``` 122 | 123 | ## Suggestion on how to run in a production environment 124 | 125 | When using this in production you could opt to 126 | create [an artisan command](https://github.com/spatie/laravel-twitter-streaming-api-example-app/blob/8175995/app/Console/Commands/ListenForHashTags.php) 127 | to listen for incoming events from Twitter. You can use [Supervisord](http://supervisord.org/) to make sure that command 128 | is running all the time. 129 | 130 | ## A word to the wise 131 | 132 | These APIs work in realtime, so they could report a lot of activity. If you need to do some heavy work processing that 133 | activity it's best to put that work in a queue to keep your listening process fast. 134 | 135 | ## Changelog 136 | 137 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 138 | 139 | ## Testing 140 | 141 | ``` bash 142 | $ composer test 143 | ``` 144 | 145 | ## Contributing 146 | 147 | Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details. 148 | 149 | ## Security 150 | 151 | If you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker. 152 | 153 | ## Credits 154 | 155 | - [Freek Van der Herten](https://github.com/freekmurze) 156 | - [All Contributors](../../contributors) 157 | 158 | ## License 159 | 160 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 161 | --------------------------------------------------------------------------------