mpyw / cowitter
此包已被废弃,不再维护。没有建议的替代包。
与 mpyw/co Generator 基础流程兼容的异步 Twitter 客户端。
v1.0.5
2019-05-16 13:42 UTC
Requires
- php: >=5.6.0
- ext-hash: *
- ext-json: *
- ext-openssl: *
- lib-curl: >=7.20.0
- mpyw/co: ^1.5
Requires (Dev)
- php: >=7.0.0
- codeception/aspect-mock: ^2.2
- codeception/codeception: ^2.2
- codeception/specify: ^0.4.6
- mpyw/privator: ^2.0
- satooshi/php-coveralls: ^1.0
README
与 mpyw/co Generator 基础流程兼容的异步 Twitter 客户端。
PHP | ❓ | 功能限制 |
---|---|---|
7.0~ | 😄 | 完全支持 |
5.5~5.6 | 😧 | 生成器并不那么酷 |
~5.4 | 💥 | 不兼容 |
安装
composer require mpyw/cowitter:^1.0
教程
快速示例
准备要求
require __DIR__ . '/vendor/autoload.php'; use mpyw\Co\Co; use mpyw\Co\CURLException; use mpyw\Cowitter\Client; use mpyw\Cowitter\HttpException;
创建客户端
$client = new Client(['CK', 'CS', 'AT', 'ATS']);
同步请求
// 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) ); });
异步请求
// 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"; });
处理异常
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()) }
或
try { // do stuff here $client->get(...); $client->post(...); } catch (\RuntimeException $e) { // Something failed. $message = $e->getMessage(); }
避免旧版 libcurl 导致的 SSL 错误
如果您遇到 SSL 证书问题
错误...
- 从官方 libcurl 网站下载最新的
cacert.pem
。
https://curl.haxx.se/docs/caextract.html - 请选择以下任何一种解决方案。
2-A: 全局配置
在您的 php.ini
中指定路径为 curl.cainfo
。
curl.cainfo="C:\foo\bar\baz\cacert.pem"
不要忘记重新启动 Apache。
2-B: 本地配置
指定路径为 CURLOPT_CAINFO
。建议使用魔术常量 __DIR__
。
$client = new Client(['CK', 'CS', 'AT', 'ATS'], [CURLOPT_CAINFO => __DIR__ . '/cacert.pem']);
或
$client = new Client(['CK', 'CS', 'AT', 'ATS']); $client = $client->withOptions([CURLOPT_CAINFO => __DIR__ . '/cacert.pem']);
详细信息
读取接口。
待办事项
- 文档
- 改进代码