cURL Client
This client uses cURL PHP extension.
Installation
To install the cURL client, run:
$ composer require php-http/curl-client
Usage
The cURL client needs a PSR-17 message factory and stream factory to work. You can either specify the factory explicitly:
use Http\Client\Curl\Client;
use Http\Message\MessageFactory\DiactorosMessageFactory;
use Http\Message\StreamFactory\DiactorosStreamFactory;
$client = new Client(new DiactorosMessageFactory(), new DiactorosStreamFactory());
Or you can omit the parameters and let the client use Discovery to determine a suitable factory:
use Http\Client\Curl\Client;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\StreamFactoryDiscovery;
$client = new Client();
Configuring Client
You can use cURL options to configure Client:
use Http\Client\Curl\Client;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\StreamFactoryDiscovery;
$options = [
CURLOPT_CONNECTTIMEOUT => 10, // The number of seconds to wait while trying to connect.
];
$client = new Client(null, null, $options);
The following options can not be changed in the set up. Most of them are to be provided with the request instead:
CURLOPT_CUSTOMREQUEST
CURLOPT_FOLLOWLOCATION
CURLOPT_HEADER
CURLOPT_HTTP_VERSION
CURLOPT_HTTPHEADER
CURLOPT_NOBODY
CURLOPT_POSTFIELDS
CURLOPT_RETURNTRANSFER
CURLOPT_URL
CURLOPT_USERPWD
Further reading
Use plugins to customize the way HTTP requests are sent and responses processed by following redirects, adding Authentication or Cookie headers and more.
Learn how you can decouple your code from any PSR-7 implementation by using the HTTP factories.
Read more about promises when using asynchronous requests.