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
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.
- Includes a custom node.js webserver to test your clients.
- Unit-tested with PHPUnit with 100% code coverage.
2011-11-13 19:28:24 -06:00
2012-06-17 17:59:53 -07:00
Getting started
---------------
2011-11-11 17:21:41 -06:00
2012-01-23 09:27:24 -06: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
- Docs: [www.guzzlephp.org ](http://www.guzzlephp.org/ )
- Forum: https://groups.google.com/forum/?hl=en#!forum/guzzle
2012-01-16 14:12:22 -06:00
- IRC: [#guzzlephp ](irc://irc.freenode.net/#guzzlephp ) channel on irc.freenode.net
2011-11-13 13:34:08 -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
2012-06-26 17:32:25 -07:00
1. Add ``guzzle/guzzle`` as a dependency in your project's ``composer.json` ` file:
{
"require": {
"guzzle/guzzle": "*"
}
2012-06-17 17:59:53 -07:00
}
2012-06-26 18:58:44 -07:00
Consider tightening your dependencies to a known version when deploying mission critical applications (e.g. ``2.7.*` `).
2012-06-26 17:32:25 -07:00
2. Download and install Composer:
curl -s http://getcomposer.org/installer | php
3. Install your dependencies:
php composer.phar install
4. Require Composer's autoloader
Composer also prepares an autoload file that's capable of autoloading all of the classes in any of the libraries that it downloads. To use it, just add the following line to your code's bootstrap process:
require 'vendor/autoload.php';
You can find out more on how to install Composer, configure autoloading, and other best-practices for defining dependencies at [getcomposer.org ](http://getcomposer.org ).
2011-11-11 17:21:41 -06:00
Features
--------
2012-03-20 11:27:40 -05:00
- Supports GET, HEAD, POST, DELETE, PUT, PATCH, OPTIONS, and any custom verbs
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 )
- 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 )
- 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
2012-06-17 17:59:53 -07:00
- Includes a [service description DSL ](http://guzzlephp.org/guide/service/service_descriptions.html ) for quickly building webservice clients
- Full support for [URI templates ](http://tools.ietf.org/html/rfc6570 )
- 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
$client = new Client('http://www.example.com/api/v1/key/{{key}}', array(
'key' => '***'
));
// 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
$response = $client->put('upload/text', array(
'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', array(
'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, array (
'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(array(
'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"
);
```
Send requests in parallel
-------------------------
```php
< ?php
try {
2012-01-14 13:57:05 -06:00
$client = new Guzzle\Http\Client('http://www.myapi.com/api/v1');
$responses = $client->send(array(
$client->get('users'),
$client->head('messages/123'),
$client->delete('orders/123')
2011-11-12 21:19:22 -06:00
));
2012-04-21 00:23:07 -07:00
} catch (Guzzle\Common\Exception\ExceptionCollection $e) {
2011-11-11 17:21:41 -06:00
echo "The following requests encountered an exception: \n";
foreach ($e as $exception) {
echo $exception->getRequest() . "\n" . $exception->getMessage() . "\n";
}
}
2012-01-14 13:57:05 -06:00
```
2012-02-07 23:13:00 -06:00
URI templates
-------------
2012-06-17 17:59:53 -07:00
Guzzle supports the entire [URI templates RFC ](http://tools.ietf.org/html/rfc6570 ).
2012-02-07 23:13:00 -06:00
```php
< ?php
$client = new Guzzle\Http\Client('http://www.myapi.com/api/v1', array(
'path' => '/path/to',
'a' => 'hi',
'data' => array(
'foo' => 'bar',
'mesa' => 'jarjar'
)
));
$request = $client->get('http://www.test.com{+path}{?a,data*}');
```
The generated request URL would become: ``http://www.test.com/path/to?a=hi&foo=bar&mesa=jarajar` `
You can specify URI templates and an array of additional template variables to use when creating requests:
```php
< ?php
$client = new Guzzle\Http\Client('http://test.com', array(
'a' => 'hi'
));
$request = $client->get(array('/{?a,b}', array(
'b' => 'there'
));
```
The resulting URL would become ``http://test.com?a=hi&b=there` `
2012-06-17 17:59:53 -07:00
Unit testing
------------
2012-01-14 13:57:05 -06:00
2012-06-17 17:59:53 -07:00
[](http://travis-ci.org/guzzle/guzzle)
You will first need to clone the GitHub repository:
2012-01-14 13:57:05 -06:00
```
git clone git@github .com:guzzle/guzzle.git
cd guzzle
2012-04-15 11:58:48 -07:00
```
Next you will need to make sure PHPUnit is configured, Composer is installed, and you have installed Guzzle's
2012-06-17 17:59:53 -07:00
testing dependencies. This can be achieved using the `test-init` Phing task. After running this task, run `phpunit` .
2012-04-15 11:58:48 -07:00
```
phing test-init
2012-01-14 13:57:05 -06:00
phpunit
```
2012-04-15 11:58:48 -07:00
If you do not have Phing installed, you will need to perform the installation steps manually:
```
curl -s http://getcomposer.org/installer | php
2012-04-16 15:13:53 -07:00
php composer.phar install --dev
2012-04-15 11:58:48 -07:00
cp phpunit.xml.dist phpunit.xml
phpunit
2012-04-16 15:13:53 -07:00
```