function httpGet($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.0\r\n";
$out .= "Host: $host\r\n";
$out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0\r\n";
$out .= "Connection: Close\r\n\r\n";
if (!$fp = @fsockopen($host, $port, $errno, $errstr, 30)) {
$rtn['errornumber'] = $errno;
$rtn['errorstring'] = $errstr;
}
fwrite($fp, $out);
while ([email protected]($fp)) {
$s = @fgets($fp, 128);
if ($s == "\r\n") {
$foundBody = true;
continue;
}
if ($foundBody) {
$body .= $s;
} else {
if (($followRedirects) && (stristr($s, "location:") != false)) {
$redirect = preg_replace("/location:/i", "", $s);
return httpGet(trim($redirect));
}
$header .= $s;
}
}
fclose($fp);
return(trim($body));
}
此代碼有時會發生無限循環。這裏有什麼問題?PHP Infine Loop問題
發佈一個巨大的代碼塊可能不會給你任何幫助。減少顯示問題的代碼量是最好的。 – 2010-05-26 14:36:42
這個函數獲取一個URL的html也做重定向。 有人有更好的功能,這樣做? file_get_contents與重定向? – 2010-05-26 14:42:58