guzzle [19.8k] http 客户端
最后更新于:2022-04-02 02:23:24
[TOC]
## 安装
`composer require guzzlehttp/guzzle
`
## 接口
前置设置
```
use GuzzleHttp\Client;
$client = new Client([
// Base URI is used with relative requests
'base_uri' => 'http://httpbin.org',
// You can set any number of default request options.
'timeout' => 2.0,
]);
```
| base\_uri | URI | Result |
| --- | --- | --- |
| `http://foo.com` | `/bar` | `http://foo.com/bar` |
| `http://foo.com/foo` | `/bar` | `http://foo.com/bar` |
| `http://foo.com/foo` | `bar` | `http://foo.com/bar` |
| `http://foo.com/foo/` | `bar` | `http://foo.com/foo/bar` |
| `http://foo.com` | `http://baz.com` | `http://baz.com` |
| `http://foo.com/?bar` | `bar` | `http://foo.com/bar` |
### get
```
$response = $client->get("get?a",[
'query' => ['foo' => 'bar']
]);
echo $response->getBody()->getContents().PHP_EOL;
$response = $client->request("get","get?a",[
'query' => ['foo' => 'bar']
]);
echo $response->getBody()->getContents().PHP_EOL;
```
### post row / post 表单 / 表单文件
```
/* row 数据*/
$r = $client->request('POST', 'http://httpbin.org/post', [
'body' => 'raw data'
]);
/* post 表单数据 */
$response = $client->request('POST', 'http://httpbin.org/post', [
'form_params' => [
'field_name' => 'abc',
'other_field' => '123',
'nested_field' => [
'nested' => 'hello'
],
'baz' => ['hi', 'there!']
]
]);
/* form 文件 */
$response = $client->request('POST', 'http://httpbin.org/post', [
'multipart' => [
[
'name' => 'field_name',
'contents' => 'abc'
],
[
'name' => 'file_name',
'contents' => fopen('/path/to/file', 'r')
],
[
'name' => 'other_file',
'contents' => 'hello',
'filename' => 'filename.txt',
'headers' => [
'X-Foo' => 'this is an extra header to include'
]
]
]
]);
```
### 持续保持 cookie
```
$jar = new \GuzzleHttp\Cookie\CookieJar;
$r = $client->request('GET', 'http://httpbin.org/cookies', [
'cookies' => $jar
]);
$client = new \GuzzleHttp\Client(['cookies' => true]);
$r = $client->request('GET', 'http://httpbin.org/cookies');
```
### 并发发送请求
```
use GuzzleHttp\Client;
use GuzzleHttp\Promise;
$client = new Client(['base_uri' => 'http://httpbin.org/']);
/*批量发送信息*/
$promises = [
'get' => $client->getAsync('get',['query'=>['name'=>'aaaa']]),
'webp' => $client->getAsync('image/webp'),
'png' => $client->getAsync('image/png'),
];
$response = new GuzzleHttp\Psr7\Response();
$results = Promise\unwrap($promises);
$results = Promise\settle($promises)->wait();
/*并发完成后获取结果*/
echo $results['get']['state'].PHP_EOL; //fulfilled
echo $results['get']['value']->getBody()->getContents().PHP_EOL; //输出get 请求的内容
file_put_contents("png_1.png", $results['png']['value']->getBody()->getContents()); //图片内容直接保存为图片
```
### 设置是否允许跳转
```
//允许跳转
$response = $client->request('GET', 'http://github.com');
echo $response->getStatusCode();// 200
//不允许跳转
$response = $client->request('GET', 'http://github.com', [
'allow_redirects' => false
]);
echo $response->getStatusCode();// 301
```
### 访问 https
```
$client->request('GET', '/', ['cert' => ['/path/server.pem', 'password']]);
//or
// Use a custom SSL certificate on disk.
$client->request('GET', '/', ['verify' => '/path/to/cert.pem']);
```
### 设置超时
```
$client->request('GET', '/delay/5', ['connect_timeout' => 3.14]);
```
### 携带请求头
```
$client->request('GET', '/get', [
'headers' => [
'User-Agent' => 'testing/1.0',
'Accept' => 'application/json',
'X-Foo' => ['Bar', 'Baz']
]
]);
```
### 设置超时
```
$response = $client->request('GET', '/stream', [
'stream' => true,
'read_timeout' => 10,
]);
```
';