【发布时间】:2015-07-10 20:44:24
【问题描述】:
是否可以使用用户当前的 IP 地址作为 PHP cURL 的代理地址?目前,我正在尝试以下代码,但给出请求超时错误。我使用 hma 代理对其进行测试,它工作正常,但是当我尝试使用用户的 IP 地址时,它会在服务器日志中给出请求时间错误。
function get_page($url){
$proxy=$_SERVER['REMOTE_ADDR'].":80";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_HEADER, 0); // return headers 0 no 1 yes
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return page 1:yes
curl_setopt($ch, CURLOPT_TIMEOUT, 40); // http request timeout 20 seconds
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Follow redirects, need this if the url changes
curl_setopt($ch, CURLOPT_MAXREDIRS, 2); //if http server gives redirection responce
curl_setopt($ch, CURLOPT_USERAGENT,
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt"); // cookies storage / here the changes have been made
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // false for https
curl_setopt($ch, CURLOPT_ENCODING, "gzip"); // the page encoding
$data = curl_exec($ch); // execute the http request
curl_close($ch); // close the connection
return $data;
}
有很多活生生的例子都在做同样的事情。例如谷歌的这两个工具
- google.com/gwt/x
- developers.google.com/speed/pagespeed/insights
【问题讨论】:
-
您为什么要这样做?您假设在用户机器的端口 80 上存在一个 http 代理。
-
这就是问题所在,我无法决定。我知道这是可能的,但我怎么不知道。
-
最好了解您在此处尝试实现的目标的背景信息,以便我们可以提供替代选项。
-
您确定远程地址的 80 端口上运行了代理服务器吗?端口 80 是 Web 服务器的默认端口,而不是代理
-
你不能只使用某人作为代理,必须有一个代理服务器运行在你在 CURLOPT_PROXY 中指定的地址