我有三個提交在我的mysql表中,它們是:id,url,狀態使用PHP檢查MySQL數據庫中URL的可用性?
如何檢查列url中的所有URL並將狀態列寫入1(可用)或0(不可用)?
要只檢查網址manualy在PHP W/O MySQL的,我可以用這個:
<?php
function Visit($url){
$agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch,CURLOPT_VERBOSE,false);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$page=curl_exec($ch);
//echo curl_error($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode >= 200 && $httpcode < 300){
return true;
}
else {
return false;
}
}
if(Visit("http://www.google.com")){//maybe make it a variable from a result of a mysql select, but how to process it one by one?
echo "Website OK"; //maybe somesql here to wtite '1'
}
else{
echo "Website DOWN";//maybe somesql here to wtite '0'
}
?>
有沒有需要寫'if(foo){return true;} else {return false; }'只要寫'return foo;'。 –
看起來你沒有考慮到你給出的答案。哦,你有什麼嘗試? –
可能你使用正則表達式嗎? :http://stackoverflow.com/questions/2490310/regular-expression-for-checking-website-url – Sergey