php使用GuzzleHttp實現(xiàn)HTTP請求
更新時間:2023年11月06日 10:53:43 作者:bug改一年
這篇文章主要為大家詳細介紹了php如何使用GuzzleHttp實現(xiàn)HTTP請求,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學習一下
1.composer安裝
composer require guzzlehttp/guzzle:~7.0
2.設置過期時間和跳過ssl驗證
use GuzzleHttp\Client; $client=new Client(['timeout' => 5, 'verify' => false]);
3.get請求
use GuzzleHttp\Client;
$client=new Client(['timeout' => 5, 'verify' => false]);
//設置headers頭
$headers=['Content-Type'=>"application/json"];
$url='https://api.netease.im/nimserver/history/queryMediaFileByChannelId.action';
$response=$client->get($url,[
'headers'=>$headers,
]);
//獲取http響應
$response->getStatusCode()
//獲取body找那個返回值信息
json_decode($response->getBody(),true);
//獲取響應頭信息
$response->getHeaders()4.post請求 :json
use GuzzleHttp\Client;
$client=new Client(['timeout' => 5, 'verify' => false]);
//設置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響應
$response->getStatusCode()
//獲取body找那個返回值信息
json_decode($response->getBody(),true);
//獲取響應頭信息
$response->getHeaders()5.post: content-type: application/x-www-form-urlencoded
use GuzzleHttp\Client;
$client=new Client(['timeout' => 5, 'verify' => false]);
//設置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響應
$response->getStatusCode()
//獲取body找那個返回值信息
json_decode($response->getBody(),true);
//獲取響應頭信息
$response->getHeaders()6.delete請求
use GuzzleHttp\Client;
$headers=['Content-Type'=>"application/json"];
$client=new Client(['timeout' => 5, 'verify' => false]);
$response=$client->delete($url,[
'headers'=>$headers,
]);
$code=$response->getStatusCode();、到此這篇關于php使用GuzzleHttp實現(xiàn)HTTP請求的文章就介紹到這了,更多相關php GuzzleHttp實現(xiàn)HTTP請求內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
PHP+MySQL實現(xiàn)輸入頁碼跳轉到指定頁面功能示例
這篇文章主要介紹了PHP+MySQL實現(xiàn)輸入頁碼跳轉到指定頁面功能,結合實例形式分析了php連接mysql數據庫進行數據查詢及分頁顯示、指定頁數跳轉顯示等相關操作技巧,需要的朋友可以參考下2018-06-06

