phpexperts / rest-speaker
一个快速且简单的GuzzleHTTP扩展,用于轻松处理RESTful API。
Requires
- php: >=7.2
- ext-curl: *
- ext-json: *
- guzzlehttp/guzzle: 6.*|7.*
Requires (Dev)
- monolog/monolog: ^2.8
- octoper/cuzzle: ^3.1
- phpexperts/dockerize: *
- phpexperts/laravel-env-polyfill: *
- phpstan/phpstan: *
- phpunit/phpunit: 8.*|9.*
- symfony/var-dumper: *
- vlucas/phpdotenv: ^5.4
Suggests
README
RESTSpeaker是PHP Experts, Inc.的项目,旨在简化访问API。
此库的Speaker类利用Guzzle HTTP客户端通过组合架构模式。
它进一步扩展了基本Guzzle,以便自动解码JSON响应,并更容易使用。
安装
通过Composer
composer require phpexperts/rest-speaker
变更日志
版本 2.6.0
- [2024-03-29 20:03:40 CDT] 如果不是JSON,则返回原始数据。
- [2024-03-29 20:05:27 CDT] 增加了PHP 8.3支持。
版本 2.5.0
- [2023-01-30 10:22:26 CDT] 移除了覆盖RESTAuth方法的需要。
- [2023-01-30 10:21:58 CDT] [m] 升级到phpunit v9.5。
- [2023-01-30 09:57:09 CDT] 添加了NoAuth类。
请参阅变更日志获取有关最近更改的更多信息。
用法
// Instantiation: // NOTE: Guzzle *requires* baseURIs to end with "/". $baseURI = 'https://api.myservice.dev/'; // Either use an .env file or configure ussing the appropriate setters. $restAuth = new RESTAuth(RESTAuth::AUTH_MODE_TOKEN); $apiClient = new RESTSpeaker($restAuth, $baseURI); $response = $apiClient->get("v1/accounts/{$uuid}", [ $this->auth->generateAuthHeaders(), ]); print_r($response); /** Output: stdClass Object ( [the] => actual [json] => stdClass Object ( [object] => 1 [returned] => stdClass Object ( [as] => if [run-through] => json_decode() ) ) ) */ // Get the more to-the-metal HTTPSpeaker: $guzzleResponse = $apiClient->http->get('/someURI');
与Guzzle的对比
// Plain Guzzle $http = new GuzzleClient([ 'base_uri' => 'https://api.my-site.dev/', ]); $response = $http->post("/members/$username/session", [ 'headers' => [ 'X-API-Key' => env('TLSV2_APIKEY'), ], ]); $json = json_decode( $response ->getBody() ->getContents(), true ); // RESTSpeaker $authStrat = new RESTAuth(RESTAuth::AUTH_MODE_XAPI); $api = new RESTSpeaker($authStrat, 'https://api.my-site.dev/'); // For URLs that return Content-Type: application/json: $json = $api->post('/members/' . $username . '/session'); // For all other URL Content-Types: $guzzleResponse = $api->get('https://slashdot.org/'); // If you have a custom REST authentication strategy, simply implement it like this: class MyRestAuthStrat extends RESTAuth { protected function generateCustomAuthOptions(): [] { // Custom code here. return []; } }
用例
PHPExperts\RESTSpeaker\Tests\HTTPSpeaker
✔ 作为Guzzle代理工作
✔ 识别为自己的用户代理
✔ 请求text/html内容类型
✔ 可以获取最后一个原始响应
✔ 可以获取最后一个状态码
PHPExperts\RESTSpeaker\Tests\RESTAuth
✔ 不能自己构建
✔ 子类可以自己构建
✔ 不允许无效的认证模式
✔ 可以设置自定义API客户端
✔ 不会调用不存在的认证策略
✔ 支持无认证
✔ 支持XAPI令牌认证
✔ 支持自定义认证策略
✔ 有自己的env()多态填充
✔ 实现了Guzzle的PSR-18 ClientInterface接口。
PHPExperts\RESTSpeaker\Tests\RESTSpeaker
✔ 可以自己构建
✔ 无内容时返回null
✔ 当不是JSON时返回精确的未修改数据 ✔ JSON URL返回纯PHP数组
✔ 可以回退到HTTPSpeaker
✔ 请求application/json内容类型
✔ 可以获取最后一个Guzzle原始响应
✔ 可以获取最后一个状态码
✔ 将数组自动作为JSON通过POST、PATCH和PUT传递。
✔ 将对象自动作为JSON通过POST、PATCH和PUT传递。
✔ 实现了Guzzle的PSR-18 ClientInterface接口。
测试
phpunit
贡献者
Theodore R. Smith theodore@phpexperts.pro
GPG指纹: 4BF8 2613 1C34 87AC D28F 2AD8 EB24 A91D D612 5690
首席执行官: PHP Experts, Inc.
许可证
MIT许可证。请参阅许可证文件获取更多信息。