note 36610 added to function.fsockopen
| From: | webmaster at elcurriculum dot com | Date: | Thu, 16 Oct 2003 07:41:23 +0000 |
| Subject: | note 36610 added to function.fsockopen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-58679@lists.php.net to get a copy of this message | ||
This function get HTML source from url. Follow all locations to last site. Ideal for Search Engine.
$url = web site to explore.
$delta = last url from location
$corto = if corto is true stop function when the tag is <body>. (This is for get metatags and
title).
$complet = if complet is true return all body of page. Else return only $delta. (This is for get
redirect from image: <img src=http://www.solobanner.com/index.php>)
function GetHTML ($url, &$delta, $corto = false, $complet = true) {
$url_stuff = parse_url($url);
$fp = fsockopen ($url_stuff['host'], 80, $errno, $errstr, 30);
if (!$fp) {
exit;
} else {
$header = "GET " . $url_stuff['path'] . "?" .
$url_stuff['query'] ;
$header = $header . " HTTP/1.0\r\nHost: " . $url_stuff['host'] .
"\r\n\r\n";
fputs ($fp, $header);
//Separar contenido...
$header = '';
$body = '';
$act = false;
$fin = false;
while ((!feof($fp)) && !$fin) {
$line = fgets ($fp,1024);
if (!$act) {
if (strpos($line, "\r\n", 0) == 0) {
$header .= $line;
if (!$complet) $fin = true;
$act = true;
} else {
$header .= $line;
}
} else {
if ($corto) {
if (eregi ("<body([^>]*)>", $line, $o)) $fin = true;
}
if (!$fin) $body = $body . $line;
}
}
//Seguir location...
$ret = strpos($header, "Location:", 0);
if ($ret !== false) {
$fin = strpos($header, "\r\n", $ret +9);
$nueva = substr($header, $ret+9, $fin - $ret - 9);
$body = GetHTML($nueva, $delta, $corto, $complet);
} else {
$delta = $url;
}
fclose ($fp);
}
return $body;
}
Example:
$url = "http://www.elcurriculum.com/banner/go.php?from=1&to26";
GetHTML($url,$a,true);
echo "<br>Go to url: $a";
By Tryke. (Jose María Rodríguez Valls).
http://www.elcurriculum.com
http://www.solobanners.com
----
Manual Page -- http://www.php.net/manual/en/function.fsockopen.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+36610
Delete -- http://master.php.net/manage/user-notes.php?action=delete+36610&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+36610&report=yes
Search -- http://master.php.net/manage/user-notes.php