HTTP客戶端

v1.1.3
版本
2023-11-14
版本更新時(shí)間
5661
安裝
39
star
簡(jiǎn)介
EasyHttp 是一個(gè)輕量級(jí)、語(yǔ)義化、對(duì)IDE友好的HTTP客戶端,支持常見的HTTP請(qǐng)求、異步請(qǐng)求和并發(fā)請(qǐng)求,讓你可以快速地使用 HTTP 請(qǐng)求與其他 Web 應(yīng)用進(jìn)行通信。
EasyHttp并不強(qiáng)制依賴于cURL,如果沒有安裝cURL,EasyHttp會(huì)自動(dòng)選擇使用PHP流處理,或者你也可以提供自己的發(fā)送HTTP請(qǐng)求的處理方式。
安裝
composer require yzh52521/easyhttp
使用
發(fā)起請(qǐng)求
同步請(qǐng)求
常規(guī)請(qǐng)求
$response = Http::get('http://httpbin.org/get');
$response = Http::get('http://httpbin.org/get?name=yzh52521');
$response = Http::get('http://httpbin.org/get?name=yzh52521', ['age' => 18]);
$response = Http::post('http://httpbin.org/post');
$response = Http::post('http://httpbin.org/post', ['name' => 'yzh52521']);
$response = Http::patch(...);
$response = Http::put(...);
$response = Http::delete(...);
$response = Http::head(...);
$response = Http::options(...);
指定服務(wù)端base_url的請(qǐng)求
// 指定服務(wù)端base_url地址,最終請(qǐng)求地址為 https://serv.yzh52521.com/login
$response = Http::withHost('https://serv.yzh52521.com')->post('/login');
發(fā)送原始數(shù)據(jù)(Raw)請(qǐng)求
$response = Http::withBody(
base64_encode($photo), 'image/jpeg'
)->post(...);
發(fā)送 Content-Type 編碼請(qǐng)求
// application/x-www-form-urlencoded(默認(rèn))
$response = Http::asForm()->post(...);
// application/json
$response = Http::asJson()->post(...);
發(fā)送 Multipart 表單請(qǐng)求
$response = Http::asMultipart(
'file_input_name', file_get_contents('photo1.jpg'), 'photo2.jpg'
)->post('http://test.com/attachments');
$response = Http::asMultipart(
'file_input_name', fopen('photo1.jpg', 'r'), 'photo2.jpg'
)->post(...);
$response = Http::attach(
'file_input_name', file_get_contents('photo1.jpg'), 'photo2.jpg'
)->post(...);
$response = Http::attach(
'file_input_name', fopen('photo1.jpg', 'r'), 'photo2.jpg'
)->post(...);
表單enctype屬性需要設(shè)置成 multipart/form-data
攜帶請(qǐng)求頭的請(qǐng)求
$response = Http::withHeaders([
'x-powered-by' => 'yzh52521'
])->post(...);
攜帶重定向的請(qǐng)求
// 默認(rèn)
$response = Http::withRedirect(false)->post(...);
$response = Http::withRedirect([
'max' => 5,
'strict' => false,
'referer' => true,
'protocols' => ['http', 'https'],
'track_redirects' => false
])->post(...);
$response = Http::maxRedirects(5)->post(...);
攜帶認(rèn)證的請(qǐng)求
// Basic認(rèn)證
$response = Http::withBasicAuth('username', 'password')->post(...);
// Digest認(rèn)證(需要被HTTP服務(wù)器支持)
$response = Http::withDigestAuth('username', 'password')->post(...);
攜帶 User-Agent 的請(qǐng)求
$response = Http::withUA('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36')->post(...);
攜帶Token令牌的請(qǐng)求
$response = Http::withToken('token')->post(...);
攜帶認(rèn)證文件的請(qǐng)求
$response = Http::withCert('/path/server.pem', 'password')->post(...);
攜帶SSL證書的請(qǐng)求
// 默認(rèn)
$response = Http::withVerify(false)->post(...);
$response = Http::withVerify('/path/to/cert.pem')->post(...);
攜帶COOKIE的請(qǐng)求
$response = Http::withCookies(array $cookies, string $domain)->post(...);
攜帶協(xié)議版本的請(qǐng)求
$response = Http::withVersion(1.1)->post(...);
攜帶代理的請(qǐng)求
$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', 'yzh52521.cn'] // Don't use a proxy with these
])->post(...);
設(shè)置超時(shí)時(shí)間(單位秒)
$response = Http::timeout(60)->post(...);
設(shè)置等待服務(wù)器響應(yīng)超時(shí)的最大值(單位秒)
$response = Http::connectTimeout(60)->post(...);
設(shè)置延遲時(shí)間(單位秒)
$response = Http::delay(60)->post(...);
設(shè)置并發(fā)次數(shù)
$response = Http::concurrency(10)->promise(...);
重發(fā)請(qǐng)求,設(shè)置retry方法。重試次數(shù)/兩次重試之間的時(shí)間間隔(毫秒):
$response = Http::retry(3, 100)->post(...);
響應(yīng)的主體部分將要保存的位置
$response = Http::sink('/path/to/file')->post(...);
Guzzle 中間件
use GuzzleHttp\Middleware;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
$response = Http::withMiddleware(
Middleware::mapRequest(function (RequestInterface $request) {
$request = $request->withHeader('X-Example', 'Value');
return $request;
})
)->get('http://example.com');
………………
$response = Http::withRequestMiddleware(
function (RequestInterface $request) {
$request = $request->withHeader('X-Example', 'Value');
return $request;
}
)->get('http://example.com');
………………
$response = Http::withResponseMiddleware(
function (RequestInterface $response) {
$response = $response->getHeader('X-Example');
return $response;
}
)->get('http://example.com');
異步請(qǐng)求
use yzh52521\EasyHttp\Response;
use yzh52521\EasyHttp\RequestException;
$promise = Http::getAsync('http://easyhttp.yzh52521.cn/api/sleep3.json', ['token' => TOKEN], function (Response $response) {
echo '異步請(qǐng)求成功,響應(yīng)內(nèi)容:' . $response->body() . PHP_EOL;
}, function (RequestException $e) {
echo '異步請(qǐng)求異常,錯(cuò)誤碼:' . $e->getCode() . ',錯(cuò)誤信息:' . $e->getMessage() . PHP_EOL;
});
$promise->wait();
echo json_encode(['code' => 200, 'msg' => '請(qǐng)求成功'], JSON_UNESCAPED_UNICODE) . PHP_EOL;
//輸出
{"code":200,"msg":"請(qǐng)求成功"}
異步請(qǐng)求成功,響應(yīng)內(nèi)容:{"code":200,"msg":"success","second":3}
$promise =Http::getAsync('http1://easyhttp.yzh52521.cn/api/sleep3.json', function (Response $response) {
echo '異步請(qǐng)求成功,響應(yīng)內(nèi)容:' . $response->body() . PHP_EOL;
}, function (RequestException $e) {
echo '異步請(qǐng)求異常,錯(cuò)誤信息:' . $e->getMessage() . PHP_EOL;
});
$promise->wait();
echo json_encode(['code' => 200, 'msg' => '請(qǐng)求成功'], JSON_UNESCAPED_UNICODE) . PHP_EOL;
//輸出
{"code":200,"msg":"請(qǐng)求成功"}
異步請(qǐng)求異常,錯(cuò)誤信息:cURL error 1: Protocol "http1" not supported or disabled in libcurl (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
Http::postAsync(...);
Http::patchAsync(...);
Http::putAsync(...);
Http::deleteAsync(...);
Http::headAsync(...);
Http::optionsAsync(...);
使用 等待異步回調(diào)處理完成
Http::wait();
異步并發(fā)請(qǐng)求
use yzh52521\EasyHttp\Response;
use yzh52521\EasyHttp\RequestException;
$promises = [
Http::getAsync('http://easyhttp.yzh52521.cn/api/sleep3.json'),
Http::getAsync('http1://easyhttp.yzh52521.cn/api/sleep1.json', ['name' => 'yzh52521']),
Http::postAsync('http://easyhttp.yzh52521.cn/api/sleep2.json', ['name' => 'yzh52521']),
];
$pool =Http::concurrency(10)->multiAsync($promises, function (Response $response, $index) {
echo "發(fā)起第 $index 個(gè)異步請(qǐng)求,請(qǐng)求時(shí)長(zhǎng):" . $response->json()->second . '秒' . PHP_EOL;
}, function (RequestException $e, $index) {
echo "發(fā)起第 $index 個(gè)請(qǐng)求失敗,失敗原因:" . $e->getMessage() . PHP_EOL;
});
$promise = $pool->promise();
$promise->wait();
//輸出
發(fā)起第 1 個(gè)請(qǐng)求失敗,失敗原因:cURL error 1: Protocol "http1" not supported or disabled in libcurl (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
發(fā)起第 2 個(gè)異步請(qǐng)求,請(qǐng)求時(shí)長(zhǎng):2 秒
發(fā)起第 0 個(gè)異步請(qǐng)求,請(qǐng)求時(shí)長(zhǎng):3 秒
如果未調(diào)用concurrency()方法,并發(fā)次數(shù)默認(rèn)為$promises的元素個(gè)數(shù),$promises數(shù)組里必須是異步請(qǐng)求
使用響應(yīng)
發(fā)起請(qǐng)求后會(huì)返回一個(gè) yzh52521\EasyHttp\Response $response的實(shí)例,該實(shí)例提供了以下方法來檢查請(qǐng)求的響應(yīng):
$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;
異常處理
請(qǐng)求在發(fā)生客戶端或服務(wù)端錯(cuò)誤時(shí)會(huì)拋出 yzh52521\EasyHttp\RequestException $e異常,該實(shí)例提供了以下方法來返回異常信息:
$e->getCode() : int;
$e->getMessage() : string;
$e->getFile() : string;
$e->getLine() : int;
$e->getTrace() : array;
$e->getTraceAsString() : string;
調(diào)試日志
有時(shí)候難免要對(duì) Http 的請(qǐng)求和響應(yīng)包體進(jìn)行記錄以方便查找問題或做什么
//傳遞一個(gè)日志類
thinkphp \think\facade\Log
laravel Illuminate\Support\Facades\Log
webman support\Log
Http::debug(Log::class)->post(...);