1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-25 18:43:22 +01:00
guzzle/README.md

213 lines
9.0 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
2013-06-10 13:47:53 -07:00
[![Latest Stable Version](https://poser.pugx.org/guzzle/guzzle/version.png)](https://packagist.org/packages/guzzle/guzzle) [![Composer Downloads](https://poser.pugx.org/guzzle/guzzle/d/total.png)](https://packagist.org/packages/guzzle/guzzle) [![Build Status](https://secure.travis-ci.org/guzzle/guzzle.png?branch=master)](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-11-30 10:57:18 +01:00
Get answers with: [Documentation](http://docs.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
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
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
```
2013-06-03 23:12:35 -07:00
After installing, you need to require Composer's autoloader:
2013-06-03 23:12:35 -07:00
```php
require 'vendor/autoload.php';
```
2013-06-03 23:12:35 -07:00
### Installing via phar
As of the 3.7.4 release, each release of Guzzle includes a "guzzle.phar" file that includes all of the files needed to
run Guzzle and all of Guzzle's dependencies. Simply download the phar and include it in your project.
You can find a list of each release and the available downloads at https://github.com/guzzle/guzzle/releases.
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
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
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');
2013-06-03 23:12:35 -07:00
// Or, mount the client to \Guzzle to make it easier to use
Guzzle\Http\StaticClient::mount();
2013-06-03 23:12:35 -07:00
$response = Guzzle::get('http://guzzlephp.org');
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', [
2013-12-31 11:27:45 +08:00
'headers' => ['X-Foo' => 'Bar'],
2013-06-03 23:12:35 -07:00
'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'
]);
```
2013-06-03 23:12:35 -07:00
### Available request options:
* 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.
* auth: Array of HTTP authentication parameters to use with the request. The array must contain the
username in index [0], the password in index [1], and can optionally contain the authentication type in index [2].
The authentication types are: "Basic", "Digest". The default auth type is "Basic".
* 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:
```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();
```
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
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`.
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.