Tom: Sinnvolle Codes für Prüfung einer URL auf ihre Existenz

Beitrag lesen

Hello,

Welche Codes sollten sinnvollerweise abgefragt werden ?

wie definierst Du "Existenz"?

Ich würde nur einen HTTP-Header mit HEAD senden und die Antwort dann auswerten.
Den Inhalt willst Du doch wahrscheinlich bei der Kontrolle gar nicht haben, oder?

Wir haben da mal im Team was gebastelt, muss aber zugeben, dass wir es nicht so toll dokumentiert haben. Man nuss sich also erst wieder reindecken.

<?php

#--------------------------------------------------------------------
function phpLinkCheck($url, $r = FALSE)
{
  $url = trim($url);

if (!preg_match("=://=", $url))
  {
    $url = "http://$url";
  }

$url = parse_url($url);

if (strtolower($url["scheme"]) != "http")
  {
    return FALSE;
  }

if (!isset($url["port"])) $url["port"] = 80;
  if (!isset($url["path"])) $url["path"] = "/";

$fp = fsockopen($url["host"], $url["port"], &$errno, &$errstr, 30);

if (!$fp)
  {
    return FALSE;
  }
  else
  {
    $httpRequest = "HEAD ". $url["path"] ." HTTP/1.1\r\n"
                  ."Host: ". $url["host"] ."\r\n"
                  ."Connection: close\r\n\r\n";

fputs($fp, $httpRequest);

$head = "";
    while(!feof($fp)) $head .= fgets($fp, 1024);

fclose($fp);
    preg_match("=^(HTTP/\d+.\d+) (\d{3}) ([^\r\n]*)=", $head, $matches);

$http["Status-Line"] = $matches[0];
    $http["HTTP-Version"] = $matches[1];
    $http["Status-Code"] = $matches[2];
    $http["Reason-Phrase"] = $matches[3];

if ($r) return $http["Status-Code"];

$rclass = array("Informational",
                    "Success",
                    "Redirection",
                    "Client Error",
                    "Server Error");

$http["Response-Class"] = $rclass[$http["Status-Code"][0] - 1];

preg_match_all("=^(.+): ([^\r\n]*)=m", $head, $matches, PREG_SET_ORDER);

foreach($matches as $key => $line)
    {
      $errlist[] = $line;
      $http[$line[1]] = $line[2];
    }

if ($http["Status-Code"][0] == 3)
    {
      $http["Location-Status-Code"] = phpLinkCheck($http["Location"], TRUE);
    }

return $http;
  }
}
#--------------------------------------------------------------------

$var = phpLinkCheck("http://peeties.de/",false);

print "Status-Code: ".$var["Status-Code"];

echo "<pre>";
print_r($var);
echo "</pre>";

?>

Aber es geht jedenfalls darum, dass man nur direkt die angegebne Adresse ($r = true) abfragen kann oder aber auch eine Weiterleitung ($r = false).

Harzliche Grüße aus http://www.annerschbarrich.de

Tom

--
Fortschritt entsteht nur durch die Auseinandersetzung der Kreativen
Nur selber lernen macht schlau