Guzzle makes it easy to send HTTP requests and super simple to integrate with web services. Guzzle manages things like persistent connections, represents query strings as collections, makes it simple to send streaming POST requests with fields and files, and abstracts away the underlying HTTP transport layer (cURL, fopen(), etc.). By providing an object oriented interface for HTTP clients, requests, responses, headers, and message bodies, Guzzle makes it so that you no longer need to fool around with cURL options or stream contexts.
To get a feel for how easy it is to use Guzzle, take a look at the quick start guide.
Guzzle will use the most appropriate HTTP adapter to send requests based on the capabilities of your environment and the options applied to a request. When cURL is available on your system, Guzzle will automatically use cURL. When a request is sent with the stream=true request option, Guzzle will automatically use the PHP stream wrapper HTTP adapter so that bytes are only read from the HTTP stream as needed.
Note
Guzzle has historically only utilized cURL to send HTTP requests. cURL is an amazing HTTP client (arguably the best), and Guzzle will continue to use it by default when it is available. It is rare, but some developers don't have cURL installed on their systems or run into version specific issues. By allowing swappable HTTP adapters, Guzzle is now much more customizable and able to adapt to fit the needs of more developers.
Request and response message bodies use Guzzle Streams, allowing you to stream data without needing to load it all into memory. Guzzle's stream layer provides a large suite of functionality:
Guzzle's flexible event system allows you to completely modify the behavior of a client or request at runtime to cater them for any API. You can send a request with a client, and the client can do things like automatically retry your request if it fails, automatically redirect, log HTTP messages that are sent over the wire, emit progress events as data is uploaded and downloaded, sign requests using OAuth 1.0, verify the integrity of messages before and after they are sent over the wire, and anything else you might need.
Another important aspect of Guzzle is that it's really easy to test clients. You can mock HTTP responses and when testing an adapter implementation, Guzzle provides a mock web server that makes it easy.
Guzzle has a large ecosystem of plugins, including service descriptions which allows you to abstract web services using service descriptions. These service descriptions define how to serialize an HTTP request and how to parse an HTTP response into a more meaningful model object.
Yes, because Guzzle 3 and 4 use different Packagist packages and different namespaces. You simply need to add guzzle/guzzle (Guzzle 3) and guzzlehttp/guzzle (Guzzle 4+) to your project's composer.json file.
{
"require": {
"guzzle/guzzle": 3.*,
"guzzlehttp/guzzle": 4.*
}
}
You might need to use Guzzle 3 and Guzzle 4 in the same project due to a requirement of a legacy application or a dependency that has not yet migrated to Guzzle 4.0.
See https://github.com/guzzle/guzzle/blob/master/UPGRADING.md#3x-to-40.
Maximum function nesting level of '100' reached, aborting
You could run into this error if you have the XDebug extension installed and you execute a lot of requests in callbacks. This error message comes specifically from the XDebug extension. PHP itself does not have a function nesting limit. Change this setting in your php.ini to increase the limit:
xdebug.max_nesting_level = 1000
[source]
This can occur for a number of reasons, but if you are sending PUT, POST, or PATCH requests with an Expect: 100-Continue header, a server that does not support this header will return a 417 response. You can work around this by setting the expect request option to false:
$client = new GuzzleHttp\Client();
// Disable the expect header on a single request
$response = $client->put('/', [], 'the body', [
'expect' => false
]);
// Disable the expect header on all client requests
$client->setDefaultOption('expect', false)
cURL offer a huge number of customizable options. While Guzzle normalizes many of these options across different adapters, there are times when you need to set custom cURL options. This can be accomplished by passing an associative array of cURL settings in the curl key of the config request option.
For example, let's say you need to customize the outgoing network interface used with a client.
$client->get('/', [
'config' => [
'curl' => [
CURLOPT_INTERFACE => 'xxx.xxx.xxx.xxx'
]
]
]);
You can pass custom stream context options using the stream_context key of the config request option. The stream_context array is an associative array where each key is a PHP transport, and each value is an associative array of transport options.
For example, let's say you need to customize the outgoing network interface used with a client and allow self-signed certificates.
$client->get('/', [
'stream' => true,
'config' => [
'stream_context' => [
'ssl' => [
'allow_self_signed' => true
],
'socket' => [
'bindto' => 'xxx.xxx.xxx.xxx'
]
]
]
]);