首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何让PHP使用代理设置来连接到互联网?

如何让PHP使用代理设置来连接到互联网?
EN

Stack Overflow用户
提问于 2010-10-09 00:48:55
回答 7查看 94.6K关注 0票数 32

我在一个不允许直接连接到互联网的代理服务器后面。我所有的PHP应用程序都无法连接到互联网进行更新检查等。

我如何告诉PHP我的代理设置?

我不想在代码中输入代理设置,我希望PHP自己通过全局配置设置或类似的设置来使用它。

EN

回答 7

Stack Overflow用户

发布于 2014-09-16 17:54:50

如果你们几乎所有人都需要一个代理服务器,我更喜欢这样做。

代码语言:javascript
复制
//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

here is a official document.

票数 18
EN

Stack Overflow用户

发布于 2011-03-23 18:16:31

这取决于PHP应用程序连接到internet的方式。

如果采取最有可能的情况使用PHP cUrl。在这种情况下,以下选项将对您有所帮助:

代码语言:javascript
复制
curl_setopt($handle, CURLOPT_PROXY, $proxy_url); 
curl_setopt($handle, CURLOPT_PROXYUSERPWD, "[username]:[password]"); 

另请参阅:http://www.php.net/manual/en/function.curl-setopt.php

票数 11
EN

Stack Overflow用户

发布于 2012-07-13 18:26:18

示例代码:

代码语言:javascript
复制
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");
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3892635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档