1、使用要求
①、php >= 5.5
②、php.ini 中啟用 allow_url_fopen
③、curl >= 7.19.4
2、安裝
composer require guzzlehttp/guzzle:~6.0
3、發送請求
3.1、快速開始
use GuzzleHttp\Client;
$client = new Client();
$response = $client->request('GET', 'http://xx.com');
$code = $response->getStatusCode();//獲取http響應狀態碼
$data = $response->getBody()->getContents();//獲取響應內容
//--GET請求添加參數
$params = [
'headers' => [//請求頭信息
'token' => 'VBkmpUn8BPMPPNXJnP8CvneYBpQFDdfnetnxQT3C...'
],
'query' => [//附加參數
'type' => 1
]
];
$response = $client->request('GET','http://xx.com',$params);
//--POST請求添加參數
$params = [
'headers' => [//請求頭信息
'token' => 'VBkmpUn8BPMPPNXJnP8CvneYBpQFDdfnetnxQT3C...'
],
'form_params' => [//附加參數
'first_name' => '周末啦',
'last_name' => 'lalala',
'email' => '123@qq.com',
'desc' => '周末啦周末啦周末啦周末啦',
],
'json' => [//傳數組
['name'=>'zs','age'=>10],
['name'=>'zs','age'=>10],
['name'=>'zs','age'=>10],
]
];
$response = $client->request('POST','http://loc.nva.com/app/message',$params);
3.2、魔術方法發送
$response = $client->get('http://httpbin.org/get');
$response = $client->delete('http://httpbin.org/delete');
$response = $client->head('http://httpbin.org/get');
$response = $client->options('http://httpbin.org/get');
$response = $client->patch('http://httpbin.org/patch');
$response = $client->post('http://httpbin.org/post');
$response = $client->put('http://httpbin.org/put');
3.3、創建請求發送
//創建請求發送
use GuzzleHttp\Psr7\Request;
$request = new Request('PUT', 'http://httpbin.org/put');
$response = $client->send($request, ['timeout' => 2]);
4、異步發送請求
4.1、快速開始
$client = new Client();
$promise = $client->requestAsync('GET', 'https://xx.com');
$promise->then(
function (ResponseInterface $res) {
echo $res->getStatusCode() . "\n";
\App\Utils\CommonTrait::Logs('StatusCode',$res->getStatusCode(),'pine');
},
function (RequestException $e) {
echo $e->getMessage() . "\n";
echo $e->getRequest()->getMethod();
\App\Utils\CommonTrait::Logs('Message',$e->getMessage(),'pine');
}
);
$promise->wait();
4.2、魔術方法發送
$promise = $client->getAsync('http://httpbin.org/get');
$promise = $client->deleteAsync('http://httpbin.org/delete');
$promise = $client->headAsync('http://httpbin.org/get');
$promise = $client->optionsAsync('http://httpbin.org/get');
$promise = $client->patchAsync('http://httpbin.org/patch');
$promise = $client->postAsync('http://httpbin.org/post');
$promise = $client->putAsync('http://httpbin.org/put');
4.3、創建請求發送
use GuzzleHttp\Psr7\Request;
$headers = ['X-Foo' => 'Bar'];
$body = 'Hello!';
$request = new Request('HEAD', 'http://httpbin.org/head', $headers, $body);
$promise = $client->sendAsync($request);
$promise = $client->requestAsync('GET', 'http://httpbin.org/get');
5、同時發送多個請求
即並發請求,通過異步實現,避免每次請求等待再進行下次請求
$url = 'http://xx.com?id=';
$arr = ['1','2','3','4','5','6'];
$promises = [];
foreach ($arr as $k=>$v){
$tmpUrl = $url.$v;
$promises[$v] = $client->requestAsync('GET', $tmpUrl,$params);
//同時發請求:
//http://xx.com?id=1
//... ...
//http://xx.com?id=6
}
$results = Promise\settle($promises)->wait();//響應結果
foreach ($results as $k=>$v){
if($v['state'] == 'fulfilled'){
$code = $v['value']->getStatusCode();//響應碼
$data = $v['value']->getBody()->getContents();//響應內容
dump(['code'=>$code,'data'=>$data]);
}
}
暫時只會那么多,有機會后面補上,詳細見官方文檔。