php使用GuzzleHttp實(shí)現(xiàn)HTTP請(qǐng)求
1.composer安裝
composer require guzzlehttp/guzzle:~7.0
2.設(shè)置過(guò)期時(shí)間和跳過(guò)ssl驗(yàn)證
use GuzzleHttp\Client; $client=new Client(['timeout' => 5, 'verify' => false]);
3.get請(qǐng)求
use GuzzleHttp\Client;
$client=new Client(['timeout' => 5, 'verify' => false]);
//設(shè)置headers頭
$headers=['Content-Type'=>"application/json"];
$url='https://api.netease.im/nimserver/history/queryMediaFileByChannelId.action';
$response=$client->get($url,[
'headers'=>$headers,
]);
//獲取http響應(yīng)
$response->getStatusCode()
//獲取body找那個(gè)返回值信息
json_decode($response->getBody(),true);
//獲取響應(yīng)頭信息
$response->getHeaders()4.post請(qǐng)求 :json
use GuzzleHttp\Client;
$client=new Client(['timeout' => 5, 'verify' => false]);
//設(shè)置headers頭
$headers=['Content-Type'=>"application/json"];//json
$url='https://api.netease.im/nimserver/history/queryMediaFileByChannelId.action';
$body=[
"namae"=>'zhou',
"mode"=>2,
"uid"=>1,
];
$response=$client->post($url,[
'headers'=>$headers,
'json'=>$body//發(fā)送body為josn格式
]);
//獲取http響應(yīng)
$response->getStatusCode()
//獲取body找那個(gè)返回值信息
json_decode($response->getBody(),true);
//獲取響應(yīng)頭信息
$response->getHeaders()5.post: content-type: application/x-www-form-urlencoded
use GuzzleHttp\Client;
$client=new Client(['timeout' => 5, 'verify' => false]);
//設(shè)置headers頭
$headers=['Content-Type'=>"application/x-www-form-urlencoded"];
$url='https://api.netease.im/nimserver/history/queryMediaFileByChannelId.action';
$body=[
"namae"=>'zhou',
"mode"=>2,
"uid"=>1,
];
$response=$client->post($url,[
'headers'=>$headers,
'form_params'=>$body
]);
//獲取http響應(yīng)
$response->getStatusCode()
//獲取body找那個(gè)返回值信息
json_decode($response->getBody(),true);
//獲取響應(yīng)頭信息
$response->getHeaders()6.delete請(qǐng)求
use GuzzleHttp\Client;
$headers=['Content-Type'=>"application/json"];
$client=new Client(['timeout' => 5, 'verify' => false]);
$response=$client->delete($url,[
'headers'=>$headers,
]);
$code=$response->getStatusCode();、到此這篇關(guān)于php使用GuzzleHttp實(shí)現(xiàn)HTTP請(qǐng)求的文章就介紹到這了,更多相關(guān)php GuzzleHttp實(shí)現(xiàn)HTTP請(qǐng)求內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PHP7下協(xié)程的實(shí)現(xiàn)方法詳解
最近在學(xué)習(xí)中遇到了協(xié)程,發(fā)現(xiàn)這類文章介紹的較少,所以下面這篇文章主要給大家介紹了關(guān)于PHP7下協(xié)程的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
php強(qiáng)大的時(shí)間轉(zhuǎn)換函數(shù)strtotime
在php中strtotime() 函數(shù)將任何英文文本的日期時(shí)間描述解析為 Unix 時(shí)間戳,這個(gè)函數(shù)也是我們經(jīng)常會(huì)用到的,有需要的朋友參考一下2016-02-02
dede3.1分頁(yè)文字采集過(guò)濾規(guī)則詳說(shuō)(圖文教程)
dede3.1分頁(yè)文字采集過(guò)濾規(guī)則詳說(shuō)(圖文教程)...2007-04-04
使用session判斷用戶登錄用戶權(quán)限(超簡(jiǎn)單)
本篇文章是對(duì)session判斷用戶登錄用戶權(quán)限進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
php倒計(jì)時(shí)出現(xiàn)-0情況的解決方法
這篇文章主要介紹了php倒計(jì)時(shí)出現(xiàn)-0情況的解決方法,實(shí)例分析了php倒計(jì)時(shí)程序出現(xiàn)-0的原因及相應(yīng)的解決方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
PHP下通過(guò)file_get_contents的代理使用方法
2011-02-02

