cURL Client

This client uses cURL PHP extension.

Installation

To install the cURL client, run:

$ composer require php-http/curl-client

This client does not come with a PSR-7 implementation out of the box, so you have to install one as well (for example Guzzle PSR-7):

$ composer require guzzlehttp/psr7

In order to provide full interoperability, message implementations are accessed through factories. Message factories for Laminas Diactoros (and its abandoned predecessor Zend Diactoros), Guzzle PSR-7 and `Slim Framework`_ are available in the message component:

$ composer require php-http/message

Alternatively you can install the discovery layer to be able to automatically find installed resources, like factories:

$ composer require php-http/discovery

Usage

The cURL client needs a message factory and a stream factory in order to 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 use Discovery:

use Http\Client\Curl\Client;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\StreamFactoryDiscovery;

$client = new Client(MessageFactoryDiscovery::find(), StreamFactoryDiscovery::find());

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(MessageFactoryDiscovery::find(), StreamFactoryDiscovery::find(), $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.