{"id":13608503,"url":"https://github.com/spatie/laravel-twitter-streaming-api","last_synced_at":"2025-05-15T10:07:04.587Z","repository":{"id":50256862,"uuid":"78793113","full_name":"spatie/laravel-twitter-streaming-api","owner":"spatie","description":"Easily work with the Twitter Streaming API in a Laravel app","archived":false,"fork":false,"pushed_at":"2025-02-14T12:42:29.000Z","size":119,"stargazers_count":203,"open_issues_count":0,"forks_count":22,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-05-11T17:38:43.269Z","etag":null,"topics":["api","laravel","php","streaming","tweets","twitter"],"latest_commit_sha":null,"homepage":"https://freek.dev/671-easily-work-with-the-twitter-streaming-api-in-php","language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"spatie"}},"created_at":"2017-01-12T22:35:36.000Z","updated_at":"2025-04-04T04:41:02.000Z","dependencies_parsed_at":"2024-06-18T14:08:07.007Z","dependency_job_id":"8523a07c-8334-4986-97d2-4379a91e2a76","html_url":"https://github.com/spatie/laravel-twitter-streaming-api","commit_stats":{"total_commits":78,"total_committers":17,"mean_commits":4.588235294117647,"dds":0.4871794871794872,"last_synced_commit":"5cc9b20c838a26e6fa885911a9807b82ca9bd8e5"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-twitter-streaming-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-twitter-streaming-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-twitter-streaming-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-twitter-streaming-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/laravel-twitter-streaming-api/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254319720,"owners_count":22051073,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","laravel","php","streaming","tweets","twitter"],"created_at":"2024-08-01T19:01:27.779Z","updated_at":"2025-05-15T10:06:59.573Z","avatar_url":"https://github.com/spatie.png","language":"PHP","funding_links":["https://github.com/sponsors/spatie"],"categories":["PHP"],"sub_categories":[],"readme":"# Easily work with the Twitter Streaming API in a Laravel app\n\n[![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)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)\n[![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)\n\nTwitter provides a streaming API with which you can do interesting things such as listening for tweets that contain\nspecific strings or actions a user might take (e.g. liking a tweet, following someone,...). This package makes it very\neasy to work with the API.\n\n```php\nTwitterStreamingApi::publicStream()\n-\u003ewhenHears('#laravel', function(array $tweet) {\n    echo \"{$tweet['user']['screen_name']} tweeted {$tweet['text']}\";\n})\n-\u003estartListening();\n```\n\nHere's [an example Laravel application](https://github.com/spatie/laravel-twitter-streaming-api-example-app) with the\npackage pre-installed. It\ncontains [an artisan command](https://github.com/spatie/laravel-twitter-streaming-api-example-app/blob/master/app/Console/Commands/ListenForHashTags.php)\nto kick off the listening process.\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/laravel-twitter-streaming-api.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/laravel-twitter-streaming-api)\n\nWe invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can\nsupport us by [buying one of our paid products](https://spatie.be/open-source/support-us).\n\nWe highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.\nYou'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards\non [our virtual postcard wall](https://spatie.be/open-source/postcards).\n\n## Installation\n\nYou can install the package via composer:\n\n``` bash\ncomposer require spatie/laravel-twitter-streaming-api\n```\n\nThe config file must be published with this command:\n\n```bash\nphp artisan vendor:publish --provider=\"Spatie\\LaravelTwitterStreamingApi\\TwitterStreamingApiServiceProvider\" --tag=\"config\"\n```\n\nIt will be published in `config/laravel-twitter-streaming-api.php`\n\n```php\nreturn [\n\n    /*\n     * To work with Twitter's Streaming API you'll need some credentials.\n     *\n     * If you don't have credentials yet, head over to https://developers.twitter.com/\n     */\n\n    'handle' =\u003e env('TWITTER_HANDLE'),\n\n    'api_key' =\u003e env('TWITTER_API_KEY'),\n\n    'api_secret_key' =\u003e env('TWITTER_API_SECRET_KEY'),\n\n    'bearer_token' =\u003e env('TWITTER_BEARER_TOKEN'),\n];\n```\n\n## Getting credentials\n\nIn order to use this package you'll need to get some credentials from Twitter. Head over to\nthe [Developer Portal on Twitter](https://developers.twitter.com/) to create an application.\n\nOnce you've created your application, click on the `Keys and tokens` tab to retrieve your `bearer_token`, `api_key`\nand `api_secret_key`.\n\n![Keys and tokens tab on Twitter](docs/tokens.png)\n\n## Usage\n\nCurrently, this package works with the public stream and the user stream. Both the `PublicStream` and `UserStream`\nclasses provide a `startListening` function that kicks of the listening process. Unless you cancel it your PHP process\nwill execute that function forever. No code after the function will be run.\n\nIn the example below a facade is used. If you don't like facades you can replace them with\n\n```php\napp(Spatie\\LaravelTwitterStreamingApi\\TwitterStreamingApi::class)\n```\n\n### The public stream\n\nThe public stream can be used to listen for specific words that are being tweeted.\n\nThe first parameter of `whenHears` must be a string, or an array containing the word or words you want to listen for. The\nsecond parameter should be a callable that will be executed when one of your words is used on Twitter.\n\n```php\nuse TwitterStreamingApi;\n\nTwitterStreamingApi::publicStream()\n-\u003ewhenHears('#laravel', function(array $tweet) {\n    echo \"{$tweet['user']['screen_name']} tweeted {$tweet['text']}\";\n})\n-\u003estartListening();\n```\n\n### The user stream\n\n```php\nuse TwitterStreamingApi;\n\nTwitterStreamingApi::userStream()\n-\u003eonEvent(function(array $event) {\n    if ($event['event'] === 'favorite') {\n        echo \"Our tweet {$event['target_object']['text']} got favorited by {$event['source']['screen_name']}\";\n    }\n})\n-\u003estartListening();\n```\n\n## Suggestion on how to run in a production environment\n\nWhen using this in production you could opt to\ncreate [an artisan command](https://github.com/spatie/laravel-twitter-streaming-api-example-app/blob/8175995/app/Console/Commands/ListenForHashTags.php)\nto listen for incoming events from Twitter. You can use [Supervisord](http://supervisord.org/) to make sure that command\nis running all the time.\n\n## A word to the wise\n\nThese APIs work in realtime, so they could report a lot of activity. If you need to do some heavy work processing that\nactivity it's best to put that work in a queue to keep your listening process fast.\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## Testing\n\n``` bash\n$ composer test\n```\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.\n\n## Security\n\nIf you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker.\n\n## Credits\n\n- [Freek Van der Herten](https://github.com/freekmurze)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Flaravel-twitter-streaming-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Flaravel-twitter-streaming-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Flaravel-twitter-streaming-api/lists"}