Guzzle是一個(gè)PHP的HTTP客戶端槐秧,用來(lái)輕而易舉地發(fā)送請(qǐng)求室谚,并集成到我們的WEB服務(wù)上。
1.接口簡(jiǎn)單:構(gòu)建查詢語(yǔ)句石挂、POST請(qǐng)求博助、分流上傳下載大文件、使用HTTP cookies痹愚、上傳JSON數(shù)據(jù)等等富岳。
2.發(fā)送同步或異步的請(qǐng)求均使用相同的接口。
3.使用PSR-7接口來(lái)請(qǐng)求拯腮、響應(yīng)窖式、分流,允許你使用其他兼容的PSR-7類庫(kù)與Guzzle共同開發(fā)动壤。
4.抽象了底層的HTTP傳輸萝喘,允許你改變環(huán)境以及其他的代碼,如:對(duì)cURL與PHP的流或socket并非重度依賴琼懊,非阻塞事件循環(huán)阁簸。
5.中間件系統(tǒng)允許你創(chuàng)建構(gòu)成客戶端行為。
附上文檔鏈接:http://guzzle-cn.readthedocs.io/zh_CN/latest/index.html
使用 composer 安裝 Guzzle 客戶端后哼丈,自己封裝一個(gè)公共方法启妹,以后直接調(diào)用即可:
/**
* 封裝 httpGuzzle
* @param $curlType
* @param $url
* @param $data
* @return mixed
*/
function httpGuzzle($curlType,$url,$data)
{
$curlType = strtoupper($curlType);
switch ($curlType) {
case 'GET':
$config['query'] = $data;
break;
case 'POST':
$config['form_params'] = $data;
break;
default:
# code...
break;
}
$config['verify'] = false; // 指定不用驗(yàn)證SSL證書
$client = new \GuzzleHttp\Client();
$respose = $client->request($curlType,$url,$config);
$result = json_decode($respose->getBody(),true);
return $result;
}