easyhttp輕量級語義化 HTTP 客戶端
EasyHttp 是一個輕量級、語義化、對IDE友好的HTTP客戶端,支持常見的HTTP請求、異步請求和并發(fā)請求,讓你可以快速地使用 HTTP 請求與其他 Web 應用進行通信。
EasyHttp 并不強制依賴于 cURL,如果沒有安裝 cURL,EasyHttp 會自動選擇使用 PHP 流處理,或者你也可以提供自己的發(fā)送 HTTP 請求的處理方式。
安裝說明
環(huán)境依賴
- PHP >= 5.5.0
- 如果使用PHP流處理,allow_url_fopen 必須在php.ini中啟用。
- 如果使用cURL處理,cURL >= 7.19.4,并且編譯了OpenSSL 與 zlib。
一鍵安裝
composer require gouguoyin/easyhttp
發(fā)起請求
同步請求
常規(guī)請求
$response = Http::get('http://httpbin.org/get');
$response = Http::post('http://httpbin.org/post');
$response = Http::patch('http://httpbin.org/patch');
$response = Http::put('http://httpbin.org/put');
$response = Http::delete('http://httpbin.org/delete');
$response = Http::head('http://httpbin.org/head');
$response = Http::options('http://httpbin.org/options');
發(fā)送 URL 編碼的請求
// application/json(默認)
$response = Http::asJson()->post(...);
// application/x-www-form-urlencoded
$response = Http::asForm()->post(...);
發(fā)送 Multipart 請求
$response = Http::asMultipart(
'input_name', file_get_contents('photo1.jpg'), 'photo2.jpg'
)->post('http://test.com/attachments');
$response = Http::asMultipart(
'input_name', fopen('photo1.jpg', 'r'), 'photo2.jpg'
)->post('http://test.com/attachments');
$response = Http::attach(
'input_name', file_get_contents('photo1.jpg'), 'photo2.jpg'
)->post('http://test.com/attachments');
$response = Http::attach(
'input_name', fopen('photo1.jpg', 'r'), 'photo2.jpg'
)->post('http://test.com/attachments');
表單enctype屬性需要設置成 multipart/form-data
攜帶請求頭的請求
$response = Http::withHeaders([
'x-powered-by' => 'gouguoyin'
])->post(...);
攜帶重定向的請求
// 默認
$response = Http::withRedirect(false)->post(...);
$response = Http::withRedirect([
'max' => 5,
'strict' => false,
'referer' => true,
'protocols' => ['http', 'https'],
'track_redirects' => false
])->post(...);
攜帶認證的請求
// Basic 認證
$response = Http::withBasicAuth('username', 'password')->post(...);
// Digest 認證(需要被HTTP服務器支持)
$response = Http::withDigestAuth('username', 'password')->post(...);
攜帶Token令牌的請求
$response = Http::withToken('token')->post(...);
攜帶認證文件的請求
$response = Http::withCert('/path/server.pem', 'password')->post(...);
攜帶SSL證書的請求
// 默認
$response = Http::withVerify(false)->post(...);
$response = Http::withVerify('/path/to/cert.pem')->post(...);
攜帶COOKIE的請求
$response = Http::withCookies(array $cookies, string $domain)->post(...);
攜帶協議版本的請求
$response = Http::withVersion(1.0)->post(...);
攜帶代理的請求
$response = Http::withProxy('tcp://localhost:8125')->post(...);
$response = Http::withProxy([
'http' => 'tcp://localhost:8125', // Use this proxy with "http"
'https' => 'tcp://localhost:9124', // Use this proxy with "https",
'no' => ['.com.cn', 'gouguoyin.cn'] // Don't use a proxy with these
])->post(...);
設置超時時間(單位秒)
$response = Http::timeout(60)->post(...);
設置延遲時間(單位秒)
$response = Http::delay(60)->post(...);
設置并發(fā)次數
$response = Http::concurrency(10)->post(...);
異步請求
use Gouguoyin\EasyHttp\Response;
use Gouguoyin\EasyHttp\RequestException;
Http::getAsync('http://easyhttp.gouguoyin.cn/api/sleep3.json', [], function (Response $response) {
echo '請求成功,返回內容:' . $response->body() . PHP_EOL;
}, function (RequestException $e) {
echo '請求異常,錯誤碼:' . $e->getCode() . ',錯誤信息:' . $e->getMessage() . PHP_EOL;
});
Http::postAsync(...);
Http::patchAsync(...);
Http::putAsync(...);
Http::deleteAsync(...);
Http::headAsync(...);
Http::optionsAsync(...);
并發(fā)請求
use Gouguoyin\EasyHttp\Response;
use Gouguoyin\EasyHttp\RequestException;
$stime = microtime(true);
$promises = [
Http::getAsync('http://easyhttp.gouguoyin.cn/api/sleep3.json'),
Http::getAsync('http://easyhttp.gouguoyin.cn/api/sleep1.json'),
Http::getAsync('http://easyhttp.gouguoyin.cn/api/sleep2.json'),
];
Http::concurrency(10)->promise($promises, function (Response $response, $index) {
echo "發(fā)起第 $index 個請求,請求時長:" . $response->json()->second . '秒' . PHP_EOL;
}, function (RequestException $e) {
echo '請求異常,錯誤碼:' . $e->getCode() . ',錯誤信息:' . $e->getMessage() . PHP_EOL;
});
$etime = microtime(true);
$total = floor($etime-$stime);
echo "當前頁面執(zhí)行總時長:{$total} 秒" . PHP_EOL;
//輸出
發(fā)起第 1 個請求,請求時長:1 秒
發(fā)起第 2 個請求,請求時長:2 秒
發(fā)起第 0 個請求,請求時長:3 秒
當前頁面執(zhí)行總時長:3 秒
如果未調用concurrency()方法,并發(fā)次數默認為$promises的元素個數
使用響應
發(fā)起請求后會返回一個 GouguoyinEasyHttpResponse 的實例,該實例提供了以下方法來檢查請求的響應:
$response->body() : string;
$response->json() : object;
$response->array() : array;
$response->status() : int;
$response->ok() : bool;
$response->successful() : bool;
$response->serverError() : bool;
$response->clientError() : bool;
$response->headers() : array;
$response->header($header) : string;
異常處理
請求在發(fā)生客戶端或服務端錯誤時會拋出 GouguoyinEasyHttpRequestException 異常,你可以在請求實例上調用 throw 方法:
$response = Http::post(...);
// 在客戶端或服務端錯誤發(fā)生時拋出異常
$response->throw();
return $response['user']['id'];
GouguoyinEasyHttpRequestException $e提供了以下方法來返回異常信息:
$response->getCode() : int;
$e->getMessage() : string;
$e->getFile() : string;
$e->getLine() : int;
$e->getTrace() : array;
$e->getTraceAsString() : string;評論
圖片
表情
