(HTTP üzerinden erişilen) uzaktaki herhangi bir dosya varsa PHP, nasıl belirleyebilir?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); //follow up to 10 redirections - avoids loops
$data = curl_exec($ch);
curl_close($ch);
if (!$data) {
echo "Domain could not be found";
}
else {
preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/",$data,$matches);
$code = end($matches[1]);
if ($code == 200) {
echo "Page Found";
}
elseif ($code == 404) {
echo "Page Not Found";
}
}
Kod değiştirilmiş versiyonu here.
Bu fonksiyon yanıt kodu (yönlendirme durumunda sonuncusu), ya da bir DNS veya başka bir hata durumunda false dönecektir. Bir argüman (url) verilirse bir HEAD isteği yapılır. İkinci argüman verilirse varsa, tam bir istek içerik yanıtı ikinci argüman olarak geçirilen değişken referans tarafından saklanır arasında yapılmış ve.
function url_response_code($url, & $contents = null)
{
$context = null;
if (func_num_args() == 1) {
$context = stream_context_create(array('http' => array('method' => 'HEAD')));
}
$contents = @file_get_contents($url, null, $context);
$code = false;
if (isset($http_response_header)) {
foreach ($http_response_header as $header) {
if (strpos($header, 'HTTP/') === 0) {
list(, $code) = explode(' ', $header);
}
}
}
return $code;
}
Geçenlerde aynı bilgi arıyordu. Burada bazı gerçekten güzel kod bulundu: http://php.assistprogramming.com/check-website-status-using-php-and-curl-library.html
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.site.com")){
echo "Website OK";
}
else{
echo "Website DOWN";
}
Bu çözümler bulunmayan bir sayfa için uygun bir yanıt vermez bir sitede çalışmaz sadece bir not. onu idare edemez bir istek aldığında sadece bir ana site sayfası yükler gibi, örneğin sadece bir sitede bir sayfa için test ile ilgili bir sorun vardı. Yani site neredeyse her zaman, hatta varolmayan sayfalar için 200 yanıt verecektir.
Bazı siteler bir standart sayfasında özel bir hata vermek ve hala bir 404 başlık vermeyecektir olmaz.
Eğer sayfanın beklenen içeriğini bilmek ve beklenen içerik var veya bazı beklenen hata sayfasında metin ve tüm biraz dağınık oluyor test olduğunu test başlatmak sürece çok değil bu durumlarda yapabilirim ...