插件窝 干货文章 php使用GuzzleHttp实现HTTP请求

php使用GuzzleHttp实现HTTP请求

Client class headers response 80    来源:    2024-10-12

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请求内容请搜索插件窝以前的文章或继续浏览下面的相关文章希望大家以后多多支持插件窝!