Note that there are some explanatory texts on larger screens.

plurals
  1. POParallel HTTP requests in PHP using PECL HTTP classes [Answer: HttpRequestPool class]
    primarykey
    data
    text
    <hr /> <p><strong> The <a href="http://docs.php.net/manual/en/class.httprequestpool.php" rel="noreferrer">HttpRequestPool</a> class provides a solution. Many thanks to those who pointed this out.</p> <p>A brief tutorial can be found at: <a href="http://www.phptutorial.info/?HttpRequestPool-construct" rel="noreferrer">http://www.phptutorial.info/?HttpRequestPool-construct</a></strong></p> <hr /> <p><strong>Problem</strong></p> <p>I'd like to make concurrent/parallel/simultaneous HTTP requests in PHP. I'd like to avoid consecutive requests as:</p> <ul> <li>a set of requests will take too long to complete; the more requests the longer</li> <li>the timeout of one request midway through a set may cause later requests to not be made (if a script has an execution time limit)</li> </ul> <p>I have managed to find details for making <a href="http://www.phpied.com/simultaneuos-http-requests-in-php-with-curl/" rel="noreferrer">simultaneuos [sic] HTTP requests in PHP with cURL</a>, however I'd like to explicitly use PHP's <a href="http://php.net/manual/en/book.http.php" rel="noreferrer">HTTP functions</a> if at all possible.</p> <p>Specifically, I need to POST data concurrently to a set of URLs. The URLs to which data are posted are beyond my control; they are user-set.</p> <p>I don't mind if I need to wait for all requests to finish before the responses can be processed. If I set a timeout of 30 seconds on each request and requests are made concurrently, I know I must wait a maximum of 30 seconds (perhaps a little more) for all requests to complete.</p> <p>I can find no details of how this might be achieved. However, I did recently notice a mention in the PHP manual of PHP5+ being able to handle concurrent HTTP requests - I intended to make a note of it at the time, forgot, and cannot find it again.</p> <p><strong>Single request example (works fine)</strong></p> <pre><code>&lt;?php $request_1 = new HttpRequest($url_1, HTTP_METH_POST); $request_1-&gt;setRawPostData($dataSet_1); $request_1-&gt;send(); ?&gt; </code></pre> <p><strong>Concurrent request example (incomplete, clearly)</strong></p> <pre><code>&lt;?php $request_1 = new HttpRequest($url_1, HTTP_METH_POST); $request_1-&gt;setRawPostData($dataSet_1); $request_2 = new HttpRequest($url_2, HTTP_METH_POST); $request_2-&gt;setRawPostData($dataSet_2); // ... $request_N = new HttpRequest($url_N, HTTP_METH_POST); $request_N-&gt;setRawPostData($dataSet_N); // Do something to send() all requests at the same time ?&gt; </code></pre> <p>Any thoughts would be most appreciated!</p> <p><strong>Clarification 1</strong>: I'd like to stick to the PECL HTTP functions as:</p> <ul> <li>they offer a nice OOP interface</li> <li>they're used extensively in the application in question and sticking to what's already in use should be beneficial from a maintenance perspective</li> <li>I generally have to write fewer lines of code to make an HTTP request using the PECL HTTP functions compared to using cURL - fewer lines of code should also be beneficial from a maintenance perspective</li> </ul> <p><strong>Clarification 2</strong>: I realise PHP's HTTP functions aren't built in and perhaps I worded things wrongly there, which I shall correct. I have no concerns about people having to install extra stuff - this is not an application that is to be distributed, it's a web app with a server to itself.</p> <p><strong>Clarification 3</strong>: I'd be perfectly happy if someone authoritatively states that the PECL HTTP cannot do this.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload