skyraptor / guzzle-services
提供了一个实现Guzzle命令库的实现,该库使用Guzzle服务描述来描述Web服务,序列化请求,并将响应解析成易于使用的模型结构。
1.1.4
2020-09-09 16:45 UTC
Requires
- php: >=5.5
- guzzlehttp/guzzle: ^6.2|^7.0.1
- guzzlehttp/uri-template: ^0.2.0
- skyraptor/command: dev-master
Requires (Dev)
- phpunit/phpunit: ~4.0
Suggests
- gimler/guzzle-description-loader: ^0.0.4
README
提供了一个实现Guzzle命令库的实现,该库使用Guzzle服务描述来描述Web服务,序列化请求,并将响应解析成易于使用的模型结构。
use GuzzleHttp\Client; use GuzzleHttp\Command\Guzzle\GuzzleClient; use GuzzleHttp\Command\Guzzle\Description; $client = new Client(); $description = new Description([ 'baseUri' => 'http://httpbin.org/', 'operations' => [ 'testing' => [ 'httpMethod' => 'GET', 'uri' => '/get{?foo}', 'responseModel' => 'getResponse', 'parameters' => [ 'foo' => [ 'type' => 'string', 'location' => 'uri' ], 'bar' => [ 'type' => 'string', 'location' => 'query' ] ] ] ], 'models' => [ 'getResponse' => [ 'type' => 'object', 'additionalProperties' => [ 'location' => 'json' ] ] ] ]); $guzzleClient = new GuzzleClient($client, $description); $result = $guzzleClient->testing(['foo' => 'bar']); echo $result['args']['foo']; // bar
安装
可以使用Composer安装此项目
composer require guzzlehttp/guzzle-services
对于 Guzzle 5,使用 composer require guzzlehttp/guzzle-services:0.6
。
注意:如果Composer没有全局安装,那么你可能需要使用php composer.phar
(其中composer.phar
是Composer副本的路径)来运行前面的Composer命令,而不是仅仅使用composer
。
插件
从Guzzle 5.0到6.0的迁移指南
关于PostField和PostFile的变更
请求位置postField
和postFile
已被移除,改为使用formParam
和multipart
。如果您的描述看起来像
[ 'baseUri' => 'http://httpbin.org/', 'operations' => [ 'testing' => [ 'httpMethod' => 'GET', 'uri' => '/get{?foo}', 'responseModel' => 'getResponse', 'parameters' => [ 'foo' => [ 'type' => 'string', 'location' => 'postField' ], 'bar' => [ 'type' => 'string', 'location' => 'postFile' ] ] ] ], ]
则需要将postField
更改为formParam
,将postFile
更改为multipart
。
更多文档即将推出。
食谱
更改查询参数的序列化方式
默认情况下,查询参数使用严格的RFC3986规则进行序列化,使用http_build_query
方法。因此,数组参数以这种方式序列化
$client->myMethod(['foo' => ['bar', 'baz']]); // Query params will be foo[0]=bar&foo[1]=baz
然而,许多野外的API需要移除数字索引,这样查询参数最终会是foo[]=bar&foo[]=baz
。您可以通过创建自己的序列化器并覆盖“查询”请求位置来轻松更改此行为
use GuzzleHttp\Command\Guzzle\GuzzleClient; use GuzzleHttp\Command\Guzzle\RequestLocation\QueryLocation; use GuzzleHttp\Command\Guzzle\QuerySerializer\Rfc3986Serializer; use GuzzleHttp\Command\Guzzle\Serializer; $queryLocation = new QueryLocation('query', new Rfc3986Serializer(true)); $serializer = new Serializer($description, ['query' => $queryLocation]); $guzzleClient = new GuzzleClient($client, $description, $serializer);
如果您有特定的需求,也可以创建自己的序列化器。