Skip to content

Constructing Http objects

Greg Bowler edited this page Mar 16, 2026 · 4 revisions

In most cases, constructing the main Http object can be done without any parameters:

use GT\Fetch\Http;

$http = new Http();

That is enough for the majority of applications. There are however a couple of constructor arguments worth understanding.

Constructor arguments

The constructor signature is:

public function __construct(
	array $curlOptions = [],
	float $interval = 0.01,
	string $curlClass = Curl::class,
	string $curlMultiClass = CurlMulti::class
)

cURL options

The first argument allows us to provide default cURL options that apply to every request made by that Http instance.

$http = new Http([
	CURLOPT_TIMEOUT => 10,
	CURLOPT_CONNECTTIMEOUT => 5,
]);

This is useful when we want to establish application-wide defaults in one place rather than repeating them on every request.

Some default options are already provided internally, including the request method and user agent. Per-request options passed to fetch() will override these defaults where appropriate.

Interval

The second argument is the loop interval used by the internal async timer. In normal use there is rarely a need to change this, but it can be useful during testing or when tuning behaviour in a very specific environment.

cURL classes

The final two arguments exist mainly for testing and specialised integration. They allow custom cURL wrapper classes to be injected instead of the standard GT\Curl\Curl and GT\Curl\CurlMulti.

One Http object or many?

In general, it makes sense to use one Http object for a group of related requests, especially when we want those requests to run concurrently and complete together.

$http = new Http();

$http->fetch("https://example.com/profile");
$http->fetch("https://example.com/messages");
$http->fetch("https://example.com/notifications");

$http->wait();

If two sets of requests are unrelated, it is perfectly reasonable to use separate Http objects for clarity.


In the next section we will make our first request with making HTTP requests with fetch.

Clone this wiki locally