stevenmaguire / trello-php
Trello API 的 PHP 封装器
Requires
- php: ^8.0
- ext-json: *
- guzzlehttp/guzzle: ^7.0
- league/oauth1-client: ^1.5
Requires (Dev)
- mockery/mockery: ~1.5
- phpunit/phpunit: ^8.0
- squizlabs/php_codesniffer: ~3.5
README
一个用于消费 Trello API 的 PHP 客户端。
安装
通过 Composer
$ composer require stevenmaguire/trello-php
使用
本指南将帮助您配置客户端,验证您的用户并获取访问令牌,以及代表他们访问 API。
完整的客户端文档可在 API 指南 中找到。
在继续之前,请确保您已安全地存储了 Trello API 密钥。有关此信息,请参阅此实用指南。
该项目包含一个 基本示例。您可以通过运行此示例来测试应用程序详情。打开示例文件并包含您的 key
和 secret
,运行 php -S localhost:8000 -t example
,然后在您喜欢的浏览器中访问 http://localhost:8000
。
配置客户端
Trello 客户端需要一些配置设置才能成功运行。
创建客户端时设置配置
$client = new Stevenmaguire\Services\Trello\Client(array( 'callbackUrl' => 'http://your.domain/oauth-callback-url', 'domain' => 'https://trello.com', 'expiration' => '3days', 'key' => 'my-application-key', 'name' => 'My sweet trello enabled app', 'scope' => 'read,write', 'secret' => 'my-application-secret', 'token' => 'abcdefghijklmnopqrstuvwxyz', 'version' => '1', 'proxy' => 'tcp://localhost:8125', ));
创建客户端后设置多个配置
$client = new Stevenmaguire\Services\Trello\Client(array( 'key' => 'my-application-key', 'name' => 'My sweet trello enabled app', )); $config = array( 'callbackUrl' => 'http://your.domain/oauth-callback-url', 'expiration' => '3days', 'scope' => 'read,write', ); $client->addConfig($config);
创建客户端后设置单个配置
$client = new Stevenmaguire\Services\Trello\Client(array( 'key' => 'my-application-key', 'name' => 'My sweet trello enabled app', )); $client->addConfig('token', 'abcdefghijklmnopqrstuvwxyz');
验证您的用户并存储访问令牌
Trello 客户端可以帮助您引导用户完成 OAuth 授权流程,并为您的应用程序提供访问令牌凭据。
此包使用 The League 的 OAuth1 Trello 客户端 来提供此帮助。
创建基本客户端
$client = new Stevenmaguire\Services\Trello\Client(array( 'key' => 'my-application-key', 'secret' => 'my-application-secret', ));
添加您的应用程序所需的 OAuth 设置
$config = array( 'name' => 'My sweet trello enabled app', 'callbackUrl' => 'http://your.domain/oauth-callback-url', 'expiration' => '3days', 'scope' => 'read,write', ); $client->addConfig($config);
获取授权 URL,然后重定向用户
$authorizationUrl = $client->getAuthorizationUrl(); header('Location: ' . $authorizationUrl);
交换授权令牌和验证器以获取访问令牌
用户批准或拒绝您的应用程序访问后,将重定向到您提供的回调 URL。如果用户批准访问,URL 将包含查询字符串参数中的 oauth_token
和 oauth_verifier
。
$token = $_GET['oauth_token']; $verifier = $_GET['oauth_verifier']; $credentials = $client->getAccessToken($token, $verifier); $accessToken = $credentials->getIdentifier();
如果成功,则 $credentials
将是 TokenCredentials 的实例。您可以在其中存储标识符并用于代表该用户验证请求。
使用访问令牌进行请求
$client->addConfig('token', $accessToken); $user = $client->getCurrentUser();
使用访问令牌访问 API
获取属于您用户的所有实体的清单
$client = new Stevenmaguire\Services\Trello\Client(array( 'key' => 'my-application-key', 'token' => 'your-users-access-token', )); $boards = $client->getCurrentUserBoards(); $cards = $client->getCurrentUserCards(); $organizations = $client->getCurrentUserOrganizations();
大多数在 API 指南 中可用的方法都需要实体 ID 来执行业务。
处理异常
在处理使用客户端向 Trello 发送请求时出现的异常时,将抛出 Stevenmaguire\Services\Trello\Exceptions\Exception
。此异常将包括来自底层 Http 请求/响应问题的信息,包括 Trello 的响应体。
try { $board = $client->getBoard($boardId); } catch (Stevenmaguire\Services\Trello\Exceptions\Exception $e) { $code = $e->getCode(); // Http status code from response $reason = $e->getMessage(); // Http status reason phrase $error = $e->getPrevious(); // GuzzleHttp\Exception\RequestException from http client $body = $e->getResponseBody(); // stdClass response body from http client }
测试
$ ./vendor/bin/phpunit
贡献
有关详细信息,请参阅 CONTRIBUTING。您可以看到当前的 PROGRESS。
致谢
许可协议
麻省理工学院许可证(MIT)。请参阅许可文件获取更多信息。