2011-11-13 19:28:24 -06:00
Guzzle, PHP HTTP client and webservice framework
2012-01-14 13:57:05 -06:00
================================================
2011-11-11 17:21:41 -06:00
2013-06-10 13:47:53 -07:00
[](https://packagist.org/packages/guzzle/guzzle) [](https://packagist.org/packages/guzzle/guzzle) [](http://travis-ci.org/guzzle/guzzle)
2012-06-17 17:59:53 -07:00
Guzzle is a PHP HTTP client and framework for building RESTful web service clients.
2011-11-13 19:28:24 -06:00
2012-06-17 17:59:53 -07:00
- Extremely powerful API provides all the power of cURL with a simple interface.
- Truly take advantage of HTTP/1.1 with persistent connections, connection pooling, and parallel requests.
- Service description DSL allows you build awesome web service clients faster.
- Symfony2 event-based plugin system allows you to completely modify the behavior of a request.
2013-06-03 23:12:35 -07:00
2013-06-03 23:16:12 -07:00
Get answers with: [Documentation ](http://www.guzzlephp.org/ ), [Forums ](https://groups.google.com/forum/?hl=en#!forum/guzzle ), IRC ([#guzzlephp ](irc://irc.freenode.net/#guzzlephp ) @ irc.freenode.net)
2013-06-03 23:12:35 -07:00
```php
// 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();
```
2011-11-13 19:28:24 -06:00
2012-06-17 17:59:53 -07:00
### Installing via Composer
2012-06-26 17:32:25 -07:00
The recommended way to install Guzzle is through [Composer ](http://getcomposer.org ).
2012-06-17 17:59:53 -07:00
2013-06-03 23:12:35 -07:00
```bash
# Install Composer
2013-06-04 00:29:54 -06:00
curl -sS https://getcomposer.org/installer | php
2012-06-17 17:59:53 -07:00
2013-06-03 23:12:35 -07:00
# Add Guzzle as a dependency
2013-06-10 23:36:02 -07:00
php composer.phar require guzzle/guzzle:~3.7
2013-06-03 23:12:35 -07:00
```
2012-06-26 17:32:25 -07:00
2013-06-03 23:12:35 -07:00
After installing, you need to require Composer's autoloader:
2012-06-26 17:32:25 -07:00
2013-06-03 23:12:35 -07:00
```php
require 'vendor/autoload.php';
```
2012-06-26 17:32:25 -07:00
2013-06-03 23:12:35 -07:00
### Installing via phar
2012-06-26 17:32:25 -07:00
2013-06-03 23:12:35 -07:00
[Download the phar ](http://guzzlephp.org/guzzle.phar ) and include it in your project
([minimal phar ](http://guzzlephp.org/guzzle-min.phar ))
2011-11-11 17:21:41 -06:00
Features
--------
2013-06-03 23:12:35 -07:00
- Supports GET, HEAD, POST, DELETE, PUT, PATCH, OPTIONS, and any other custom HTTP method
2011-11-11 17:21:41 -06:00
- Allows full access to request and response headers
- Persistent connections are implicitly managed by Guzzle, resulting in huge performance benefits
2012-06-17 17:59:53 -07:00
- [Send requests in parallel ](http://guzzlephp.org/tour/http.html#send-http-requests-in-parallel )
2013-06-03 23:12:35 -07:00
- Cookie sessions can be maintained between requests using the
[CookiePlugin ](http://guzzlephp.org/tour/http.html#cookie-session-plugin )
- Allows custom [entity bodies ](http://guzzlephp.org/tour/http.html#entity-bodies ), including sending data from a PHP
stream and downloading data to a PHP stream
- Responses can be cached and served from cache using the
[caching forward proxy plugin ](http://guzzlephp.org/tour/http.html#php-based-caching-forward-proxy )
- Failed requests can be retried using
[truncated exponential backoff ](http://guzzlephp.org/tour/http.html#truncated-exponential-backoff ) with custom retry
policies
- Entity bodies can be validated automatically using Content-MD5 headers and the
[MD5 hash validator plugin ](http://guzzlephp.org/tour/http.html#md5-hash-validator-plugin )
- All data sent over the wire can be logged using the
[LogPlugin ](http://guzzlephp.org/tour/http.html#over-the-wire-logging )
- Subject/Observer signal slot system for unobtrusively
[modifying request behavior ](http://guzzlephp.org/guide/http/creating_plugins.html )
2012-06-17 17:59:53 -07:00
- Supports all of the features of libcurl including authentication, compression, redirects, SSL, proxies, etc
2011-11-11 17:21:41 -06:00
- Web service client framework for building future-proof interfaces to web services
2013-06-03 23:12:35 -07:00
- Includes a [service description DSL ](http://guzzlephp.org/guide/service/service_descriptions.html ) for quickly
building webservice clients
2012-06-17 17:59:53 -07:00
- Full support for [URI templates ](http://tools.ietf.org/html/rfc6570 )
2013-06-03 23:12:35 -07:00
- Advanced batching functionality to efficiently send requests or commands in parallel with customizable batch sizes
and transfer strategies
2011-11-11 17:21:41 -06:00
HTTP basics
-----------
```php
< ?php
2012-01-14 13:57:05 -06:00
use Guzzle\Http\Client;
2011-11-11 17:21:41 -06:00
2013-06-03 23:12:35 -07:00
$client = new Client('http://www.example.com/api/v1/key/{key}', [
2011-11-11 17:21:41 -06:00
'key' => '***'
2013-06-03 23:12:35 -07:00
]);
2011-11-11 17:21:41 -06:00
// Issue a path using a relative URL to the client's base URL
// Sends to http://www.example.com/api/v1/key/***/users
2011-11-11 17:26:18 -06:00
$request = $client->get('users');
2011-11-11 17:21:41 -06:00
$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
2013-06-03 23:12:35 -07:00
$response = $client->put('upload/text', [
2011-11-11 17:21:41 -06:00
'X-Header' => 'My Header'
2013-06-03 23:12:35 -07:00
], 'body of the request')->send();
2011-11-11 17:21:41 -06:00
// Send a PUT request using the contents of a PHP stream as the body
// Send using an absolute URL (overrides the base URL)
2013-06-03 23:12:35 -07:00
$response = $client->put('http://www.example.com/upload', [
2011-11-11 17:21:41 -06:00
'X-Header' => 'My Header'
2013-06-03 23:12:35 -07:00
], fopen('http://www.test.com/', 'r'));
2011-11-11 17:21:41 -06:00
// Create a POST request with a file upload (notice the @ symbol):
2013-06-03 23:12:35 -07:00
$request = $client->post('http://localhost:8983/solr/update', null, [
2011-11-11 17:21:41 -06:00
'custom_field' => 'my value',
'file' => '@/path/to/documents .xml'
2013-06-03 23:12:35 -07:00
]);
2011-11-11 17:21:41 -06:00
// Create a POST request and add the POST files manually
$request = $client->post('http://localhost:8983/solr/update')
2013-06-03 23:12:35 -07:00
->addPostFiles(['file' => '/path/to/documents.xml']);
2011-11-11 17:21:41 -06:00
// 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" .
2013-02-08 15:34:36 +02:00
"Content-Type: text/plain" .
2011-11-11 17:21:41 -06:00
"Transfer-Encoding: chunked\r\n" .
"\r\n" .
"this is the body"
);
```
2013-06-03 23:12:35 -07:00
Using the static client facade
------------------------------
You can use Guzzle through a static client to make it even easier to send simple HTTP requests.
2011-11-11 17:21:41 -06:00
```php
< ?php
2013-06-03 23:12:35 -07:00
// Use the static client directly:
$response = Guzzle\Http\StaticClient::get('http://www.google.com');
2012-02-07 23:13:00 -06:00
2013-06-03 23:12:35 -07:00
// Or, mount the client to \Guzzle to make it easier to use
Guzzle\Http\StaticClient::mount();
2012-02-07 23:13:00 -06:00
2013-06-03 23:12:35 -07:00
$response = Guzzle::get('http://guzzlephp.org');
2012-02-07 23:13:00 -06:00
2013-06-03 23:12:35 -07:00
// 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'],
2013-06-03 23:17:33 -07:00
'query' => ['Test' => 123],
2013-06-03 23:12:35 -07:00
'timeout' => 10,
'debug' => true,
'save_to' => '/path/to/file.html'
]);
2012-02-07 23:13:00 -06:00
```
2013-06-03 23:12:35 -07:00
### Available request options:
2013-06-07 18:43:57 -07:00
* headers: Associative array of headers
* query: Associative array of query string values to add to the request
* body: Body of a request, including an EntityBody, string, or array when sending POST requests. Setting a body for a
2013-06-03 23:12:35 -07:00
GET request will set where the response body is downloaded.
2013-06-07 21:29:56 -07:00
* auth: Array of HTTP authentication parameters to use with the request. The array must contain the
2013-09-07 21:31:15 +02:00
username in index [0], the password in index [1], and can optionally contain the authentication type in index [2].
2013-06-07 21:29:56 -07:00
The authentication types are: "Basic", "Digest". The default auth type is "Basic".
2013-06-07 18:43:57 -07:00
* cookies: Associative array of cookies
* allow_redirects: Set to false to disable redirects
* save_to: String, fopen resource, or EntityBody object used to store the body of the response
* events: Associative array mapping event names to a closure or array of (priority, closure)
* plugins: Array of plugins to add to the request
* exceptions: Set to false to disable throwing exceptions on an HTTP level error (e.g. 404, 500, etc)
* timeout: Float describing the timeout of the request in seconds
* connect_timeout: Float describing the number of seconds to wait while trying to connect. Use 0 to wait
indefinitely.
* verify: Set to true to enable SSL cert validation (the default), false to disable, or supply the path to a CA
bundle to enable verification using a custom certificate.
* proxy: Specify an HTTP proxy (e.g. "http://username:password@192 .168.16.1:10")
* debug: Set to true to display all data sent over the wire
2013-06-03 23:12:35 -07:00
2013-06-03 23:20:26 -07:00
These options can also be used when creating requests using a standard client:
2012-02-07 23:13:00 -06:00
```php
2013-06-03 23:12:35 -07:00
$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();
2012-02-07 23:13:00 -06:00
```
2012-10-31 22:47:44 -07:00
Unit testing
------------
2012-11-03 00:09:36 +00:00
Guzzle uses PHPUnit for unit testing. In order to run the unit tests, you'll first need
2012-10-31 22:47:44 -07:00
to install the dependencies of the project using Composer: `php composer.phar install --dev` .
2013-06-03 23:12:35 -07:00
You can then run the tests using `vendor/bin/phpunit` .
2013-06-30 11:14:39 +01:00
If you are running the tests with xdebug enabled, you may encounter the following issue: 'Fatal error: Maximum function nesting level of '100' reached, aborting!'. This can be resolved by adding 'xdebug.max_nesting_level = 200' to your php.ini file.
2013-06-30 11:21:03 +01:00
The PECL extensions, uri_template and pecl_http will be required to ensure all the tests can run.