mpyw/cowitter
最新稳定版本:v1.0.5
Composer 安装命令:
composer require mpyw/cowitter
包简介
Asynchronous Twitter client compatible with mpyw/co Generator-based flows.
README 文档
README
Asynchronous Twitter client compatible with mpyw/co Generator-based flows.
| PHP | ❓ | Feature Restriction |
|---|---|---|
| 7.0~ | 😄 | Full Support |
| 5.5~5.6 | 😧 | Generator is not so cool |
| ~5.4 | 💥 | Incompatible |
Installing
composer require mpyw/cowitter:^1.0
Tutorial
- Preparation
- Example: Application for your own personal use
- Example: Sign in with Twitter
- Example: Commandline streaming readers
- Example: Account activity webhook setup and usage
Quick examples
Prepare requirements
require __DIR__ . '/vendor/autoload.php'; use mpyw\Co\Co; use mpyw\Co\CURLException; use mpyw\Cowitter\Client; use mpyw\Cowitter\HttpException;
Create client
$client = new Client(['CK', 'CS', 'AT', 'ATS']);
Synchronous requests
// Search tweets $statuses = $client->get('search/tweets', ['q' => 'cowitter'])->statuses; var_dump($statuses);
// Update tweet $client->post('statuses/update', ['status' => 'Cowitter is the best twitter library for PHP!']);
// Send direct message with new API $params = [ 'event' => [ 'type' => 'message_create', 'message_create' => [ 'target' => [ 'recipient_id' => 'RECIPIENT_USER_ID' ], 'message_data' => [ 'text' => 'Hello World!', ] ] ] ]; // Post as json $client->postJson('direct_messages/events/new ', $params);
// Update tweet with multiple images $ids = [ $client->postMultipart('media/upload', ['media' => new \CURLFile('photo01.png')])->media_id_string, $client->postMultipart('media/upload', ['media' => new \CURLFile('photo02.jpg')])->media_id_string, ]; $client->post('statuses/update', [ 'status' => 'My photos', 'media_ids' => implode(',', $ids), ]);
// Listen user streaming $client->streaming('user', function ($status) { if (!isset($status->text)) return; printf("%s(@%s) - %s\n", $status->user->name, $status->user->screen_name, htmlspecialchars_decode($status->text, ENT_NOQUOTES) ); });
Asynchronous requests
// Search tweets Co::wait(function () use ($client) { $statuses = (yield $client->getAsync('search/tweets', ['q' => 'cowitter']))->statuses; var_dump($statuses); });
// Rapidly update tweets for 10 times $tasks = []; for ($i = 0; $i < 20; ++$i) { $tasks[] = $client->postAsync('statuses/update', [ 'status' => str_repeat('!', $i + 1), ]); } Co::wait($tasks);
// Rapidly update tweet with multiple images Co::wait(function () use ($client) { $info = yield [ $client->postMultipartAsync('media/upload', ['media' => new \CURLFile('photo01.png')]), $client->postMultipartAsync('media/upload', ['media' => new \CURLFile('photo02.png')]), ]; yield $client->postAsync('statuses/update', [ 'status' => 'My photos', 'media_ids' => implode(',', array_column($info, 'media_id_string')), ]); });
// Listen filtered streaming to favorite/retweet at once each tweet Co::wait($client->streamingAsync('statuses/filter', function ($status) use ($client) { if (!isset($status->text)) return; printf("%s(@s) - %s\n", $status->user->name, $status->user->screen_name, htmlspecialchars_decode($status->text, ENT_NOQUOTES) ); yield Co::SAFE => [ // ignore errors $client->postAsync('favorites/create', ['id' => $status->id_str]), $client->postAsync("statuses/retweet/{$status->id_str}"), ]; }, ['track' => 'PHP']));
// Rapidly update with MP4 video Co::wait(function () use ($client) { $file = new \SplFileObject('video.mp4', 'rb'); $on_uploading = function ($percent) { echo "Uploading ... ({$percent}%)\n"; }; $on_processing = function ($percent) { echo "Processing ... ({$percent}%)\n"; }; yield $client->postAsync('statuses/update', [ 'status' => 'My video', 'media_ids' => (yield $client->uploadVideoAsync($file, 'tweet_video', $on_uploading, $on_processing))->media_id_string, ]); echo "Done\n"; });
Handle exceptions
try { // do stuff here $client->get(...); $client->post(...); } catch (HttpException $e) { // cURL communication successful but something went wrong with Twitter APIs. $message = $e->getMessage(); // Message $code = $e->getCode(); // Error code (-1 if not available) $status = $e->getStatusCode(); // HTTP status code } catch (CURLException $e) { // cURL communication failed. $message = $e->getMessage(); // Message (equivalent to curl_error()) $code = $e->getCode(); // Error code (equivalent to curl_errno()) }
or
try { // do stuff here $client->get(...); $client->post(...); } catch (\RuntimeException $e) { // Something failed. $message = $e->getMessage(); }
Avoiding SSL errors due to the old libcurl version
If you encountered SSL certificate problem error...
- Download the latest
cacert.pemfrom official libcurl site.
https://curl.haxx.se/docs/caextract.html - Please choose either of the following solutions.
2-A: Configure globally
Specify the path as curl.cainfo in your php.ini.
curl.cainfo="C:\foo\bar\baz\cacert.pem"
DO NOT forget restarting Apache.
2-B: Configure locally
Specify the path as CURLOPT_CAINFO. Using the magic constant __DIR__ is recommended.
$client = new Client(['CK', 'CS', 'AT', 'ATS'], [CURLOPT_CAINFO => __DIR__ . '/cacert.pem']);
or
$client = new Client(['CK', 'CS', 'AT', 'ATS']); $client = $client->withOptions([CURLOPT_CAINFO => __DIR__ . '/cacert.pem']);
Details
Read interfaces.
Todos
- Documentation
- Improving codes
mpyw/cowitter 适用场景与选型建议
mpyw/cowitter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18.43k 次下载、GitHub Stars 达 115, 最近一次更新时间为 2016 年 08 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「image」 「api」 「oauth」 「upload」 「twitter」 「video」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mpyw/cowitter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mpyw/cowitter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mpyw/cowitter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
WeChat OAuth SDK
Ory-Hydra OAuth 2.0 Client Provider for The PHP League OAuth2-Client
Library for ORCID web services
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
统计信息
- 总下载量: 18.43k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 116
- 点击次数: 22
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-08-13