Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php url检查可用_Php_Url - Fatal编程技术网

php url检查可用

php url检查可用,php,url,Php,Url,我想检查我的数据库中是否有可用的url。我选择了fopen,但我从数据库中测试了30行,这将花费近20秒的时间。有没有办法,可以让它更有效?谢谢 <?php $start_t = microtime(true); //connect database and select query while ($row = mysql_fetch_array($result)){ //$url = 'http://www.google.com'; //not test from database,

我想检查我的数据库中是否有可用的url。我选择了
fopen
,但我从数据库中测试了30行,这将花费近20秒的时间。有没有办法,可以让它更有效?谢谢

<?php
$start_t = microtime(true); 
//connect database and select query
while ($row = mysql_fetch_array($result)){
//$url = 'http://www.google.com'; //not test from database, but a google.com, one url will cost 0.49 seconds.
$url = $row['url'];
$res = @fopen($url, "r "); 
if($res){
    echo $row['url'].' yes<br />';
}else{
    echo $row['url']. ' no<br />';
}   
}
$end_t = microtime(true);
$totaltime = $end_t-$start_t;
echo "<br />".$totaltime." s";
?>

你不能像那样加快速度

对于30行,我假设您正在连接30个不同的URL。20秒已经是一个很好的时间了


我还建议您使用
file\u get\u contents
检索HTML
或者如果需要了解标头响应,请使用
get_headers()

如果您想加快进程,只需生成更多进程。它们中的每一个都将获取一个
tot
url

补遗
也不要忘记伟大的
Zend_HTTP_Client()
这非常适合此类任务

您可以尝试将CURL与CURLOPT_NOBODY选项集一起使用,该选项集使用HTTP HEAD方法并避免下载整个页面:

$ch = curl_init($row['url']);

curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

// 400 means not found, 200 means found.
curl_close($ch);
从Curlu NOBODY:

如果为TRUE,则将主体从 输出。然后将Request方法设置为 头。将此更改为FALSE不起作用 把它换成一个


尝试使用
fsockopen
这比
fopen

<?php

$t = microtime(true);

$valid = @fsockopen("www.google.com", 80, $errno, $errstr, 30);

echo (microtime(true)-$t);

if (!$valid) {
   echo "Failure";
} else {
   echo "Success";
}
?>

尝试批量URL检查,即以10或20块为单位

Curl-Multi-Exec

只对NOBODY和HEADER使用CURL选项,这样您的响应会快得多

另外,不要忘记为curl设置超时,否则一个错误的url可能会花费太多时间

我在20秒内做了50次URL检查


希望对您有所帮助。

如果您想检查DNS是否正常或服务器是否在线,可以使用Rakesh提供的代码片段。要检查内容的可用性,您可以使用curl(参见karim79的答案)或get_headers()(参见yes123的答案)。@Rakesh,出人意料的速度。但两者都不是
http://www.google.com
nor
http://google.com
他们都返回了
失败
?@Yuli like@yes123我的错误。通常,在fsockopen调用中应该只使用主机名,而不是URL。您需要提供uri,减去实际HTTP头中的主机/端口。太好了。我还查看了
php.net/manual/en/function.fsockopen.php
,移动
http头
。谢谢,谢谢。这将节省20%的时间。但是,不仅
400
200
返回,还可以查看
302
等。不像
get\u headers
这样的
文件获取内容更容易,成本更高,尝试了
http://www.google.com
,花费
1+秒
。从未说过一门新课花费更少的钱,但它会花费10秒。
0.0013298988342285