我在一个不允许直接连接到互联网的代理服务器后面。我所有的PHP应用程序都无法连接到互联网进行更新检查等。
我如何告诉PHP我的代理设置?
我不想在代码中输入代理设置,我希望PHP自己通过全局配置设置或类似的设置来使用它。
发布于 2014-09-16 17:54:50
如果你们几乎所有人都需要一个代理服务器,我更喜欢这样做。
//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
发布于 2011-03-23 18:16:31
这取决于PHP应用程序连接到internet的方式。
如果采取最有可能的情况使用PHP cUrl。在这种情况下,以下选项将对您有所帮助:
curl_setopt($handle, CURLOPT_PROXY, $proxy_url);
curl_setopt($handle, CURLOPT_PROXYUSERPWD, "[username]:[password]");
发布于 2012-07-13 18:26:18
示例代码:
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");
https://stackoverflow.com/questions/3892635
复制相似问题