Clients are the low-level objects to send HTTP requests. All clients are minimalistic both in features and flexibility. You may give the client some default configuration and some additional configuration each time you send a request.
There are 3 clients: FileGetContents
, Curl
and MultiCurl
.
use Buzz\Client\FileGetContents;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7\Request;
$request = new Request('GET', 'https://example.com');
$client = new FileGetContents(new Psr17Factory(), ['allow_redirects' => true]);
$response = $client->sendRequest($request, ['timeout' => 4]);
Not all configuration works will all clients. If there is any client specific configuration it will be noted below.
Type: boolean
Default: false
Should the client follow HTTP redirects or not.
Type: callable
Default: function() {}
Only for MultiCurl
A callback function that is called after a request has been sent.
use Nyholm\Psr7\Request;
$callback = function(RequestInterface $request, ResponseInterface $response = null, ClientException $exception = null) {
// Process the response
};
$request = new Request('GET', 'https://example.com');
$client->sendAsyncRequest($request, array('callback' => $callback));
Type: array
Default: []
Only for Curl and MultiCurl
An array with Curl options.
use Nyholm\Psr7\Request;
$request = new Request('GET', 'https://example.com');
$client->sendAsyncRequest($request, array('curl' => [
CURLOPT_FAILONERROR => false,
]));
Type: boolean
Default: false
Only for Curl and MultiCurl
If set to true
the response header __curl_info
will contain a json_encoded
serialization of the curl metadata information about the response.
use Nyholm\Psr7\Request;
$request = new Request('GET', 'https://example.com');
$response = $client->sendRequest($request, [
'expose_curl_info' => true,
]);
$curlInfo = json_decode($response->getHeader('__curl_info')[0], true);
Type: integer
Default: 5
The maximum number of redirects to follow. Note that this will have no effect unless you set
'allow_redirects' => true
.
Type: string
Default: null
A proxy server to use when sending requests.
Type: callable, null
Default: null
Only for MultiCurl
A callable for CURLMOPT_PUSHFUNCTION
. See PHP docs.
Since MultiCurl supports adding multiple requests, all Push Functions callbacks are
chained together. If one of them returns CURL_PUSH_DENY
, then the request will be denied.
$options['push_function_callback'] = function ($parent, $pushed, $headers) {
if (strpos($header, ':path:') === 0) {
$path = substr($header, 6);
if ($path === '/foo/bar') {
return CURL_PUSH_DENY;
}
return CURL_PUSH_OK;
};
Type: integer
Default: 0
(no limit)
The time to wait before interrupt the request.
Type: boolean
Default: true
Only for MultiCurl
If true, we can use responses pushed to us by HTTP/2.0 server push.
Type: boolean
Default: true
If SSL protocols should verified.
Continue reading about Middleware.