php使用GuzzleHttp實(shí)現(xiàn)HTTP請(qǐng)求
1.composer安裝
composer require guzzlehttp/guzzle:~7.0
2.設(shè)置過期時(shí)間和跳過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)文章
提高PHP性能的編碼技巧以及性能優(yōu)化詳細(xì)解析
include文件時(shí)盡量使用絕對(duì)路徑,因?yàn)樗苊饬薖HP去include_path里查找文件的速度,解析操作系統(tǒng)路徑所需的時(shí)間會(huì)更少2013-08-08thinkphp操作mongo數(shù)據(jù)的三種方法
這篇文章主要給大家介紹了thinkphp操作mongo數(shù)據(jù)的三種方法,使用tp中的擴(kuò)展,使用tp中的db類和使用MongoDB PHP驅(qū)動(dòng)程序這三種方法,并通過代碼講解的非常詳細(xì),需要的朋友可以參考下2023-12-12PHP實(shí)現(xiàn)返回JSON和XML的類分享
這篇文章主要給大家分享了一個(gè)使用PHP實(shí)現(xiàn)返回JSON和XML的類,非常實(shí)用,希望大家能夠喜歡2015-01-01PHP+MySQL實(shí)現(xiàn)輸入頁(yè)碼跳轉(zhuǎn)到指定頁(yè)面功能示例
這篇文章主要介紹了PHP+MySQL實(shí)現(xiàn)輸入頁(yè)碼跳轉(zhuǎn)到指定頁(yè)面功能,結(jié)合實(shí)例形式分析了php連接mysql數(shù)據(jù)庫(kù)進(jìn)行數(shù)據(jù)查詢及分頁(yè)顯示、指定頁(yè)數(shù)跳轉(zhuǎn)顯示等相關(guān)操作技巧,需要的朋友可以參考下2018-06-06php簡(jiǎn)單生成一組與多組隨機(jī)字符串的方法
這篇文章主要介紹了php簡(jiǎn)單生成一組與多組隨機(jī)字符串的方法,涉及php基于rand方法的隨機(jī)字符串相關(guān)操作技巧,需要的朋友可以參考下2017-05-05