Reputation: 189
There is any way you can use HTTP persistent connections between requests ? I don't see the CURL extensions having a way to create a pool of connections that's used by all requests as there are in other extensions for mysql, redis, pg.
From what I see you can use persistent http connections only inside the same request.
Silviu
Upvotes: 4
Views: 7130
Reputation: 917
The pecl_http extension for PHP uses libcurl and allows you to open a persistent TCP connection that can be reused:
$client = new http\Client('curl', $persistentHandleID);
$request = new http\Client\Request('GET', 'http://example.com/');
$client->enqueue($request);
$client->send();
$response = $client->getResponse($request);
If another $client
running on the same PHP process (possibly during a different PHP request) accesses the same host and shares the same $persistentHandleID
, it will send its HTTP requests over the same TCP connection as before.
The TCP connection will be kept alive until the PHP module is shut down or until the $client
sends Connection: Close
or forbids further use of the connection:
$client->setOptions(['forbid_reuse' => true, … ]);
Upvotes: 4
Reputation: 1134
In HTTP there is the concept of keep-alive and pipelining. Keep-Alive allows one TCP connection to be used across multiple HTTP requests and responses. This allows a browser to load a web page with all its resources (e.g. images, scripts, etc.) on one TCP connection, which avoids the connection setup and tear down overhead. CURL uses this by default, appending Connection: keep-alive
to the header. The server is configured for max requests and how long the connection will remain open. But it still acts in a request- response mode.
With Pipelining multiple requests can be made prior to receiving a response. Pipelining is not widely implemented and to work both the client and server must implement it. To use in CURL you must find a library (there are some out there) which implements it for you client, and again the server must also utilize it.
If you are unsure if your CURL app is sending the keep-alive header then use an http proxy or packet sniffer to inspect your traffic.
Upvotes: 2