note 36610 modified in function.fsockopen by et

From: Date: Sat, 06 Mar 2004 14:27:58 +0000
Subject: note 36610 modified in function.fsockopen by et
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-66344@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). --was-- 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 http://www.php.net/manual/en/function.fsockopen.php

« previous php.notes (#66344) next »