Published under the terms of the BSD License
<?php
function html_get($server, $path, $referer = false, $ua = false) {
$response="";
$ip = @gethostbyname($server);
if($ip==$server){ return false; }
else {
$fp = fsockopen($ip, 80, $errno, $errstr, 30);
if (!$fp) {
echo "($errstr) ";
return false;
}
else {
fputs($fp, "GET $path HTTP/1.1\r\n");
fputs($fp, "Host: $server\r\n");
if($referer !== false) { fputs($fp, "Referer: $referer\r\n"); }
if($ua !== false) { fputs($fp, "User-Agent: $ua\r\n"); }
fputs($fp, "Connection: close\r\n");
fputs($fp, "\r\n");
$response .= fread($fp, 1);
while (!feof($fp)) {
$response .= fread($fp,1024);
}
}
fclose ($fp);
}
$temp = explode("\r\n", substr($response, 0, (strpos($response, "\r\n\r\n"))));
foreach($temp as $header) {
$field = substr($header, 0, strpos($header, ": "));
$value = substr($header, strpos($header, ": ")+2);
if($field=="") { $field = 0; $value = "HT".$value; }
$headers[strtolower($field)] = $value;
}
$body = substr($response, (strpos($response, "\r\n\r\n")+4));
if (!in_array(substr($headers[0], 9, 3), array("200", "302"))) {
echo "(errorcode: ".substr($headers[0], 9, 3).") ";
return false;
}
else {
return array('body' => $body,'headers' => $headers);
}
}
?>
Last updated: Wed Sep 16 00:20:43 CEST 2009