要通过/通过不需要身份验证的代理使用file_get_contents(),应该这样做:
(我无法测试这个:我的代理需要身份验证)
$aContext = array(
'http' => array(
'proxy' => 'tcp://192.168.0.2:3128',
'request_fulluri' => true,
),
);
$cxContext = stream_context_create($aContext);
$sFile = file_get_contents("http://www.google.com", False, $cxContext);
echo $sFile;
当然,将我代理的 IP 和端口替换为适合您的代理的 IP 和端口 ;-)
如果您遇到这种错误:
Warning: file_get_contents(http://www.google.com) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 407 Proxy Authentication Required
这意味着您的代理需要身份验证。
如果代理需要身份验证,您必须添加几行,如下所示:
$auth = base64_encode('LOGIN:PASSWORD');
$aContext = array(
'http' => array(
'proxy' => 'tcp://192.168.0.2:3128',
'request_fulluri' => true,
'header' => "Proxy-Authorization: Basic $auth",
),
);
$cxContext = stream_context_create($aContext);
$sFile = file_get_contents("http://www.google.com", False, $cxContext);
echo $sFile;
IP 和端口也一样,而且这次还有 LOGIN 和 PASSWORD ;-) 检查所有有效的http options。
现在,您将 Proxy-Authorization 标头传递给代理,其中包含您的登录名和密码。
并且...应该显示该页面;-)