ZFSYS

guzzle的简单使用

52

安装

composer require guzzlehttp/guzzle

use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;

设置请求头

        $cookieJar = CookieJar::fromArray([
            'token' => '221FB9D3-968E'
        ], 'api.aaaa.com');  // 此处记得请求域名需要保持跟请求的url host一致,否则不会携带此cookie。

        $client = new Client([
            'cookies' => $cookieJar,
            'headers'=>[
                'User-Agent' => 'okhttp/3.8.1',
                'Host' => 'api.aa.com',
                'Connection' => 'Keep-Alive',
                'Accept-Encoding' => 'gzip',
                "Access-Token" => '104231665f5749ecd79122edbcb89b55f7c55e40',
                "Content-type" => 'application/json',
                "Referer" => 'https://wx.aa.com/dweb2/index/group/',
                "Origin" => 'https://wx.aa.com',
            ]
        ]);

get

$result = $client->get($url);
$ret_json = $result->getBody()->getContents();
$ret_arr = json_decode($ret_json, true);

post

        $result = $client->post($url, [
            'form_params' =>  [
                    'name'=>'aaa'
                ]
        ]);

post json

            $result = $client->post($url, [
                'json' => [        //参数组
                    'req_data'=> [
                        'name'=>'aaa'
                    ]
                ],
            ]
            );

上传

            $result = $client->post($url, [
                'json' => [        //参数组
                    'req_data'=> [
                        'name'=>'aaa'
                    ]
                ],
                'multipart' => [
                    [
                        'name'     => 'file',
                        'contents' => fopen($upload_file, 'r')
                    ],
                ]
            ]
            );

其他请求方式


$response = $client->get('http://httpbin.org/get');
$response = $client->delete('http://httpbin.org/delete');
$response = $client->head('http://httpbin.org/get');
$response = $client->options('http://httpbin.org/get');
$response = $client->patch('http://httpbin.org/patch');
$response = $client->post('http://httpbin.org/post');
$response = $client->put('http://httpbin.org/put');

参考:https://blog.csdn.net/qq_44077052/article/details/125988456

  • 没有任何评论
最新帖子
[站长推荐] 一些站长/创业者使用的工具推荐 591
HTML+JS 全选与取消全选功能 545
子枫内容系统更新记录 449
有没有人做百度小程序啊 447
PHP 按照指定数量切割字符串 392
php composer更换国内源 375
a链接一键安装ios应用 372
composer 报错SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify 347
layui table 的宽度超出自动隐藏 343
怎么克服拖延症呀 339
最近热帖
[站长推荐] 一些站长/创业者使用的工具推荐 591
HTML+JS 全选与取消全选功能 545
子枫内容系统更新记录 449
有没有人做百度小程序啊 447
PHP 按照指定数量切割字符串 392
php composer更换国内源 375
a链接一键安装ios应用 372
composer 报错SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify 347
layui table 的宽度超出自动隐藏 343
怎么克服拖延症呀 339
近期热议
开始使用 161