該庫不在主動發展中。
請考慮使用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標准進行格式。