Guzzle —— HTTP客户端
本文字数:363 字 | 阅读时长 ≈ 1 min

Guzzle —— HTTP客户端

本文字数:363 字 | 阅读时长 ≈ 1 min

Guzzle介绍

Guzzle 是一个 PHP HTTP 客户端,致力于让发送 HTTP 请求以及与 Web 服务进行交互变得简单。

$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'https://api.github.com/user', [
    'auth' => ['user', 'pass']
]);

echo $res->getStatusCode(); # 200
echo $res->getHeader('content-type'); # 'application/json; charset=utf8'
echo $res->getBody(); # '{"id": 1420053, "name": "guzzle", ...}'

# Send an asynchronous request.
$request = new \GuzzleHttp\Psr7\Request('GET', 'http://httpbin.org');
$promise = $client->sendAsync($request)->then(function ($response) {
    echo 'I completed! ' . $response->getBody();
});

$promise->wait();

github官方地址:Guzzle

中文文档地址:Guzzle中文文档

Guzzle安装

推荐使用Composer安装,Composer是PHP的依赖管理工具,允许你在项目中声明依赖关系,并安装这些依赖。

composer require guzzlehttp/guzzle:~6.0

使用

具体使用方法请查看Guzzle中文文档,有更详细的使用介绍。