该 HttpRequestPool class提供了解决方案。非常感谢那些指出这一点的人。
可以在以下位置找到简要教程: http://www.phptutorial.info/?HttpRequestPool-construct
该 HttpRequestPool class提供了解决方案。非常感谢那些指出这一点的人。
可以在以下位置找到简要教程: http://www.phptutorial.info/?HttpRequestPool-construct
我相当确定 HttpRequestPool 是你在找什么。
为了详细说明,您可以使用分叉来实现您正在寻找的东西,但这似乎不必要地复杂并且在HTML上下文中不是非常有用。虽然我没有测试过,但这段代码应该是:
//让$ requests成为要发送的请求数组 $ pool = new HttpRequestPool(); foreach($ request as $ request){ $ pool->连接($请求); } $ pool->发送(); foreach($ pool as $ request){ // 做东西 }
我相当确定 HttpRequestPool 是你在找什么。
为了详细说明,您可以使用分叉来实现您正在寻找的东西,但这似乎不必要地复杂并且在HTML上下文中不是非常有用。虽然我没有测试过,但这段代码应该是:
//让$ requests成为要发送的请求数组 $ pool = new HttpRequestPool(); foreach($ request as $ request){ $ pool->连接($请求); } $ pool->发送(); foreach($ pool as $ request){ // 做东西 }
你试过了吗 HttpRequestPool (这是Http的一部分)?看起来它会汇集请求对象并进行处理。我知道我在某个地方读过Http会同时支持同时请求 池 我也找不到任何东西。
我曾经不得不解决类似的问题:做多个请求而不累积响应时间。
该解决方案最终成为一个使用非阻塞的自定义构建函数 插座。 它的工作原理如下:
$request_list = array(
# address => http request string
#
'127.0.0.1' => "HTTP/1.1 GET /index.html\nServer: website.com\n\n",
'192.169.2.3' => "HTTP/1.1 POST /form.dat\nForm-data: ...",
);
foreach($request_list as $addr => $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.
由于socket_read不等待响应但是如果套接字缓冲区尚未满,则会以半并行方式获取thunked响应,因此速度要快得多。
您可以将其包装在适当的OOP接口中。您 将 需要自己创建HTTP请求字符串,并且当然要处理服务器响应。
一位朋友向我指出了CurlObjects( http://trac.curlobjects.com/trac )最近,我发现使用curl_multi非常有用。
$curlbase = new CurlBase;
$curlbase->defaultOptions[ CURLOPT_TIMEOUT ] = 30;
$curlbase->add( new HttpPost($url, array('name'=> 'value', 'a' => 'b')));
$curlbase->add( new HttpPost($url2, array('name'=> 'value', 'a' => 'b')));
$curlbase->add( new HttpPost($url3, array('name'=> 'value', 'a' => 'b')));
$curlbase->perform();
foreach($ curlbase->请求为$ request){ ... }
PHP的HTTP功能 不是内置的,要么 - 他们是PECL扩展。如果你担心的是人们不得不安装额外的东西,那么两个解决方案都会遇到同样的问题 - 而且我认为cURL更有可能被安装,因为它是我所经历过的每个网络主机的默认设置。
您可以使用pcntl_fork()为每个请求创建一个单独的进程,然后等待它们结束:
http://www.php.net/manual/en/function.pcntl-fork.php
你有什么理由不想使用cURL吗? curl_multi_ *函数可以同时允许多个请求。