如何让PHP使用代理设置连接到互联网?
我背后的代理服务器,不允许直接连接到互联网。我所有的PHP应用程序都无法连接到互联网进行更新检查等。如何让PHP使用代理设置连接到互联网?
如何告诉PHP我的代理设置?
我不想在代码中输入代理设置,我想PHP本身通过全局配置设置或类似的东西来使用它。
这取决于您的PHP应用程序如何连接到互联网。
如果使用PHP cUrl最可能的情况。在这种情况下,下列选项将帮助您:
curl_setopt($handle, CURLOPT_PROXY, $proxy_url);
curl_setopt($handle, CURLOPT_PROXYUSERPWD, "[username]:[password]");
我使用PEAR HTTP_Request2模块。
这里是我的urlopen()函数的一个简化版本:
function UrlOpen($url)
{
$request = new HTTP_Request2($url);
$request->setConfig(array(
'proxy_host' => '192.168.1.6',
'proxy_port' => 8080,
'proxy_user' => 'MYUSER',
'proxy_password' => 'MYPASS',
'ssl_verify_peer' => False,
'connect_timeout' => 3,
);
return $request;
}
$req = UrlOpen($url);
$res = $req->send();
if ($res->getStatus() == '200')
$data = $request->getBody();
示例代码:
function getUrl($url)
{
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, "http://proxy.example.com"); //your proxy url
curl_setopt($ch, CURLOPT_PROXYPORT, "8080"); // your proxy port number
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "username:pass"); //username:pass
$file_contents = curl_exec($ch);
curl_close($ch);
return $file_contents;
}
echo getUrl("http://www.google.com");
如果几乎所有的你上网需要一个代理,我宁愿做这样的。
//add this as the first line of the entry file may it is the index.php or config.php
stream_context_set_default(['http'=>['proxy'=>'proxy-host:proxy-port']]);
代理,才能既为工作的file_get_contents和curl_exec
这对我很好。 – 2014-12-11 21:12:36
它不适用于CURL – 2015-10-01 12:11:55
对于我的错误感到抱歉,流api不会影响curl,如果您使用curl,则最好通过curl_setopt($ handle,CURLOPT_PROXY,$ proxy_url);'if你同时使用,你可以添加两个。 – 2016-08-24 01:49:20
对于一些脚本,所有你需要做的就是设置环境变量HTTP_PROXY
。 composer.phar
和media-wiki的维护/ update.php
脚本都是这种情况。
例如为:
setenv HTTP_PROXY http://1.2.3.4:3934
如果您的代理是1.2.3.4侦听端口3934为我工作。
是的,这是可能的!
您可以在文件中配置stream_context_set_default
,并使用auto_prepend_file
php.ini属性将此文件包含在您的所有Php程序中。
我写了一个小要点有关:
https://gist.github.com/ebuildy/381f116e9cd18216a69188ce0230708d
和法文的文章:
这种技术是 “酷”,因为这可以让你的SYS-管理员配置主机,因此开发人员不必更改代码中的任何内容。
我搜索在互联网上,关于stream_context_set_default()与密码保护代理服务器无法找到任何东西。
然后我认为在基本授权中的密码是在标头中发送的。 所以我修改了标题,并从CURL请求中提取了密码,它的工作非常完美!
这里是你如何做到这一点:
首先该请求发送给任何域(example.com)如下:
curl -v -U user:pass -x your_proxy_ip:port --url https://example.com
见卷曲发送了头,我得到了这些代理线以后使用:现在
> Trying XXX.XXX.XXX.XXX...
> Connected to XXX.XXX.XXX.XXX (XXX.XXX.XXX.XXX) port YYYY (#0)
> Establish HTTP proxy tunnel to example.com:443
> Proxy auth using Basic with user 'your_username_here'
> CONNECT example.com:443 HTTP/1.1
> Host: example.com:443
> Proxy-Authorization: Basic YW1hem9uOnXXXXXXXXXXXXXXX25SMg
> User-Agent: curl/7.47.0
> Proxy-Connection: Keep-Alive
>
< HTTP/1.1 200 Connection established
<
< Proxy replied OK to CONNECT request
OK,是时候建立我们自定义标题:
$default_opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Proxy-Authorization: Basic YW1hem9uOnXXXXXXXXXXXXXXX25SMg\r\n" .
"Proxy-Connection: Keep-Alive",
'proxy'=>"XXX.XXX.XXX.XXX:YYYY"
)
);
$default = stream_context_set_default($default_opts);
$result = file_get_contents("https://ipleak.net/json/");
print_r(json_decode($result));
它的工作原理非常完美,您可以获得代理服务器的IP作为响应!
感谢您的回答。但考虑到这可能仅适用于我自己的代码。其他PHP应用程序如Drupal呢?我需要PHP本身通过代理连接,而不是通过代码。 – Alexar 2011-03-24 10:00:06