Linkcheck
Tom1tk
- php
0 Tom0 Tom1tk0 Tom0 Tom0 Tom0 Thomas Luethi0 Tom0 Thomas Luethi0 Tom
0 Linkcheck, funktionstüchtige Version
Tom
Hallo zusammen!
Warum gibt die Funktion nix aus???
<?php
function phpLinkCheck($url, $r = FALSE)
{
/* Purpose: Check HTTP Links
* Usage: $var = phpLinkCheck(absoluteURI)
* $var["Status-Code"] will return the HTTP status code
* (e.g. 200 or 404). In case of a 3xx code (redirection)
* $var["Location-Status-Code"] will contain the status
* code of the new loaction.
* See print_r($var) for the complete result
*
* Author: Johannes Froemter j-f@gmx.net
* Date: 2001-04-14
* Version: 0.1 (currently requires PHP4)
*/
$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
{
$head = "";
$httpRequest = "HEAD ". $url["path"] ." HTTP/1.1\r\n"
."Host: ". $url["host"] ."\r\n"
."Connection: close\r\n\r\n";
fputs($fp, $httpRequest);
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 $line) $http[$line[1]] = $line[2];
if ($http["Status-Code"][0] == 3)
$http["Location-Status-Code"] = phpLinkCheck($http["Location"], TRUE);
return $http;
}
}
############Ende Funktion
$var = phpLinkCheck("http://forum.de.selfhtml.org/");
print $var[Status-Code];
?>
Hat jemand ne Idee??
Danke Tom1tk
Hello,
Hallo zusammen!
Warum gibt die Funktion nix aus???
<?php
function phpLinkCheck($url, $r = FALSE)
{
/* Purpose: Check HTTP Links
* Usage: $var = phpLinkCheck(absoluteURI)
* $var["Status-Code"] will return the HTTP status code
* (e.g. 200 or 404). In case of a 3xx code (redirection)
* $var["Location-Status-Code"] will contain the status
* code of the new loaction.
* See print_r($var) for the complete result
*
* Author: Johannes Froemter j-f@gmx.net
* Date: 2001-04-14
* Version: 0.1 (currently requires PHP4)
*/$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
{$head = "";
$httpRequest = "HEAD ". $url["path"] ." HTTP/1.1\r\n"
."Host: ". $url["host"] ."\r\n"
."Connection: close\r\n\r\n";
fputs($fp, $httpRequest);
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 $line) $http[$line[1]] = $line[2];if ($http["Status-Code"][0] == 3)
$http["Location-Status-Code"] = phpLinkCheck($http["Location"], TRUE);return $http;
}
}
############Ende Funktion$var = phpLinkCheck("http://forum.de.selfhtml.org/");
print $var[Status-Code];?>
Hat jemand ne Idee??
Sind bestimmt noch mehr Fehler drin.
Liebe Grüße aus http://www.braunschweig.de
Tom
Hallo nochmals,
$var = phpLinkCheck("http://forum.de.selfhtml.org/");
print $var[Status-Code];
Das klappt bei mir nicht!?!?!?!
Normlerweise sollte da jetzt der Status-Code rauskommen (z.B."200")
Vielleicht kanns jemand mal auf seinem Server testen. Ich bau da jetzt schon seit 3 Tagen dran rum und bekomms nicht hin. Mittlerweile bin ich ja schon wieder "backToTheRoots" gegangen und hab nur die Fúnktion aufgerufen, ohne Abfrage-Schleifen usw. Es geht trotzdem nicht.
Thanx Tom1tk
Hello,
ich hab mal ein paar Checks eingebaut und dir das soweit ins mail gegeben. Wird hier sonst zu fett. Das Ergebnis kann dann ja hier wieder erscheinen.
Der Fehler scheint nun noch im letzten Abschnitt der Funktion zu liegen...
Liebe Grüße aus http://www.braunschweig.de
Tom
Hello,
hier steckt der Fehler:
preg_match_all("=^(.+): ([^\r\n]*)=m", $head, $matches, PREG_SET_ORDER);
foreach($matches as $line)
{
# $errlist[] = $line; ## zum Debug
$http[$line[1]] = $line[2];
}
Was ist das Ziel der foreach()-Schleife?
Liebe Grüße aus http://www.braunschweig.de
Tom
Hello Tom und Mitleser,
ich habe alle Fehler gefunden.
Einer der blödesten steckt wohl in dieser Zeile:
print $var[Status-Code];
Es müsste heißen
print $var["Status-Code"];
Und dann klappts auch. Ich bau nun noch die Debug-Zeilen wirder raus und dann gibts das Ergebnis hier...
Liebe Grüße aus http://www.braunschweig.de
Tom
Hallo Tom,
Dass Du diesen Fehler nicht schon laengst von
blossem Auge entdeckt hast, verwundert mich
ein wenig. ;-)
print $var[Status-Code];
print $var["Status-Code"];
=> Warum ist $foo[bar] falsch?
http://www.php.net/manual/de/language.types.array.php#language.types.array.foo-bar
Ich verstehe nicht, warum Ihr hier an einer offensichtlich
korrupten Kopie rumbastelt, wenn das perfekte Original
so nahe liegt...
http://www.dclp-faq.de/q/q-code-links-testen.html
=> http://froemter.de/php/phpLinkCheck.phps
Gruesse,
Thomas
der dieses Skript selbst erfolgreich in Betrieb hat
Hello,
Dass Du diesen Fehler nicht schon laengst von
blossem Auge entdeckt hast, verwundert mich
ein wenig. ;-)print $var[Status-Code];
print $var["Status-Code"];
Na, man muss sich in fremden Code erst reindenken oder rein-debuggen. Wenn man nachher geschrieben sieht, woran es lag, ist es immer leicht zu sagen, wieso habt Ihr das nicht gleich gesehen?
Ich verstehe nicht, warum Ihr hier an einer offensichtlich
korrupten Kopie rumbastelt, wenn das perfekte Original
so nahe liegt...http://www.dclp-faq.de/q/q-code-links-testen.html
=> http://froemter.de/php/phpLinkCheck.phps
Bist Du sicher, dass das läuft?
Ich habe noch einen Fehler bei foreach() gefunden...
Meine Version von [pref:t=67177&m=384165] funktioniert wirklich.
Liebe Grüße aus http://www.braunschweig.de
Tom
Hallo,
=> http://froemter.de/php/phpLinkCheck.phps
Bist Du sicher, dass das läuft?
Ja.
Habe (vorhin) den Code mit Copy-Paste in Phase 5 kopiert.
Die zahlreichen unerklaerlichen Fehlermeldungen
kann man wegzaubern, wenn man die bloeden geschuetzten
Leerzeichen (Alt-0160) durch normale (32) ersetzt.
Dann habe ich unter die Funktion geschrieben:
$url="http://www.tiptom.ch/";
$resultat=phpLinkCheck($url);
$status=$resultat["Status-Line"];
print("<p>Linkcheck auf $url: Status-Line: ($status)</p>");
Output:
Linkcheck auf http://www.tiptom.ch/: Status-Line: (HTTP/1.1 200 OK)
Ich habe noch einen Fehler bei foreach() gefunden...
Was meinst Du? Wohl folgende Zeile:
foreach($matches as $line) $http[$line[1]] = $line[2];
Ich seh gerade nicht, was daran falsch sein soll.
Bitte erklaer es mir!
Und wenn es denn wirklich ein Fehler ist, dann melde
ihn bitte auch Johannes Froemter.
Meine Version von [pref:t=67177&m=384165] funktioniert wirklich.
Schoen! ;-)
Gruesse,
Thomas
Hello,
Ich habe noch einen Fehler bei foreach() gefunden...
Was meinst Du? Wohl folgende Zeile:
foreach($matches as $line) $http[$line[1]] = $line[2];
Ich seh gerade nicht, was daran falsch sein soll.
Bitte erklaer es mir!
Das Array hat eine Ebene mehr und daher muss es heißen
foreach($matches as $blah => $line)
Und wenn es denn wirklich ein Fehler ist, dann melde
ihn bitte auch Johannes Froemter.
Nö, habe ich gerade keine Lust. Mach Du mal bitte.
Liebe Grüße aus http://www.braunschweig.de
Tom
Hello zusammen,
hier die funktionstüchtige Version:
<?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://forum.de.selfhtml.org/",false);
print "Status-Code: ".$var["Status-Code"];
echo "<pre>";
print_r($var);
echo "</pre>";
?>
Liebe Grüße aus http://www.braunschweig.de
Tom