php使用GuzzleHttp实现HTTP请求
作者:bug改一年
这篇文章主要为大家详细介绍了php如何使用GuzzleHttp实现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//发送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实现HTTP请求的文章就介绍到这了,更多相关php GuzzleHttp实现HTTP请求内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!