get-stream / stream
Stream (https://getstream.io) 的 PHP 客户端
Requires
- php: >=8.0
- firebase/php-jwt: ^v6.4.0
- guzzlehttp/guzzle: ^7.5.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.14.0
- phan/phan: ^5.4.0
- phpunit/phpunit: ^9.6.3
- dev-main
- v7.1.0
- 7.0.1
- v7.0.0
- 6.0.0
- 5.2.0
- 5.1.1
- 5.1.0
- 5.0.2
- v5.0.1
- 5.0.0
- 4.1.1
- 4.1.0
- 4.0.1
- 4.0.0
- 3.0.2
- 3.0.1
- 3.0.0
- 2.9.1
- 2.9.0
- 2.8.0
- 2.7.0
- 2.6.0
- 2.5.2
- 2.5.1
- 2.5.0
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.0
- 2.2.9
- 2.2.8
- 2.2.7
- 2.2.6
- 2.2.5
- 2.2.4
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.3.8
- 1.3.7
- 1.3.6
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-release-v7.1.0
- dev-soft-deletes-reactions
- dev-release-7.0.1
- dev-release-v7.0.0
- dev-AC-46/partial-update
- dev-AC-37/get-activities
This package is auto-updated.
Last update: 2024-08-30 01:22:36 UTC
README
Stream Feeds 的官方 PHP API 客户端,用于构建可扩展的新闻源和活动流。
探索文档
Laravel Feeds 库 · 报告错误 · 请求功能
📝 关于 Stream
💡 注意:这是一个 Feeds 产品的库。Chat SDK 可以在这里找到:这里。
您可以在我们的 入门 页面上注册 Stream 账户。
您可以使用此库在服务器端访问 Feeds API 端点。
对于客户端集成(Web 和移动),请查看 JavaScript、iOS 和 Android SDK 库(文档)。
💡 注意:还有一个更高层次的 Laravel 集成,它可以与 Eloquent ORM 集成。
⚙️ 安装
$ composer require get-stream/stream
Composer 会自动安装我们的最新版本。
PHP 兼容性
当前版本需要 PHP 8.0
或更高版本,并依赖于 Guzzle 版本 ^7.5.0
。
如果您需要使用较旧的 PHP 或 Guzzle 的早期版本,您可以获取此包的早期版本,例如
$ composer require get-stream/stream:"~5.0.0"
有关如何构建和针对不同 PHP 版本进行测试的详细信息,请参阅 操作。
📚 文档
本包的完整文档可在 https://getstream.io/docs/php/ 找到。
✨ 入门
首先,在此处注册 免费账户并获取您的 API 密钥和密钥。
初始化客户端和 Feed 对象
// Instantiate a new client, find your API keys in the dashboard. $client = new GetStream\Stream\Client('YOUR_API_KEY', 'YOUR_API_SECRET'); // Instantiate a feed object $userFeed = $client->feed('user', '1'); // If you want, you can set a custom http handler $handler = new \GuzzleHttp\HandlerStack(); $stack->push(Middleware::mapRequest(function (RequestInterface $r) { echo 'Sending request to Stream Feeds API: ' . $r->getUri() . PHP_EOL; return $r; })); $client->setCustomHttpHandler($handler);
默认情况下,客户端将针对 stream-io-api.com/api
上的 GetStream REST API。如果您想更改此设置,可以设置 STREAM_BASE_URL
环境变量。
Feed 中的活动
// Create a new activity $data = [ 'actor' => '1', 'verb' => 'like', 'object' => '3', 'foreign_id' => 'like:42', ]; $response = $userFeed->addActivity($data); // The response will include Stream's internal ID: // {"id": "e561...", "actor": "1", ...} // Get the latest activities for this user's personal feed, based on who they are following. $response = $userFeed->getActivities(); // Get activities directly by their ID or combination of foreign ID and time. $response = $client->getActivitiesById(['74b9e88a-a684-4197-b30c-f5e568ef9ae2', '965f7ba5-8f1d-4fd1-a9ee-22d1a2832645']); $response = $client->getActivitiesByForeignId(['fid:123', '2006-01-02T15:04:05.000000000'], ['fid:456', '2006-01-02T16:05:06.000000000']); // The response will be the json decoded API response. // {"duration": 45ms, "next": "/api/v1.0/feed/...", "results": [...]} // Remove an activity by its ID $userFeed->removeActivity('e561de8f-00f1-11e4-b400-0cc47a024be0'); // To remove activities by their foreign_id, set the "foreign id" flag to true. $userFeed->removeActivity('like:42', true);
Feed 的关注/粉丝关系
// When user 1 starts following user 37's activities $userFeed->follow('user', '37'); // When user 1 stops following user 37's activities $userFeed->unfollow('user', '37'); // Retrieve followers of a feed $userFeed->followers(); // Retrieve feeds followed by $userFeed $userFeed->following();
高级活动操作
// Create a bit more complex activity with custom fields $data = [ 'actor' => 1, 'verb' => 'run', 'object' => 1, 'foreign_id' => 'run:42', // Custom fields: 'course' => [ 'name'=> 'Golden Gate park', 'distance'=> 10, ], 'participants' => ['Thierry', 'Tommaso'], 'started_at' => new DateTime('now', new DateTimeZone('Pacific/Nauru'), ]; // Add an activity and push it to other feeds too using the `to` field $data = [ 'actor' => '1', 'verb' => 'like', 'object' => '3', 'to' => [ 'user:44', 'user:45', ], ]; $userFeed->addActivity($data); // Batch adding activities $activities = [ ['actor' => '1', 'verb' => 'tweet', 'object' => '1'], ['actor' => '2', 'verb' => 'like', 'object' => '3'], ]; $userFeed->addActivities($activities);
高级批量操作
// Batch operations (batch activity add, batch follow) $batcher = $client->batcher(); // Add one activity to many feeds $activity = ['actor' => '1', 'verb' => 'tweet', 'object' => '1']; $feeds = ['user:1', 'user:2']; $batcher->addToMany($activity, $feeds); // Create many follow relations $follows = [ ['source' => 'user:b1', 'target' => 'user:b2'], ['source' => 'user:b1', 'target' => 'user:b3'], ]; $batcher->followMany($follows);
生成客户端使用的令牌
// Generating a user token $userToken = $client->createUserSessionToken("the-user-id");
速率限制
如果您的应用程序触发了速率限制,将抛出 StreamFeedException。您可以通过捕获异常并调用以下方法来获取更多信息
try { $client->updateActivity($activity); }catch(StreamFeedException $e){ $limit = $e->getRateLimitLimit(); $remaining = $e->getRateLimitRemaining(); $reset = $e->getRateLimitReset(); // a unix timestamp }
反应
反应模块有以下方法。
- add(string $kind, string $activityId, string $userId, array $data=null, array $targetFeeds=null)
- addChild(string $kind, string $parentId, string $userId, array $data=null, array $targetFeeds=null)
- delete(string $reactionId)
- filter(string $lookupField, string $lookupValue, string $kind=null, array $params=null)
- get(string $reactionId)
- update(string $reactionId, array $data=null, array $targetFeeds=null)
还可以查看关于 reactions 端点 的文档。
$reaction = $client->reactions()->add('like', $activity_id, 'bob'); $bob_likes = $client->reactions()->filter('user_id', 'bob', 'like'); $client->reactions()->delete($reaction['id']);
用户
用户模块有以下方法。
- add(string $userId, array $data=null, bool $getOrCreate=false)
- createReference(string $userId)
- delete(string $userId)
- get(string $userId)
- update(string $userId, array $data=null)
还可以查看关于 users 端点 的文档。
$user = $client->users()->add('42'); $user = $client->users()->update('42', array('name' => 'Arthur Dent'); $client->users()->delete('42');
再次,我们提供完整的文档,包括所有选项和方法,可在 https://getstream.io/docs/php/ 找到。
☯️ 框架集成
Laravel
存在一个与 Laravel 的更高层次集成,称为 get-stream/stream-laravel
。stream-laravel
集成可以帮助您将数据同步到 Stream 中,并与 Laravel 的 Eloquent ORM 进行钩接。
✍️ 贡献
项目遵循 BSD 3-Clause 许可协议。
我们欢迎提交代码更改,以改进此库或修复问题,请在提交 Github 上的 Pull Request 之前确保遵循所有最佳实践,并在适用的情况下添加测试。我们非常愿意将您的代码合并到官方仓库中。请首先签署我们的 贡献者许可协议 (CLA)。有关更多详细信息,请参阅我们的 许可文件。
🧑💻 我们正在招聘!
我们最近完成了一轮 3800万美元的 B 轮融资,并且正在积极扩张。我们的 API 被超过十亿最终用户使用,您将有机会在世界上最优秀的工程师团队中为产品产生巨大影响。
请查看我们的当前空缺职位,并通过 Stream 网站 申请。