fenzland / http-api-gluer
0.2.0
2018-03-26 07:56 UTC
Requires
- php: >=7.1.0
- fenzland/data-parser: ^0.1.0
- fenzland/http: ^0.1.13
This package is not auto-updated.
Last update: 2024-09-15 05:49:46 UTC
README
一个用于调用HTTP API的库,避免硬编码特定API的详细信息。
用法
composer require fenzland/http-api-gluer
创建Gluer实例。
use Fenzland\HttpApiGluer\Gluer; use Fenzland\DataParser\Transformer; $gluer= new Gluer( 'https://url' // URL of API. , 'POST' // Method of API. , $request_transformer // instance of Transformer. , $response_transformer // instance of Transformer. , 'application/json' // content type of request , 'application/json' // content type of response (optional if same with content type of request) ); // or $gluer= Gluer::make_( 'https://url/{path_param}' , 'POST' , $request_transformer_meta // meta array of Transformer. , $response_transformer_meta // meta array of Transformer. , 'application/json' );
调用API。
$result= $gluer->call( $data );
账户类型
内置支持
- application/octet-stream
- application/php-serialized
- application/json
- application/x-www-form-urlencoded
扩展
use Fenzland\HttpApiGluer\serializers\ASerializer class CustomSerializer extends ASerializer { public function encode( $data ):string { # ... } public function decode( string$encoded ) { # ... } } Gluer::registerSerializer_( 'custom/mime', CustomSerializer::class ); // or Gluer::registerSerializer_( 'custom/mime', new CustomSerializer );
关于Transformer
请求转换器
[ 'path'=> ..., // parameters in path example: https://github.com/{name}/{repo} 'query'=> ..., // parameters in url query example: ?key0=value0&key1=value1 'headers'=> ..., // parameters in header 'body'=> ..., // parameters or data in body ];
响应转换器
[ "foo_in_header"=> [ 'type'=> 'value', 'keys'=> [ 'headers', "foo", ], ], "bar_in_body"=> [ 'type'=> 'value', 'keys'=> [ 'body', "bar", "baz", ], ], ];