要使用不需要身份认证的代理服务器使用
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地址和端口,而且还需要使用登录名和密码;-)查看所有有效的http选项。
现在,您正在向代理传递一个包含您的登录和密码的Proxy-Authorization头。
然后...页面应该显示出来了 ;-)
tcp
作为协议也是必要的。之前我使用了http
,导致出现错误:failed to open stream: Unable to find the socket transport "http" - did you forget to enable it when you configured PHP? - robsch