Created
April 27, 2014 02:11
-
-
Save xombra/11336106 to your computer and use it in GitHub Desktop.
Extraer código HTML de un sitio web
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function get_source($url,$quitar,$show_headers=0) | |
{ if(preg_match('!^http://!',$url)) | |
$url=substr($url,7,strlen($url)); | |
if($start=strpos($url,'/')) $uri=substr($url,$start,strlen($url)); | |
else $uri=''; | |
$fp= @fsockopen($url,80,$errno,$errstr,4); | |
if(!$fp) | |
{ echo "Imposible conectar $url o no ha indicado URL"; | |
return false; | |
} | |
else | |
{ $headers=$buffer=''; | |
fputs($fp,"GET /$uri HTTP/1.0\r\n"); | |
fputs($fp,"Host: $url\r\n"); | |
fputs($fp,"User-Agent: sourcegetter\r\n"); | |
fputs($fp,"Connection: close\r\n"); | |
fputs($fp,"\r\n"); | |
$buscar = array("<br />", "<br>"); | |
while(!feof($fp)) | |
{ if(!isset($end_of_headers)) | |
{ $header=fgets($fp,4096); | |
if($header=="\r\n") | |
$end_of_headers=1; | |
$headers.=$header; } else { | |
if (!empty($quitar)) { | |
$buffer.= strip_tags(fgets($fp,4096)); } | |
else { $buffer.=fgets($fp,4096); } } | |
} | |
fclose($fp); | |
if($show_headers) | |
{ $headers=htmlentities($headers); | |
$headers=nl2br($headers); | |
$headers = str_ireplace($buscar," ",$headers); | |
echo $headers; } | |
$buffer=htmlentities($buffer); | |
$buffer=nl2br($buffer); | |
$buffer = str_ireplace($buscar," ",$buffer); | |
echo $buffer; | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
HTML