PHP项目中使用Guzzle执行POST和GET请求

以往在项目中要用到第三方接口时会用到封装好的curl执行请求,现在有了更好的解决方案——Guzzle。
下面是官方介绍:

Guzzle是一个PHP的HTTP客户端,用来轻而易举地发送请求,并集成到我们的WEB服务上。

  • 接口简单:构建查询语句、POST请求、分流上传下载大文件、使用HTTP cookies、上传JSON数据等等。
  • 发送同步或异步的请求均使用相同的接口。
  • 使用PSR-7接口来请求、响应、分流,允许你使用其他兼容的PSR-7类库与Guzzle共同开发。
  • 抽象了底层的HTTP传输,允许你改变环境以及其他的代码,如:对cURL与PHP的流或socket并非重度依赖,非阻塞事件循环。
  • 中间件系统允许你创建构成客户端行为。

安装

composer require guzzlehttp/guzzle //用composer安装最新guzzle,当前是6.3版

GET请求示例

$client = new GuzzleHttp\Client(); //初始化客户端
$response = $client->get('http://httpbin.org/get', [
            'query' => [		//get查询字符串参数组
                'a' => '参数a的值',
                'b' => '参数b的值',
            ],
            'timeout' => 3.14 //设置请求超时时间
        ]);
        
//       与上面一条等价
//        $response = $client->request('GET','http://httpbin.org/get', [
//            'query' => [        
//                'a' => '参数a的值',
//                'b' => '参数b的值',
//            ],
//            'timeout' => 3.14 
//        ]);

$body = $response->getBody(); //获取响应体,对象
$bodyStr = (string)$body; //对象转字串,这就是请求返回的结果
echo $bodyStr;

类似的请求方法还有:

$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');

POST请求示例

$client = new GuzzleHttp\Client();
//普通表单`application/x-www-form-urlencoded`的POST请求
$response = $client->post('http://httpbin.org/post', [
    'form_params' => [        //参数组
        'a' => 'aaa',
        'b' => 'bbb',
        'nested_field' => [		//参数允许嵌套多层
            'A' => 'AAA',
            'B' => 'BBB',
        ]
    ],
]);
        
        //包含文件上传的表单`multipart/form-data`的POST请求
//        $response = $client->post('http://httpbin.org/post', [
//            'multipart' => [		//注意这个参数组的键名与前一个不同
//                [
//                    'name' => 'a',		//字段名
//                    'contents' => 'aaa'	//对应的值
//                ],
//                [
//                    'name' => 'upload_file_name',		//文件字段名
//                    'contents' => fopen('/data/test.md', 'r') //文件资源
//                ],
//            ]
//        ]);
        
        $body = $response->getBody(); //获取响应体,对象
        $bodyStr = (string)$body; //对象转字串
        echo $bodyStr;

以上便是Guzzle的POST和GET请求的基本介绍,相信很多时间掌握这两个语法方法已经能满足项目开发的需求了。当然这只是强大的Guzzle功能中很小的一部份,感兴趣的同学想深入了解的可以参考官方文档。

Guzzle6中文文档参考

(end)

使用 GuzzleHttp 发送 multipart/form-data POST 请求,可以按照以下步骤进行: 1. 首先,需要使用 Composer 安装 GuzzleHttp: ``` composer require guzzlehttp/guzzle ``` 2. 在需要使用 GuzzleHttp 的文件引入 GuzzleHttp: ```php use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request; ``` 3. 构造 GuzzleHttp 客户端: ```php $client = new Client(); ``` 4. 构造发送的数据: ```php $data = [ [ 'name' => 'field1', 'contents' => 'value1' ], [ 'name' => 'field2', 'contents' => 'value2' ], [ 'name' => 'file', 'contents' => fopen('/path/to/file', 'r'), 'filename' => 'filename.jpg' ] ]; ``` 其,第一个和第二个元素是普通的键值对,第三个元素是上传的文件,使用 `fopen()` 函数打开文件并传递给 `'contents'` 参数。 5. 发送请求: ```php $response = $client->request('POST', 'https://example.com/upload', [ 'multipart' => $data ]); ``` 其,`'multipart'` 参数指定了发送的数据格式为 multipart/form-data。 完整代码示例: ```php use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request; $client = new Client(); $data = [ [ 'name' => 'field1', 'contents' => 'value1' ], [ 'name' => 'field2', 'contents' => 'value2' ], [ 'name' => 'file', 'contents' => fopen('/path/to/file', 'r'), 'filename' => 'filename.jpg' ] ]; $response = $client->request('POST', 'https://example.com/upload', [ 'multipart' => $data ]); ``` 注意,这里的 `'https://example.com/upload'` 是示例网址,请替换成你需要发送的实际网址。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值