1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-01-17 21:38:16 +01:00
guzzle/README.md

133 lines
4.9 KiB
Markdown
Raw Normal View History

2011-11-13 19:28:24 -06:00
Guzzle, PHP HTTP client and webservice framework
Guzzle 2.0 Adopting composer for dependency management Updating LICENSE, travis build file, making better use of git ignores, and remove unused build target Removing @author tags. Use the commit history for a changelog. Moving files from build folder to / Adding min build target to product a Guzzle only phar with no autoloader [Common] Accepting ZF1 or ZF2 cache in ZendCacheAdapter [Common] Optimizing Stream wrapper and EntityBody abstractions. [Common] [Http] Migrating from Guzzle event system to the Symfony2 event dispatcher [Common] Moved Inflector and Inspector to Service namespace [Http] Simplifying Guzzle\Guzzle curl detection [Http] Removing Guzzle\Http\Pool and now using Guzzle\Http\Curl\CurlMulti [Http] The helper methods from Guzzle\Http\Message\RequestFactory have been removed to prevent confusion and encourage developers to use Guzzle\Http\Client to create requests. [Http] Clients can now send one or more requests in an array using the send() method, so the batch() method was removed. [Http] Updating curl multi to allow blocking calls while sending other transfers [Http] Making the Request::hasHeader method more intuitive. Guzzle\Http\Message\AbstractMessage::hasHeader() now returns true if the header is found using exact matching. If the header is found using a regex or case-insensitive match, then it will return the name of the found header. [Http] Removing content-type guessing from EntityBody based on file extension and solely using finfo. [Http] Adding basic auth plugin [Http] Cleaning up CookieJar and CurlMulti [Http] Removing custom rawurlencode from QueryString because PHP 5.3 now properly deals with tilde characters. [Http] Minor optimization to parsing messages in RequestFactory [Http] Adding Guzzle\Http\Client for developers that don't need commands or service descriptions [Http] Making it easier to set a global User-Agent header for a Guzzle\Http\Client [Http] Fixing the discrepancies between the ClientInterface and Guzzle\Http\Client [Http] Adding the ability to set and retrieve tokenized headers from Requests and Responses [Service] Ditching NIH filters and using the Symfony2 validator [Service] Moving most service building logic to the ServiceBuilder::factory method so that it is easier to build custom config readers. [Service] Allowing deep nested command inheritance. [Service] Cleaning up Inflector caching. [Service] Getting rid of concept of can_batch because everything is now sent in parallel. [Service] Adding a JSON description builder. [Service] Cleaning up ResourceIteratorApplyBatched. [Service] Removing caching stuff from ServiceBuilder because the data being cached is extremely fast to generate. [Service] Added a method to serialize the ServiceDescription in case a ServiceDescription needs to be cached in an application. [Service] Making description builders use static methods. [Service] Adding support to include other description files for XML and JSON description builders. [Service] Adding support for filters to ApiCommands [Service] Using {{}} instead of $. to reference other services as a dependency for another service
2012-01-14 13:57:05 -06:00
================================================
2011-11-11 17:21:41 -06:00
Guzzle 2.0 Adopting composer for dependency management Updating LICENSE, travis build file, making better use of git ignores, and remove unused build target Removing @author tags. Use the commit history for a changelog. Moving files from build folder to / Adding min build target to product a Guzzle only phar with no autoloader [Common] Accepting ZF1 or ZF2 cache in ZendCacheAdapter [Common] Optimizing Stream wrapper and EntityBody abstractions. [Common] [Http] Migrating from Guzzle event system to the Symfony2 event dispatcher [Common] Moved Inflector and Inspector to Service namespace [Http] Simplifying Guzzle\Guzzle curl detection [Http] Removing Guzzle\Http\Pool and now using Guzzle\Http\Curl\CurlMulti [Http] The helper methods from Guzzle\Http\Message\RequestFactory have been removed to prevent confusion and encourage developers to use Guzzle\Http\Client to create requests. [Http] Clients can now send one or more requests in an array using the send() method, so the batch() method was removed. [Http] Updating curl multi to allow blocking calls while sending other transfers [Http] Making the Request::hasHeader method more intuitive. Guzzle\Http\Message\AbstractMessage::hasHeader() now returns true if the header is found using exact matching. If the header is found using a regex or case-insensitive match, then it will return the name of the found header. [Http] Removing content-type guessing from EntityBody based on file extension and solely using finfo. [Http] Adding basic auth plugin [Http] Cleaning up CookieJar and CurlMulti [Http] Removing custom rawurlencode from QueryString because PHP 5.3 now properly deals with tilde characters. [Http] Minor optimization to parsing messages in RequestFactory [Http] Adding Guzzle\Http\Client for developers that don't need commands or service descriptions [Http] Making it easier to set a global User-Agent header for a Guzzle\Http\Client [Http] Fixing the discrepancies between the ClientInterface and Guzzle\Http\Client [Http] Adding the ability to set and retrieve tokenized headers from Requests and Responses [Service] Ditching NIH filters and using the Symfony2 validator [Service] Moving most service building logic to the ServiceBuilder::factory method so that it is easier to build custom config readers. [Service] Allowing deep nested command inheritance. [Service] Cleaning up Inflector caching. [Service] Getting rid of concept of can_batch because everything is now sent in parallel. [Service] Adding a JSON description builder. [Service] Cleaning up ResourceIteratorApplyBatched. [Service] Removing caching stuff from ServiceBuilder because the data being cached is extremely fast to generate. [Service] Added a method to serialize the ServiceDescription in case a ServiceDescription needs to be cached in an application. [Service] Making description builders use static methods. [Service] Adding support to include other description files for XML and JSON description builders. [Service] Adding support for filters to ApiCommands [Service] Using {{}} instead of $. to reference other services as a dependency for another service
2012-01-14 13:57:05 -06:00
Guzzle is a game changer in the world of PHP HTTP clients. Guzzle allows you to truly reap the benefits of the HTTP/1.1 spec. No other library makes it easier to manage persistent connections or send requests in parallel.</p>
2011-11-13 19:28:24 -06:00
In addition to taking the pain out of HTTP, Guzzle provides a lightweight framework for creating web service clients. Most web service clients follow a specific pattern: create a client class, create methods for each action, create and execute a cURL handle, parse the response, implement error handling, and return the result. Guzzle takes the redundancy out of this process and gives you the tools you need to quickly build a web service client.
Start <strong>truly</strong> consuming HTTP with Guzzle.
2011-11-11 17:21:41 -06:00
2012-01-21 17:31:21 -06:00
- [Download the phar](https://github.com/downloads/guzzle/guzzle/guzzle.phar) and include it in your project
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
[![Build Status](https://secure.travis-ci.org/guzzle/guzzle.png)](http://travis-ci.org/guzzle/guzzle)
2011-11-11 17:21:41 -06:00
Features
--------
- Supports GET, HEAD, POST, DELETE, PUT, and OPTIONS
- Allows full access to request and response headers
- Persistent connections are implicitly managed by Guzzle, resulting in huge performance benefits
- Send requests in parallel
- Cookie sessions can be maintained between requests using the CookiePlugin
- Allows custom entity bodies to be sent in PUT and POST requests, including sending data from a PHP stream
- Responses can be cached and served from cache using the caching reverse proxy plugin
- Failed requests can be retried using truncated exponential backoff
- Entity bodies can be validated automatically using Content-MD5 headers
- All data sent over the wire can be logged using the LogPlugin
- Automatically requests compressed data and automatically decompresses data
- Subject/Observer signal slot system for unobtrusively modifying request behavior
- Supports all of the features of libcurl including authentication, redirects, SSL, proxies, etc
- Web service client framework for building future-proof interfaces to web services
HTTP basics
-----------
```php
<?php
Guzzle 2.0 Adopting composer for dependency management Updating LICENSE, travis build file, making better use of git ignores, and remove unused build target Removing @author tags. Use the commit history for a changelog. Moving files from build folder to / Adding min build target to product a Guzzle only phar with no autoloader [Common] Accepting ZF1 or ZF2 cache in ZendCacheAdapter [Common] Optimizing Stream wrapper and EntityBody abstractions. [Common] [Http] Migrating from Guzzle event system to the Symfony2 event dispatcher [Common] Moved Inflector and Inspector to Service namespace [Http] Simplifying Guzzle\Guzzle curl detection [Http] Removing Guzzle\Http\Pool and now using Guzzle\Http\Curl\CurlMulti [Http] The helper methods from Guzzle\Http\Message\RequestFactory have been removed to prevent confusion and encourage developers to use Guzzle\Http\Client to create requests. [Http] Clients can now send one or more requests in an array using the send() method, so the batch() method was removed. [Http] Updating curl multi to allow blocking calls while sending other transfers [Http] Making the Request::hasHeader method more intuitive. Guzzle\Http\Message\AbstractMessage::hasHeader() now returns true if the header is found using exact matching. If the header is found using a regex or case-insensitive match, then it will return the name of the found header. [Http] Removing content-type guessing from EntityBody based on file extension and solely using finfo. [Http] Adding basic auth plugin [Http] Cleaning up CookieJar and CurlMulti [Http] Removing custom rawurlencode from QueryString because PHP 5.3 now properly deals with tilde characters. [Http] Minor optimization to parsing messages in RequestFactory [Http] Adding Guzzle\Http\Client for developers that don't need commands or service descriptions [Http] Making it easier to set a global User-Agent header for a Guzzle\Http\Client [Http] Fixing the discrepancies between the ClientInterface and Guzzle\Http\Client [Http] Adding the ability to set and retrieve tokenized headers from Requests and Responses [Service] Ditching NIH filters and using the Symfony2 validator [Service] Moving most service building logic to the ServiceBuilder::factory method so that it is easier to build custom config readers. [Service] Allowing deep nested command inheritance. [Service] Cleaning up Inflector caching. [Service] Getting rid of concept of can_batch because everything is now sent in parallel. [Service] Adding a JSON description builder. [Service] Cleaning up ResourceIteratorApplyBatched. [Service] Removing caching stuff from ServiceBuilder because the data being cached is extremely fast to generate. [Service] Added a method to serialize the ServiceDescription in case a ServiceDescription needs to be cached in an application. [Service] Making description builders use static methods. [Service] Adding support to include other description files for XML and JSON description builders. [Service] Adding support for filters to ApiCommands [Service] Using {{}} instead of $. to reference other services as a dependency for another service
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 {
Guzzle 2.0 Adopting composer for dependency management Updating LICENSE, travis build file, making better use of git ignores, and remove unused build target Removing @author tags. Use the commit history for a changelog. Moving files from build folder to / Adding min build target to product a Guzzle only phar with no autoloader [Common] Accepting ZF1 or ZF2 cache in ZendCacheAdapter [Common] Optimizing Stream wrapper and EntityBody abstractions. [Common] [Http] Migrating from Guzzle event system to the Symfony2 event dispatcher [Common] Moved Inflector and Inspector to Service namespace [Http] Simplifying Guzzle\Guzzle curl detection [Http] Removing Guzzle\Http\Pool and now using Guzzle\Http\Curl\CurlMulti [Http] The helper methods from Guzzle\Http\Message\RequestFactory have been removed to prevent confusion and encourage developers to use Guzzle\Http\Client to create requests. [Http] Clients can now send one or more requests in an array using the send() method, so the batch() method was removed. [Http] Updating curl multi to allow blocking calls while sending other transfers [Http] Making the Request::hasHeader method more intuitive. Guzzle\Http\Message\AbstractMessage::hasHeader() now returns true if the header is found using exact matching. If the header is found using a regex or case-insensitive match, then it will return the name of the found header. [Http] Removing content-type guessing from EntityBody based on file extension and solely using finfo. [Http] Adding basic auth plugin [Http] Cleaning up CookieJar and CurlMulti [Http] Removing custom rawurlencode from QueryString because PHP 5.3 now properly deals with tilde characters. [Http] Minor optimization to parsing messages in RequestFactory [Http] Adding Guzzle\Http\Client for developers that don't need commands or service descriptions [Http] Making it easier to set a global User-Agent header for a Guzzle\Http\Client [Http] Fixing the discrepancies between the ClientInterface and Guzzle\Http\Client [Http] Adding the ability to set and retrieve tokenized headers from Requests and Responses [Service] Ditching NIH filters and using the Symfony2 validator [Service] Moving most service building logic to the ServiceBuilder::factory method so that it is easier to build custom config readers. [Service] Allowing deep nested command inheritance. [Service] Cleaning up Inflector caching. [Service] Getting rid of concept of can_batch because everything is now sent in parallel. [Service] Adding a JSON description builder. [Service] Cleaning up ResourceIteratorApplyBatched. [Service] Removing caching stuff from ServiceBuilder because the data being cached is extremely fast to generate. [Service] Added a method to serialize the ServiceDescription in case a ServiceDescription needs to be cached in an application. [Service] Making description builders use static methods. [Service] Adding support to include other description files for XML and JSON description builders. [Service] Adding support for filters to ApiCommands [Service] Using {{}} instead of $. to reference other services as a dependency for another service
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')
));
Guzzle 2.0 Adopting composer for dependency management Updating LICENSE, travis build file, making better use of git ignores, and remove unused build target Removing @author tags. Use the commit history for a changelog. Moving files from build folder to / Adding min build target to product a Guzzle only phar with no autoloader [Common] Accepting ZF1 or ZF2 cache in ZendCacheAdapter [Common] Optimizing Stream wrapper and EntityBody abstractions. [Common] [Http] Migrating from Guzzle event system to the Symfony2 event dispatcher [Common] Moved Inflector and Inspector to Service namespace [Http] Simplifying Guzzle\Guzzle curl detection [Http] Removing Guzzle\Http\Pool and now using Guzzle\Http\Curl\CurlMulti [Http] The helper methods from Guzzle\Http\Message\RequestFactory have been removed to prevent confusion and encourage developers to use Guzzle\Http\Client to create requests. [Http] Clients can now send one or more requests in an array using the send() method, so the batch() method was removed. [Http] Updating curl multi to allow blocking calls while sending other transfers [Http] Making the Request::hasHeader method more intuitive. Guzzle\Http\Message\AbstractMessage::hasHeader() now returns true if the header is found using exact matching. If the header is found using a regex or case-insensitive match, then it will return the name of the found header. [Http] Removing content-type guessing from EntityBody based on file extension and solely using finfo. [Http] Adding basic auth plugin [Http] Cleaning up CookieJar and CurlMulti [Http] Removing custom rawurlencode from QueryString because PHP 5.3 now properly deals with tilde characters. [Http] Minor optimization to parsing messages in RequestFactory [Http] Adding Guzzle\Http\Client for developers that don't need commands or service descriptions [Http] Making it easier to set a global User-Agent header for a Guzzle\Http\Client [Http] Fixing the discrepancies between the ClientInterface and Guzzle\Http\Client [Http] Adding the ability to set and retrieve tokenized headers from Requests and Responses [Service] Ditching NIH filters and using the Symfony2 validator [Service] Moving most service building logic to the ServiceBuilder::factory method so that it is easier to build custom config readers. [Service] Allowing deep nested command inheritance. [Service] Cleaning up Inflector caching. [Service] Getting rid of concept of can_batch because everything is now sent in parallel. [Service] Adding a JSON description builder. [Service] Cleaning up ResourceIteratorApplyBatched. [Service] Removing caching stuff from ServiceBuilder because the data being cached is extremely fast to generate. [Service] Added a method to serialize the ServiceDescription in case a ServiceDescription needs to be cached in an application. [Service] Making description builders use static methods. [Service] Adding support to include other description files for XML and JSON description builders. [Service] Adding support for filters to ApiCommands [Service] Using {{}} instead of $. to reference other services as a dependency for another service
2012-01-14 13:57:05 -06:00
} catch (Guzzle\Common\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";
}
}
Guzzle 2.0 Adopting composer for dependency management Updating LICENSE, travis build file, making better use of git ignores, and remove unused build target Removing @author tags. Use the commit history for a changelog. Moving files from build folder to / Adding min build target to product a Guzzle only phar with no autoloader [Common] Accepting ZF1 or ZF2 cache in ZendCacheAdapter [Common] Optimizing Stream wrapper and EntityBody abstractions. [Common] [Http] Migrating from Guzzle event system to the Symfony2 event dispatcher [Common] Moved Inflector and Inspector to Service namespace [Http] Simplifying Guzzle\Guzzle curl detection [Http] Removing Guzzle\Http\Pool and now using Guzzle\Http\Curl\CurlMulti [Http] The helper methods from Guzzle\Http\Message\RequestFactory have been removed to prevent confusion and encourage developers to use Guzzle\Http\Client to create requests. [Http] Clients can now send one or more requests in an array using the send() method, so the batch() method was removed. [Http] Updating curl multi to allow blocking calls while sending other transfers [Http] Making the Request::hasHeader method more intuitive. Guzzle\Http\Message\AbstractMessage::hasHeader() now returns true if the header is found using exact matching. If the header is found using a regex or case-insensitive match, then it will return the name of the found header. [Http] Removing content-type guessing from EntityBody based on file extension and solely using finfo. [Http] Adding basic auth plugin [Http] Cleaning up CookieJar and CurlMulti [Http] Removing custom rawurlencode from QueryString because PHP 5.3 now properly deals with tilde characters. [Http] Minor optimization to parsing messages in RequestFactory [Http] Adding Guzzle\Http\Client for developers that don't need commands or service descriptions [Http] Making it easier to set a global User-Agent header for a Guzzle\Http\Client [Http] Fixing the discrepancies between the ClientInterface and Guzzle\Http\Client [Http] Adding the ability to set and retrieve tokenized headers from Requests and Responses [Service] Ditching NIH filters and using the Symfony2 validator [Service] Moving most service building logic to the ServiceBuilder::factory method so that it is easier to build custom config readers. [Service] Allowing deep nested command inheritance. [Service] Cleaning up Inflector caching. [Service] Getting rid of concept of can_batch because everything is now sent in parallel. [Service] Adding a JSON description builder. [Service] Cleaning up ResourceIteratorApplyBatched. [Service] Removing caching stuff from ServiceBuilder because the data being cached is extremely fast to generate. [Service] Added a method to serialize the ServiceDescription in case a ServiceDescription needs to be cached in an application. [Service] Making description builders use static methods. [Service] Adding support to include other description files for XML and JSON description builders. [Service] Adding support for filters to ApiCommands [Service] Using {{}} instead of $. to reference other services as a dependency for another service
2012-01-14 13:57:05 -06:00
```
Testing Guzzle
--------------
Here's how to install Guzzle from source to run the unit tests:
```
git clone git@github.com:guzzle/guzzle.git
cd guzzle
composer.phar install --install-suggests
cp phpunit.xml.dist phpunit.xml
phpunit
```