该库不在主动发展中。
请考虑使用GuzzlehTTP/Guzzle或其他库。
错误仅修复。
Hyper是HTTP客户端,旨在提供一个简单但功能强大的接口,用于进行HTTP调用,获取和操纵API数据。
Hyper::get('http://some/url') 。Hyper::make(...)->get('http://some/url') 。Response对象,该对象提供有用的信息,例如HTTP状态代码,身体和标头。Response都在Rexlabs Array-Object中混合,使您可以轻松询问API响应。$response->getRequest()访问原始Request 。 <?php
use Rexlabs HyperHttp Hyper ;
$ response = Hyper:: get ( ' http://openlibrary.org/subjects/love.json ' );
// The first book for 'love' is: Wuthering Heights
echo " The first book for ' { $ response -> name } ' is: { $ response -> works -> first ()-> title }n" ;
echo " Total works: { $ response -> works -> count ()} books n" ;在项目中安装:
composer require rexlabs/hyper-httpRESTFUL方法都返回一个Response对象,该对象与响应的交互变得简单。
<?php
use Rexlabs HyperHttp Hyper ;
$ response = Hyper:: get ( ' https://example.com/url ' );
echo ' Status Code: ' . $ response -> getStatusCode (). "n" ;
echo ( string ) $ response ; // Output the response body由于响应混合arrayObject,您可以轻松地从响应中获取和操纵值:
<?php
use Rexlabs HyperHttp Hyper ;
// Fetch historical price via CryptoCompare's public API for Ethereum
$ response = Hyper:: get ( ' https://min-api.cryptocompare.com/data/pricehistorical ' , [
' fsym ' => ' ETH ' ,
' tsyms ' => ' BTC,USD ' ,
' ts ' => ' 1452680400 ' ,
]);
// Output prices
printf ( " ETH->USD: %s n" , $ response -> get ( ' ETH.USD ' ));
printf ( " ETH->BTC: %s n" , $ response -> get ( ' ETH.BTC ' ));使用make()简化实例化,然后为将来的请求设置对象:
<?php
use Rexlabs HyperHttp Hyper ;
use Rexlabs Logger CustomLogger ;
$ hyper = Hyper:: make ()
-> setBaseUri ( ' http://example.com/api/v1 ' )
-> setHeader ( ' X-App-Identity ' , ' Some App ' )
-> setHeader ( ' X-Correlation-Id ' , ' 12345 ' )
-> setLogger ( new CustomLogger );$hyper = Hyper::make(array $config = [], GuzzleHttpClient $guzzle, PsrLogLoggerInterface $logger)要完全控制实例化,请使用构造函数并传递在Guzzle实例中:
<?php
use Rexlabs HyperHttp Client ;
use GuzzleHttp Client as GuzzleClient ;
use Psr Log NullLogger ;
$ hyper = new Client ( new GuzzleClient (), new NullLogger (), [
' base_uri ' => ' http://example.com/api/v1 ' ,
' headers ' => [
' X-App-Identity ' => ' Some App ' ,
],
]);
$ response = $ hyper -> get ( ' /messages ' );您可以轻松地生成一个从命令行运行的卷曲请求,以重现您的最后一个请求:
<?php
use Rexlabs HyperHttp Hyper ;
echo Hyper:: get ( ' https://example.com/api/v1/resources ' )
-> getCurlRequest ();输出:
curl
' https://min-api.cryptocompare.com/data/pricehistorical?fsym=ETH&tsyms=BTC%2CUSD&ts=1452680400&extraParams=your_app_name '
-H ' Content-Type: application/json ' -H ' Accept: application/json ' Hyper提供了以下与远程端点交互的方法:
get(mixed $uri, array $query = [], array $headers = [], array $options = []): Response
发送HTTP获取请求,然后返回响应:
$ response = Hyper:: get ( ' https://example.com ' , [ ' sort ' => ' latest ' ], [ ' X-Greeting ' => ' Hello! ' ]);
$ response = $ hyper -> get ( ' /v1/people ' );$uri是字符串或Uri 。如果字符串不是绝对的,则将附加到基本URI。$query是可选的查询参数,将附加到URI上。$headers是可选的标头(由标题名称索引),它将与任何全球标头合并。$options是Guzzle Client选项的可选阵列。 post(mixed $uri, mixed $body = null, array $headers = [], array $options = []): Response
发送HTTP POST请求,然后返回响应:
$ response = Hyper:: post ( ' https://example.com/fruit ' , ' apples ' );
$ response = $ hyper -> post ( ' /v1/people ' , [ ' name ' => ' Bob ' , ' age ' => 25 ]);$uri是字符串或Uri 。如果字符串不是绝对的,则将附加到基本URI。$body是有效载荷。如果提供一个阵列,它将被转换为JSON。$headers是可选的标头(由标题名称索引),它将与任何全球标头合并。$options是Guzzle Client选项的可选阵列。替代方法:
$response = $hyper->postForm($uri, $formParams, $headers, $options);$response = $hyper->postMultipartForm($uri, $formParams, $headers, $options); put(mixed $uri, mixed $body = null, array $headers = [], array $options = []): Response
发送HTTP PUT请求,然后返回响应:
$ response = Hyper:: put ( ' https://example.com/fruit ' , ' apples ' );
$ response = $ hyper -> put ( ' /v1/people ' , [ ' name ' => ' Bob ' , ' age ' => 25 ]);$uri是字符串或Uri 。如果字符串不是绝对的,则将附加到基本URI。$body是有效载荷。如果提供一个阵列,它将被转换为JSON。$headers是可选的标头(由标题名称索引),它将与任何全球标头合并。$options是Guzzle Client选项的可选阵列。 patch(mixed $uri, mixed $body = null, array $headers = [], array $options = []): Response
发送HTTP补丁请求,然后返回响应:
$ response = Hyper:: patch ( ' https://example.com/fruit ' , ' apples ' );
$ response = $ hyper -> patch ( ' /v1/people ' , [ ' name ' => ' Bob ' , ' age ' => 25 ]);$uri是字符串或Uri 。如果字符串不是绝对的,则将附加到基本URI。$body是有效载荷。如果提供一个阵列,它将被转换为JSON。$headers是可选的标头(由标题名称索引),它将与任何全球标头合并。$options是Guzzle Client选项的可选阵列。 delete(mixed $uri, mixed $body = null, array $headers = [], array $options = []): Response
发送HTTP删除请求,然后返回响应:
$ response = Hyper:: delete ( ' https://example.com/fruit ' , ' apples ' );
$ response = $ hyper -> delete ( ' /v1/people/1 ' );$uri是字符串或Uri 。如果字符串不是绝对的,则将附加到基本URI。$body是可选的有效载荷。如果提供一个阵列,它将被转换为JSON。$headers是可选的标头(由标题名称索引),它将与任何全球标头合并。$options是Guzzle Client选项的可选阵列。 call(string $method, mixed $uri, mixed $body, array $headers, array $options): Response
通过将method指定为第一个参数来发送通用的HTTP请求。
// Statically
$ response = Hyper:: call ( ' MOVE ' , ' myfile1234 ' , [ ' new_location ' => ' some_folder ' ]);
// Http method verbs may also be invoked via method name
$ response = Hyper:: move ( ' myfile1234 ' , [ ' new_location ' => ' some_folder ' ]);
$ response = Hyper:: somethingelse (...);
// Via object
$ response = $ hyper -> call ( ' MOVE ' , ' myfile1234 ' , [ ' new_location ' => ' some_folder ' ]);$method是http动词。例如。 GET或不是标准的一部分。$uri是字符串或Uri 。如果字符串不是绝对的,则将附加到基本URI。$body是可选的有效载荷。如果提供一个阵列,它将被转换为JSON。$headers是可选的标头(由标题名称索引),它将与任何全球标头合并。$options是Guzzle Client选项的可选阵列。 RexlabsHyperHttpMessageRequest Object的方法:
返回将此请求封装URI/URL的URIInterface对象。
返回此请求的HTTP方法动词。
为此Request恢复一系列标头
返回适合从命令行运行的卷曲请求(字符串)。可用于调试请求。
可从RexlabsHyperHttpMessageResponse Object中获得的方法:
返回RexlabsHyperHttpMessageRequest对象与Response关联的对象
返回适合从命令行运行的卷曲请求(字符串)。可用于调试请求。
返回此Response的HTTP状态代码。例如。 200
返回与状态代码关联的HTTP原因短语。例如。 “好的”
如果这是JSON响应,则返回true 。
将JSON响应转换为数组,然后返回数组。
将JSON响应转换为ArrayObject
每个Response对象都有从rexlabsarray-object软件包中的ArrayObject类的所有方法和功能。
这意味着基于以下响应有效载荷:
{
"books" : [
{
"id" : 1 ,
"title" : " 1984 " ,
"author" : " George Orwell "
},
{
"id" : 2 ,
"title" : " Pride and Prejudice " ,
"author" : " Jane Austen "
}
]
}您可以执行以下功能:
$ response -> books ; // Instance of ArrayObject
$ response -> books -> pluckArray ( ' author ' ); // array [ 'George Orwell', 'Jane Austen' ]
$ response -> pluckArray ( ' books.author ' ); // array [ 'George Orwell', 'Jane Austen' ]
$ response -> books -> count (); // 2
$ response -> books -> isCollection (); // true
$ response -> books [ 0 ]; // Instance of ArrayObject
$ response -> books [ 0 ]-> isCollection (); // false
$ response -> books [ 0 ]-> id ; // 1
$ response -> get ( ' books.1.title ' ); // "Pride and Prejudice"
foreach ( $ response -> books as $ book ) {
echo "{ $ book -> title } by { $ book -> author }n" ;
}您也可以致电:
$ obj = $ response -> toObject (); // Instance of Arraybject 设置所有客户端的默认配置(默认为[])
Hyper:: setDefaultConfig ( $ config );设置此客户端的配置(值将覆盖 /合并默认值)
$ client = Hyper:: make ( $ config );设置所有不提供一个客户端使用的默认记录器。
必须实现LoggerInterface (默认为NullLogger )
Hyper:: setDefaultLogger ( $ logger );记录所有请求的卷曲字符串(需要一个记录器集)
$ config = [
' log_curl ' => true ,
];将传递给基础的GuzzleClient设置
$ config = [
' guzzle ' => [
' verify ' => false ,
],
];
// Set for all clients
Hyper:: setDefaultConfig ( $ config );
// Set for one client
$ client = Hyper:: make ( $ config );进行测试:
composer tests运行覆盖范围报告:
composer coverage覆盖报告输出到./tests/report/index.html
Hyper允许为自定义客户端扩展:
MyHyperSubclass的静态使用将返回MyHyperSubclass创建的正确实例Hyper的静态使用将返回由Hyper创建的正确实例protected static function makeClient可以自定义客户端类(例如,用new Client myclient替换new MyClient客户端)protected static function makeConfig以自定义默认客户端配置protected static function makeGuzzleConfig自定义默认的guzzle客户端protected static function getBaseUri为客户提供默认的base_uri 欢迎捐款,请提交抽拉赛或创建问题。您提交的代码应使用PSR-1/PSR-2标准进行格式。