我想在php中修改套接字連接中的每個請求的IP地址(以防止主機在發送大量請求時阻止我的IP地址)Iam將我的代碼添加到下面php在套接字中更改請求的IP地址
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));
}
那麼什麼是這個功能的好替代? file_get_contents? 它支持重定向嗎? – 2010-06-20 21:04:23
如上所述,如果您想使用代理,請使用cURL或PECL的HttpRequest – 2010-06-20 21:24:29