本文主要是介绍PHP使用GuzzleHttp进行HTTP请求,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1,composer安装
composer require guzzlehttp/guzzle:~7.0
2,设置过期时间和跳过ssl验证
use GuzzleHttp\Client;$client=new Client(['timeout' => 5, 'verify' => false]);
2,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()
3,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//发送body为josn格式]);//获取http响应$response->getStatusCode()//获取body找那个返回值信息json_decode($response->getBody(),true);//获取响应头信息$response->getHeaders()
4,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()
3,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进行HTTP请求的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!