从谷歌获取随机网址并将其存储到数组中

时间:2013-02-12 17:32:58

标签: php javascript

我需要检查一些网址的连接时间,我是用 curl 做的(我只知道如何在当时的1个网址上做这个,我的问题是我需要检查它适用于许多网址,而不仅仅是1)

以下是我如何计算连接时间的示例:

$url1 = 'stackoverflow.com';
$curl1 = curl_init();
curl_setopt($curl1, CURLOPT_URL, $url1);
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, TRUE);
curl_exec($curl1);
$contime1 = curl_getinfo($curl1, CURLINFO_CONNECT_TIME);
echo $contime1;

现在让我们说而不是'stackoverflow.com'网址我想使用多个网址,比如这个数组列表:

$imones = array ("php.net","w3schools.com", "stackoverflow.com");
while (list(, $value) = each($imones)) {
$imoneslist = "$value<br />\n";

如何将$ imoneslist数组放入$ url1?

1 个答案:

答案 0 :(得分:2)

$imones = array ("php.net","w3schools.com", "stackoverflow.com");
foreach($imones as $url1) {
    $curl1 = curl_init();
    curl_setopt($curl1, CURLOPT_URL, $url1);
    curl_setopt($curl1, CURLOPT_RETURNTRANSFER, TRUE);
    curl_exec($curl1);
    $contime1 = curl_getinfo($curl1, CURLINFO_CONNECT_TIME);
    echo $url1 . ' took ' . $contime1 . ' seconds to connect<br />';
}

测试。样本结果:

php.net took 0.032 seconds to connect 
w3schools.com took 0.124 seconds to connect 
stackoverflow.com took 0.016 seconds to connect