可扩展的PHP HTTP客户端 - Guzzle

2011-02-28      8898      PHP
项目简介

Guzzle是一个PHP HTTP客户端,可以轻松地发送HTTP请求和轻松集成Web服务。

特性:

  • 用于构建查询字符串,POST请求,流大上传,流大型下载,使用HTTP Cookie,上传JSON数据等的简单界面...
  • 可以使用相同的接口发送同步和异步请求。
  • 对请求,响应和流使用PSR-7接口。这允许您利用其他PSR-7兼容库与Guzzle。
  • 抽象出底层的HTTP传输,允许你编写环境和传输无关的代码;即对cURL,PHP流,套接字或非阻塞事件循环没有硬依赖。
  • 中间件系统允许您扩充和撰写客户端行为。
$client = new \GuzzleHttp\Client();
$res = $client->request('GET', 'https://api.github.com/user', [
    'auth' => ['user', 'pass']
]);
echo $res->getStatusCode();
// 200
echo $res->getHeaderLine('content-type');
// 'application/json; charset=utf8'
echo $res->getBody();
// {"type":"User"...'

// 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();

帮助和文档

安装 Guzzle

The recommended way to install Guzzle is through Composer .

# Install Composer
curl -sS https://getcomposer.org/installer | php

Next, run the Composer command to install the latest stable version of Guzzle:

php composer.phar require guzzlehttp/guzzle

After installing, you need to require Composer's autoloader:

require 'vendor/autoload.php';

You can then later update Guzzle using composer:

composer.phar update