Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I once had to solve similar problem: doing multiple requests without cumulating the response times.</p> <p>The solution ended up being a custom-build function which used non-blocking <a href="http://www.php.net/sockets" rel="nofollow noreferrer">sockets</a>. It works something like this:</p> <pre><code>$request_list = array( # address =&gt; http request string # '127.0.0.1' =&gt; "HTTP/1.1 GET /index.html\nServer: website.com\n\n", '192.169.2.3' =&gt; "HTTP/1.1 POST /form.dat\nForm-data: ...", ); foreach($request_list as $addr =&gt; $http_request) { # first, create a socket and fire request to every host $socklist[$addr] = socket_create(); socket_set_nonblock($socklist[$addr]); # Make operation asynchronious if (! socket_connect($socklist[$addr], $addr, 80)) trigger_error("Cannot connect to remote address"); # the http header is send to this host socket_send($socklist[$addr], $http_request, strlen($http_request), MSG_EOF); } $results = array(); foreach(array_keys($socklist) as $host_ip) { # Now loop and read every socket until it is exhausted $str = socket_read($socklist[$host_ip], 512, PHP_NORMAL_READ); if ($str != "") # add to previous string $result[$host_ip] .= $str; else # Done reading this socket, close it socket_close($socklist[$host_ip]); } # $results now contains an array with the full response (including http-headers) # of every connected host. </code></pre> <p>It's much faster since thunked reponses are fetched in semi-parallel since socket_read doesn't wait for the response but returns if the socket-buffer isn't full yet.</p> <p>You can wrap this in appropriate OOP interfaces. You <em>will</em> need to create the HTTP-request string yourself, and process the server response of course.</p>
 

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