CURL 同时测试一个 url 的多个 ip 地址:响应问题
我正在对 URL 发出卷曲请求。此 URL 由许多服务器托管 ==>许多IP地址。 托管,
- 想象一下“www.example.com”由192.168.60.31
- 198.166.15.15
- 217.15.74.1
这是我所做的测试:
public function curlInfosMulti($ips,$url, $timeout, $method, $params) {
$count= count($ips);
$container = array ();
$tab = array('Host:'.$url);
// set URL and other appropriate options
for ($i=0; $i<$count; $i++)
{
$container[$i] = curl_init();
curl_setopt($container[$i], CURLOPT_URL, $ips[$i]);
curl_setopt($container[$i], CURLOPT_HTTPHEADER, $tab);
curl_setopt($container[$i], CURLOPT_HEADER, 1);
curl_setopt($container[$i], CURLOPT_RETURNTRANSFER, true);
}
//create the multiple cURL handle
$mh = curl_multi_init();
for ($i=0; $i<$count; $i++)
{
curl_multi_add_handle($mh, $container[$i]);
}
$running = null;
//execute the handles
do {
curl_multi_exec($mh, $running);
} while ($running > 0);
$response_1 = curl_multi_getcontent($container[1]);
//$response_2 = curl_multi_getcontent($ch2);
$infos0 = curl_getinfo($container[0]);
$infos1 = curl_getinfo($container[1]);
$infos2 = curl_getinfo($container[2]);
$infos3 = curl_getinfo($container[3]);
$infos4 = curl_getinfo($container[4]);
$infos5 = curl_getinfo($container[5]);
print_r($infos0);
echo '</br></br></br>';
print_r($infos1);
echo '</br></br></br>';
print_r($infos2);
echo '</br></br></br>';
print_r($infos3);
echo '</br></br></br>';
print_r($infos4);
echo '</br></br></br>';
print_r($infos5);
//close the handles
for ($i=0; $i<$count; $i++)
{
curl_multi_remove_handle($mh, $container[$i]);
}
curl_multi_close($mh);
}
这里的问题是,当我可视化信息时,我得到这个:
**Array ( [url] => HTTP://195.81.228.5 [content_type] => text/html; charset=utf-8 [http_code] => 200 [header_size] => 354 [request_size]
=> 53 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 1.138 [namelookup_time] => 0.998 [connect_time] =>
1.045 [pretransfer_time] => 1.045 [size_upload] => 0 [size_download]
=> 8387 [speed_download] => 7369 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => 0 [starttransfer_time] => 1.092 [redirect_time] => 0 [certinfo] => Array ( ) )
Array ( [url] => HTTP://195.81.228.17 [content_type] => text/html; charset=utf-8 [http_code] => 200 [header_size] => 354 [request_size]
=> 53 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 1.138 [namelookup_time] => 0.998 [connect_time] =>
1.045 [pretransfer_time] => 1.045 [size_upload] => 0 [size_download]
=> 8387 [speed_download] => 7369 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => 0 [starttransfer_time] => 1.092 [redirect_time] => 0 [certinfo] => Array ( ) )
Array ( [url] => HTTP://195.81.228.37 [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 22.136 [namelookup_time] => 0.998 [connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0 [certinfo] => Array ( ) )
Array ( [url] => HTTP://195.81.228.39 [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 22.136 [namelookup_time] => 0.998 [connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0 [certinfo] => Array ( ) )
Array ( [url] => HTTP://195.81.228.40 [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 22.136 [namelookup_time] => 0.998 [connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0 [certinfo] => Array ( ) )
Array ( [url] => HTTP://195.81.228.41 [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 22.136 [namelookup_time] => 0.998 [connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0 [certinfo] => Array ( ) )**
如你所见:在某些服务器中我不要获取时间信息(一切都设置为 0) 这段代码有什么问题吗?
Am making a curl request on an URL. this URL is hosted by many servers ==> many ip addresses.
Imagine "www.example.com" is hosted by
- 192.168.60.31
- 198.166.15.15
- 217.15.74.1
here is the test that i did:
public function curlInfosMulti($ips,$url, $timeout, $method, $params) {
$count= count($ips);
$container = array ();
$tab = array('Host:'.$url);
// set URL and other appropriate options
for ($i=0; $i<$count; $i++)
{
$container[$i] = curl_init();
curl_setopt($container[$i], CURLOPT_URL, $ips[$i]);
curl_setopt($container[$i], CURLOPT_HTTPHEADER, $tab);
curl_setopt($container[$i], CURLOPT_HEADER, 1);
curl_setopt($container[$i], CURLOPT_RETURNTRANSFER, true);
}
//create the multiple cURL handle
$mh = curl_multi_init();
for ($i=0; $i<$count; $i++)
{
curl_multi_add_handle($mh, $container[$i]);
}
$running = null;
//execute the handles
do {
curl_multi_exec($mh, $running);
} while ($running > 0);
$response_1 = curl_multi_getcontent($container[1]);
//$response_2 = curl_multi_getcontent($ch2);
$infos0 = curl_getinfo($container[0]);
$infos1 = curl_getinfo($container[1]);
$infos2 = curl_getinfo($container[2]);
$infos3 = curl_getinfo($container[3]);
$infos4 = curl_getinfo($container[4]);
$infos5 = curl_getinfo($container[5]);
print_r($infos0);
echo '</br></br></br>';
print_r($infos1);
echo '</br></br></br>';
print_r($infos2);
echo '</br></br></br>';
print_r($infos3);
echo '</br></br></br>';
print_r($infos4);
echo '</br></br></br>';
print_r($infos5);
//close the handles
for ($i=0; $i<$count; $i++)
{
curl_multi_remove_handle($mh, $container[$i]);
}
curl_multi_close($mh);
}
The PROBLEM here is that when i visualise the infos i get this :
**Array ( [url] => HTTP://195.81.228.5 [content_type] => text/html; charset=utf-8 [http_code] => 200 [header_size] => 354 [request_size]
=> 53 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 1.138 [namelookup_time] => 0.998 [connect_time] =>
1.045 [pretransfer_time] => 1.045 [size_upload] => 0 [size_download]
=> 8387 [speed_download] => 7369 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => 0 [starttransfer_time] => 1.092 [redirect_time] => 0 [certinfo] => Array ( ) )
Array ( [url] => HTTP://195.81.228.17 [content_type] => text/html; charset=utf-8 [http_code] => 200 [header_size] => 354 [request_size]
=> 53 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 1.138 [namelookup_time] => 0.998 [connect_time] =>
1.045 [pretransfer_time] => 1.045 [size_upload] => 0 [size_download]
=> 8387 [speed_download] => 7369 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => 0 [starttransfer_time] => 1.092 [redirect_time] => 0 [certinfo] => Array ( ) )
Array ( [url] => HTTP://195.81.228.37 [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 22.136 [namelookup_time] => 0.998 [connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0 [certinfo] => Array ( ) )
Array ( [url] => HTTP://195.81.228.39 [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 22.136 [namelookup_time] => 0.998 [connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0 [certinfo] => Array ( ) )
Array ( [url] => HTTP://195.81.228.40 [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 22.136 [namelookup_time] => 0.998 [connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0 [certinfo] => Array ( ) )
Array ( [url] => HTTP://195.81.228.41 [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 22.136 [namelookup_time] => 0.998 [connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0 [certinfo] => Array ( ) )**
as you see: in some servers i DON'T GET Time informations (everything is set to 0)
is there any problem with this code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将
CURLOPT_CONNECTTIMEOUT
设置为 0(无限期等待),看看是否有帮助。Try setting
CURLOPT_CONNECTTIMEOUT
to 0 (wait indefinitely) and see if it helps.