spatie/laravel-twitter-streaming-api
Composer 安装命令:
composer require spatie/laravel-twitter-streaming-api
包简介
Easily work with the Twitter Streaming API in a Laravel app
README 文档
README
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.
TwitterStreamingApi::publicStream() ->whenHears('#laravel', function(array $tweet) { echo "{$tweet['user']['screen_name']} tweeted {$tweet['text']}"; }) ->startListening();
Here's an example Laravel application with the package pre-installed. It contains an artisan command to kick off the listening process.
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.
Installation
You can install the package via composer:
composer require spatie/laravel-twitter-streaming-api
The config file must be published with this command:
php artisan vendor:publish --provider="Spatie\LaravelTwitterStreamingApi\TwitterStreamingApiServiceProvider" --tag="config"
It will be published in config/laravel-twitter-streaming-api.php
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'), ];
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
and api_secret_key.
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.
In the example below a facade is used. If you don't like facades you can replace them with
app(Spatie\LaravelTwitterStreamingApi\TwitterStreamingApi::class)
The public stream
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 executed when one of your words is used on Twitter.
use TwitterStreamingApi; TwitterStreamingApi::publicStream() ->whenHears('#laravel', function(array $tweet) { echo "{$tweet['user']['screen_name']} tweeted {$tweet['text']}"; }) ->startListening();
The user stream
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();
Suggestion on how to run in a production environment
When using this in production you could opt to create an artisan command to listen for incoming events from Twitter. You can use Supervisord to make sure that command is running all the time.
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.
Changelog
Please see CHANGELOG for more information what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING for details.
Security
If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
spatie/laravel-twitter-streaming-api 适用场景与选型建议
spatie/laravel-twitter-streaming-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 46.22k 次下载、GitHub Stars 达 205, 最近一次更新时间为 2017 年 01 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「spatie」 「laravel-twitter-streaming-api」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spatie/laravel-twitter-streaming-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spatie/laravel-twitter-streaming-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spatie/laravel-twitter-streaming-api 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Receive webhooks in Laravel apps
Easily optimize images using PHP
Store your application settings
Add tags and taggable behaviour to your Laravel app
Speed up a Laravel application by caching the entire response additions
Manage events on a Google Calendar
统计信息
- 总下载量: 46.22k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 209
- 点击次数: 16
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-01-12
