4

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

2 Answers 2

5

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, … ]);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Joe, This is what I'm searching for !
You might wanna take a look at this excellent article: technosophos.com/2012/06/18/…
2

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.

2 Comments

With HTTP/1.1 persistent connections are the default and no Connection: header is needed for that. All a PHP program needs to do is to reuse the same curl handle when doing subsequent requests.
my question was : There is any way you can use HTTP persistent connections between requests and also in the final post I worte: From what I see you can use persistent http connections only inside the same request. Because of the php scripts life-cycle I don't see how you can use a pool of connections.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.