HTTP_Request2, keep-alive and re-using request objects

[email protected] (Christian Weiske) Fri, 2 Sep 2016 10:00:17 +0200
Newsgroups php.pear.general
Message-ID <[email protected]>
Hi,


HTTP_Request2 supports keep-alive in HTTP requests.
The question is now how to use it.

The following naive code does not use it:

<?php
$req1 = new HTTP_Request2('http://example.org/1.htm');
$res1 = $req1->send();

$req2 = new HTTP_Request2('http://example.org/1.htm');
$res2 = $req2->send();
?>

As far as I see, the keep-alive support is implemented in the adapter.
This means that I could create the adapter myself and share it:

<?php
$adapter = new HTTP_Request2_Adapter_Socket();
$req1 = new HTTP_Request2('http://example.org/1.htm');
$req1->setAdapter($adapter);
$res1 = $req1->send();

$req2 = new HTTP_Request2('http://example.org/1.htm');
$req2->setAdapter($adapter);
$res2 = $req2->send();
?>

This forces me to choose the adapter myself, which I might not want.


Another option is to re-use the request object:

<?php
$req = new HTTP_Request2('http://example.org/1.htm');
$res1 = $req->send();

$req->setURL('http://example.org/1.htm');
$res2 = $req->send();
?>

This works, but the issue I have here are headers, the request body and 
POST data. They are - as far as I see - not reset between requests.

Now my questions:

1. Is the observation about not-resetting headers between requests correct?
2. Did I forget some way to do keep-alive?
3. What's the best way to use keep-alive with HTTP_Request2?


-- 
Regards/Mit freundlichen Grüßen
Christian Weiske

-=≡ Geeking around in the name of science since 1982 ≡=-