php使用GuzzleHttp實現(xiàn)HTTP請求
更新時間:2023年11月06日 10:53:43 作者:bug改一年
這篇文章主要為大家詳細介紹了php如何使用GuzzleHttp實現(xiàn)HTTP請求,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學(xué)習一下
1.composer安裝
composer require guzzlehttp/guzzle:~7.0
2.設(shè)置過期時間和跳過ssl驗證
use GuzzleHttp\Client; $client=new Client(['timeout' => 5, 'verify' => false]);
3.get請求
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找那個返回值信息 json_decode($response->getBody(),true); //獲取響應(yīng)頭信息 $response->getHeaders()
4.post請求 :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找那個返回值信息 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找那個返回值信息 json_decode($response->getBody(),true); //獲取響應(yīng)頭信息 $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();、
到此這篇關(guān)于php使用GuzzleHttp實現(xiàn)HTTP請求的文章就介紹到這了,更多相關(guān)php GuzzleHttp實現(xiàn)HTTP請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
thinkphp操作mongo數(shù)據(jù)的三種方法
這篇文章主要給大家介紹了thinkphp操作mongo數(shù)據(jù)的三種方法,使用tp中的擴展,使用tp中的db類和使用MongoDB PHP驅(qū)動程序這三種方法,并通過代碼講解的非常詳細,需要的朋友可以參考下2023-12-12PHP+MySQL實現(xiàn)輸入頁碼跳轉(zhuǎn)到指定頁面功能示例
這篇文章主要介紹了PHP+MySQL實現(xiàn)輸入頁碼跳轉(zhuǎn)到指定頁面功能,結(jié)合實例形式分析了php連接mysql數(shù)據(jù)庫進行數(shù)據(jù)查詢及分頁顯示、指定頁數(shù)跳轉(zhuǎn)顯示等相關(guān)操作技巧,需要的朋友可以參考下2018-06-06