get-stream/stream-chat

Stream Chat (https://getstream.io/chat/) 的 PHP 客户端

3.5.0 2024-09-04 13:31 UTC

README

build Latest Stable Version

Stream Chat 的官方 PHP API 客户端,用于构建聊天应用程序。
探索文档

报告错误 · 请求功能

📝 关于 Stream

您可以在我们的 入门 页面上注册 Stream 账户。

您可以使用这个库在服务器端访问聊天 API 端点。

对于客户端集成(Web 和移动),请查看 JavaScript、iOS 和 Android SDK 库(文档)。

⚙️ 安装

$ composer require get-stream/stream-chat

✨ 入门

require_once "./vendor/autoload.php";

实例化一个新的客户端,在仪表板中找到您的 API 密钥。

$client = new GetStream\StreamChat\Client("<api-key>", "<api-secret>");

生成客户端使用令牌

$token = $client->createToken("bob-1");

// with an expiration time
$expiration = (new DateTime())->getTimestamp() + 3600;
$token = $client->createToken("bob-1", $expiration);

更新/创建用户

$bob = [
    'id' => 'bob-1',
    'role' => 'admin',
    'name' => 'Robert Tables',
];

$bob = $client->upsertUser($bob);

// Batch update is also supported
$jane = ['id' => 'jane', 'role' => 'admin'];
$june = ['id' => 'june', 'role' => 'user'];
$tom = ['id' => 'tom', 'role' => 'guest'];
$users = $client->upsertUsers([$jane, $june, $tom]);

频道类型

$channelConf = [
    'name' => 'livechat',
    'automod' => 'disabled',
    'commands' => ['ban'],
    'mutes' => true
];

$channelType = $client->createChannelType($channelConf);

$allChannelTypes =  $client->listChannelTypes();

频道和消息

$channel = $client->Channel("messaging", "bob-and-jane");
$state = $channel->create("bob-1", ['bob-1', 'jane']);
$channel->addMembers(['mike', 'joe']);

消息传递

$msg_bob = $channel->sendMessage(["text" => "Hi June!"], 'bob-1');

// Reply to a message
$reply_bob = $channel->sendMessage(["text" => "Long time no see!"], 'bob-1', $msg_bob['message']['id']);

反应

$channel->sendReaction($reply_bob['message']['id'], ['type' => 'like'], 'june');

管理

$channel->addModerators(['june']);
$channel->demoteModerators(['june']);

$channel->banUser('june', ["reason" => "Being a big jerk", "timeout" => 5, "user_id" => 'bob-1']);
$channel->unbanUser('june', ["user_id" => 'bob-1']);

设备

$device_id = "iOS_Device_Token_123";
$client->addDevice($device_id, "apn", "june");
$devices = $client->getDevices('june');

$client->deleteDevice($device_id, 'june');

🙋‍♀️ 常见问题

  • Q: 后端接受哪些日期格式?
  • A: 我们接受 RFC3339 格式。因此,您可以使用原始字符串作为日期,或者为您的 DateTime 对象实现序列化器。
class MyDateTime extends \DateTime implements \JsonSerializable
{
    public function jsonSerialize()
    {
        // Note: this returns ISO8601
        // but it's compatible with 3339
       return $this->format("c");
    }
}

$createdAt = new MyDateTime();

$client->search( 
	['type' => "messaging"], 
	['created_at' => ['$lte' => $createdAt]], 
	['limit' => 10]
);

✍️ 贡献

我们欢迎改进此库或解决问题,请在提交 Github 上的 Pull Request 之前确保遵循所有最佳实践,并在适用的情况下添加测试。我们非常乐意将您的代码合并到官方仓库中。请首先签署我们的 贡献者许可协议 (CLA)。有关更多详细信息,请参阅我们的 许可文件

前往 CONTRIBUTING.md 了解一些开发技巧。

🧑‍💻 我们正在招聘!

我们最近完成了一轮 $38 million Series B funding round,并且我们还在积极增长。我们的 API 被超过十亿最终用户使用,您将有机会在世界上最强大的工程师团队中对产品产生巨大影响。

查看我们的当前空缺职位,并通过 Stream 网站 申请。