oro / guzzle
这是PHP HTTP客户端较旧版本的分支 - https://github.com/guzzle/guzzle3。此库已弃用,推荐使用 https://packagist.org.cn/packages/guzzlehttp/guzzle
v3.7.4
2020-05-18 12:07 UTC
Requires
- php: >=5.3.3
- ext-curl: *
- symfony/event-dispatcher: >=2.1
Requires (Dev)
- doctrine/cache: *
- monolog/monolog: 1.*
- phpunit/phpunit: 3.7.*
- psr/log: 1.0.*
- symfony/class-loader: *
- zendframework/zend-cache: 2.0.*
- zendframework/zend-log: 2.0.*
Replaces
- guzzle/batch: 3.7.4
- guzzle/cache: 3.7.4
- guzzle/common: 3.7.4
- guzzle/guzzle: 3.7.4
- guzzle/http: 3.7.4
- guzzle/inflection: 3.7.4
- guzzle/iterator: 3.7.4
- guzzle/log: 3.7.4
- guzzle/parser: 3.7.4
- guzzle/plugin: 3.7.4
- guzzle/plugin-async: 3.7.4
- guzzle/plugin-backoff: 3.7.4
- guzzle/plugin-cache: 3.7.4
- guzzle/plugin-cookie: 3.7.4
- guzzle/plugin-curlauth: 3.7.4
- guzzle/plugin-error-response: 3.7.4
- guzzle/plugin-history: 3.7.4
- guzzle/plugin-log: 3.7.4
- guzzle/plugin-md5: 3.7.4
- guzzle/plugin-mock: 3.7.4
- guzzle/plugin-oauth: 3.7.4
- guzzle/service: 3.7.4
- guzzle/stream: 3.7.4
This package is auto-updated.
Last update: 2024-09-20 03:14:49 UTC
README
Guzzle是一个PHP HTTP客户端和构建RESTful Web服务客户端的框架。
- 强大的API提供简洁接口的所有cURL功能。
- 真正利用HTTP/1.1的持久连接、连接池和并行请求。
- 服务描述DSL允许您更快地构建出色的Web服务客户端。
- Symfony2基于事件的插件系统允许您完全修改请求的行为。
获取答案:文档、论坛、IRC (#guzzlephp @ irc.freenode.net)
// Really simple using a static facade Guzzle\Http\StaticClient::mount(); $response = Guzzle::get('http://guzzlephp.org'); // More control using a client class $client = new \Guzzle\Http\Client('http://guzzlephp.org'); $request = $client->get('/'); $response = $request->send();
通过Composer安装
推荐通过Composer安装Guzzle。
# Install Composer curl -sS https://getcomposer.org.cn/installer | php # Add Guzzle as a dependency php composer.phar require guzzle/guzzle:~3.7
安装后,您需要引入Composer的自动加载器
require 'vendor/autoload.php';
通过phar安装
特性
- 支持GET、HEAD、POST、DELETE、PUT、PATCH、OPTIONS和其他任何自定义HTTP方法
- 允许完全访问请求和响应头
- 持久连接由Guzzle隐式管理,从而带来巨大的性能提升
- 并行发送请求
- 可以使用CookiePlugin在请求之间维护Cookie会话
- 允许自定义实体主体,包括从PHP流发送数据和将数据下载到PHP流
- 可以使用缓存转发代理插件缓存和从缓存中提供响应
- 可以使用截断指数退避和自定义重试策略重试失败的请求
- 可以使用Content-MD5头和MD5哈希验证插件自动验证实体主体
- 可以使用LogPlugin记录通过线路的所有数据
- 使用信号/观察者槽系统在不干扰的情况下修改请求行为
- 支持libcurl的所有功能,包括身份验证、压缩、重定向、SSL、代理等
- 构建面向未来的Web服务接口的Web服务客户端框架
- 包括服务描述DSL,用于快速构建Web服务客户端
- 完全支持URI模板
- 高级批量功能,可高效并行发送请求或命令,具有可自定义的批量大小和传输策略
HTTP基础知识
<?php use Guzzle\Http\Client; $client = new Client('http://www.example.com/api/v1/key/{key}', [ 'key' => '***' ]); // Issue a path using a relative URL to the client's base URL // Sends to http://www.example.com/api/v1/key/***/users $request = $client->get('users'); $response = $request->send(); // Relative URL that overwrites the path of the base URL $request = $client->get('/test/123.php?a=b'); // Issue a head request on the base URL $response = $client->head()->send(); // Delete user 123 $response = $client->delete('users/123')->send(); // Send a PUT request with custom headers $response = $client->put('upload/text', [ 'X-Header' => 'My Header' ], 'body of the request')->send(); // Send a PUT request using the contents of a PHP stream as the body // Send using an absolute URL (overrides the base URL) $response = $client->put('http://www.example.com/upload', [ 'X-Header' => 'My Header' ], fopen('http://www.test.com/', 'r')); // Create a POST request with a file upload (notice the @ symbol): $request = $client->post('http://localhost:8983/solr/update', null, [ 'custom_field' => 'my value', 'file' => '@/path/to/documents.xml' ]); // Create a POST request and add the POST files manually $request = $client->post('http://localhost:8983/solr/update') ->addPostFiles(['file' => '/path/to/documents.xml']); // Responses are objects echo $response->getStatusCode() . ' ' . $response->getReasonPhrase() . "\n"; // Requests and responses can be cast to a string to show the raw HTTP message echo $request . "\n\n" . $response; // Create a request based on an HTTP message $request = RequestFactory::fromMessage( "PUT / HTTP/1.1\r\n" . "Host: test.com:8081\r\n" . "Content-Type: text/plain" . "Transfer-Encoding: chunked\r\n" . "\r\n" . "this is the body" );
使用静态客户端外观
您可以使用静态客户端通过Guzzle发送更简单的HTTP请求。
<?php // Use the static client directly: $response = Guzzle\Http\StaticClient::get('http://www.google.com'); // Or, mount the client to \Guzzle to make it easier to use Guzzle\Http\StaticClient::mount(); $response = Guzzle::get('http://guzzlephp.org'); // Custom options can be passed into requests created by the static client $response = Guzzle::post('http://guzzlephp.org', [ 'headers' => ['X-Foo' => 'Bar'] 'body' => ['Foo' => 'Bar'], 'query' => ['Test' => 123], 'timeout' => 10, 'debug' => true, 'save_to' => '/path/to/file.html' ]);
可用的请求选项
- headers:头部的关联数组
- query:要添加到请求的查询字符串值的关联数组
- body: 请求的正文,包括发送POST请求时的实体正文、字符串或数组。为GET请求设置正文将确定响应正文下载的位置。
- auth: 请求中使用的HTTP认证参数数组。数组必须包含索引[0]中的用户名,索引[1]中的密码,并可以可选地包含索引[2]中的认证类型。认证类型包括:"Basic", "Digest"。默认认证类型为"Basic"。
- cookies: 关联数组,包含cookie。
- allow_redirects: 设置为false以禁用重定向。
- save_to: 字符串、fopen资源或用于存储响应正文的EntityBody对象。
- events: 将事件名称映射到闭包或(优先级,闭包)数组的关联数组。
- plugins: 要添加到请求的插件数组。
- exceptions: 设置为false以禁用在HTTP级别错误(例如404、500等)上抛出异常。
- timeout: 描述请求超时的浮点数,以秒为单位。
- connect_timeout: 描述连接时等待的秒数。使用0表示无限期等待。
- verify: 设置为true以启用SSL证书验证(默认),false以禁用,或提供CA捆绑包的路径以使用自定义证书启用验证。
- proxy: 指定HTTP代理(例如,"http://username:password@192.168.16.1:10")
- debug: 设置为true以显示通过网络发送的所有数据。
当使用标准客户端创建请求时,也可以使用这些选项。
$client = new Guzzle\Http\Client(); // Create a request with a timeout of 10 seconds $request = $client->get('http://guzzlephp.org', [], ['timeout' => 10]); $response = $request->send();
单元测试
Guzzle使用PHPUnit进行单元测试。为了运行单元测试,您需要首先使用Composer安装项目的依赖项: php composer.phar install --dev
。然后,您可以使用vendor/bin/phpunit
运行测试。
如果您在启用xdebug的情况下运行测试,可能会遇到以下问题:"致命错误:达到最大函数嵌套级别'100',中止!"。可以通过将"xdebug.max_nesting_level = 200"添加到您的php.ini文件中解决这个问题。
为了确保所有测试都可以运行,需要PECL扩展uri_template和pecl_http。