pnz / mattermost-client
PHP Mattermost API 客户端
Requires
- php: ^7.0
- php-http/client-common: ^1.1
- php-http/discovery: ^1.0
- php-http/httplug: ^1.0
- php-http/multipart-stream-builder: ^1.0
Requires (Dev)
- guzzlehttp/psr7: ^1.3
- php-http/curl-client: ^1.6
- php-http/message: ^1.0
- php-http/message-factory: ^1.0
- phpunit/phpunit: ^6.0
This package is auto-updated.
Last update: 2024-09-20 15:51:11 UTC
README
这是一个 PHP 库,提供了对 Mattermost REST API v4 的客户端支持。
此库允许开发人员通过一组特定的模型使用 Mattermost 数据。与团队、频道、用户、帖子等相关数据将被转换为模型对象,以便轻松使用和处理。Mattermost API 的错误响应也被处理为特定的领域异常。
您的 IDE 将能够自动完成和提示模型属性,从而降低使用 Mattermost API 而不阅读大量 API 文档的门槛。
遵循 Friends of Api 的例子,此库允许开发人员使用和扩展用于解析 API 响应的 Hydrators
。这些负责将 API 返回的 JSON 转换为模型(默认)或其他响应类型。
包括模型 builders
以便于通过 API 创建/更新模型。
有关更改列表,请参阅 变更日志。支持的 API 端点列表可在以下 Google 电子表格 文档中找到。
安装
TL;DR
composer require php-http/curl-client nyholm/psr7 php-http/message pnz/mattermost-client
此库不依赖于 Guzzle 或任何其他发送 HTTP 请求的库。我们使用惊人的 HTTPlug 来实现解耦。我们希望您选择用于发送 HTTP 请求的库。咨询支持 php-http/client-implementation 的包列表,找到要使用的客户端。有关虚拟包的更多信息,请参阅 HTTPlug。示例
composer require php-http/curl-client
您还需要安装一个 PSR-7 实现,以及一个用于创建 PSR-7 消息的工厂(PSR-17 发布时)。您可以使用 Nyholm PSR-7 实现 和 php-http 的工厂。
composer require nyholm/psr7 php-http/message
现在您可以通过运行以下命令安装此库:
composer require pnz/mattermost-client
用法示例
<?php require_once 'vendor/autoload.php'; $endpoint = 'http://mattermostserver.ext/api/v4'; $username = 'username'; $password = 'password'; $configurator = (new HttpClientConfigurator()) ->setEndpoint($endpoint) ->setCredentials($username, $password); $apiClient = ApiClient::configure($configurator); try { // Get the currently logged-in User; the "me" ID is a special one, as documented on Mattermost.org APIs. $user = $apiClient->users()->getUserById('me'); var_dump($user->getUsername());
处理 Mattermost 实体
特定的模型构建器可用于帮助创建 Mattermost 实体。
例如,要创建团队,请使用 TeamBuilder()
实例,添加所需的字段,并调用 build()
以获取调用 createTeam()
API 所需的数据。
创建团队
use Pnz\MattermostClient\Model\Team; $teamData = (new Team\TeamBuilder()) ->setDisplayName('Team 01') ->setName('team-01') ->setType(Team\Team::TEAM_INVITE_ONLY) ->build(); $team = $apiClient->teams()->createTeam($teamData);
模型构建器还可以用于生成更新或修补 Mattermost 实体所需的数据。
修补帖子
<?php use Pnz\MattermostClient\Model\Post; $post = $apiClient->posts()->getPost('zhcapisftibyjnf54gixg3hdew'); $postData = (new Post\PostBuilder()) ->setMessage('I can `format` the _text_ of a *message*, including [links](www.mattermost.com)') ->setIsPinned(true) ->build(Post\PostBuilder::BUILD_FOR_PATCH); $post = $apiClient->posts()->patchPost($post->getId(), $postData);