PHP Tutorials Mimic user access to Web page program code
function http ($url, $followredirects =true) {
Global $final _url;
$url _parsed = Parse_url ($url);
if (Empty ($url _parsed[' scheme ')) {
$url _parsed = Parse_url (' http://'. $url);
}
$final _url = $url _parsed;
$port = $url _parsed["Port";
if (! $port) {
$port = 80;
}
$rtn [' url '] [' port '] = $port;
$path = $url _parsed["path");
if (empty ($path)) {
$path = "/";
}
if (!empty ($url _parsed["Query")) {
$path. = "?". $url _parsed["Query"];
}
$rtn [' url '] [' path '] = $path;
$host = $url _parsed["host"];
$foundbody = false;
$out = "Get $path http/1.0rn";
$out. = "Host: $hostrn";
$out. = "user-agent:mozilla/5.0 (Windows; U; Windows NT 5.1; En; rv:1.8.1) gecko/20061010 Firefox/2.0rn ";
$out. = "Connection:closernrn";
if (! $fp = @fsockopen ($host, $port, $errno, $errstr, 30)) {
$rtn [' errornumber '] = $errno;
$rtn [' errorstring '] = $errstr;
}
fwrite ($fp, $out);
while (! @feof ($fp)) {
$s = @fgets ($fp, 128);
if ($s = = "rn") {
$foundbody = true;
re-enters;
}
if ($foundbody) {
$body. = $s;
} else {
if (($followredirects) && (Stristr ($s, "location:")!= false) {
$redirect = preg_replace ("/location:/i", "", $s);
return http (Trim ($redirect));
}
$header. = $s;
}
}
fclose ($FP);
return (Trim ($body));