在Linux上的PHP:如何使PHP使用代理设置连接到互联网?

我的服务器是在一个局域网中,并通过代理服务器连接到互联网。 我的PHP应用程序(Drupal)应该能够通过PHP代码检查更新。 我如何设置 PHP的 代理设置 ,使其正常工作?

注意 :我知道我可以设置curl或其他的代理设置。 但情况是,代码是不是我的,我不想改变它。 我只是想让PHP总是通过特定的代理设置连接到互联网。

这个function在php中没有实现,但是在讨论中:
http://bugs.php.net/bug.php?id=29280

如果您有php> = 5.3,则可以使用auto_prepend ini设置来包含使用stream_context_set_default()设置默认代理的脚本。 但这只会为使用默认上下文的PHP的部分设置代理,没有testing它,但我认为curl的例子doesent。

你有没有尝试过设置环境variables?

http_proxy = http://proxy.example.com:8080

我不知道你的发行版,但是在SUSE中,你可以编辑文件/etc/sysconfig/proxy并input一个或多个代理服务器以供系统使用。

这里是一个例子:

 ## Path: Network/Proxy ## Description: ## Type: yesno ## Default: no ## Config: kde,profiles # # Enable a generation of the proxy settings to the profile. # This setting allows to turn the proxy on and off while # preserving the particular proxy setup. # PROXY_ENABLED="yes" ## Type: string ## Default: "" # # Some programs (eg lynx, arena and wget) support proxies, if set in # the environment. SuSEconfig can add these environment variables to # /etc/SuSEconfig/* (sourced by /etc/profile etc.) - # See http://portal.suse.com/sdb/en/1998/01/lynx_proxy.html for more details. # Example: HTTP_PROXY="http://proxy.provider.de:3128/" HTTP_PROXY="http://your.proxy.here:3128" ## Type: string ## Default: "" # # Some programs (eg lynx, arena and wget) support proxies, if set in # the environment. SuSEconfig can add these environment variables to # /etc/SuSEconfig/* (sourced by /etc/profile etc.) - # this setting is for https connections HTTPS_PROXY="http://your.proxy.here:3128" ## Type: string ## Default: "" # # Example: FTP_PROXY="http://proxy.provider.de:3128/" # FTP_PROXY="http://your.proxy.here:3128" ## Type: string ## Default: "" # # Example: GOPHER_PROXY="http://proxy.provider.de:3128/" # GOPHER_PROXY="" ## Type: string(localhost) ## Default: localhost # # Example: NO_PROXY="www.me.de, do.main, localhost" # NO_PROXY="localhost, 127.0.0.1" 

如果您的服务器连接方式只能通过代理发送http请求,则不需要进行任何更改。 但是,您将需要确保您有一种方法来validation您的http请求是通过代理进行的。

另外请注意 – 您的代理可能不会处理所有请求 – 只需http / https。 stream量可能会通过代理,但是如果代理没有configuration为处理这些代理,那么它们可能会经过而没有太多的跟踪。

如果您认为这可能会发生,我会加强PHP服务器上的防火墙,以便它只能发送指定种类的stream量到指定的位置。 然后,您应该能够看到失败的情况并采取适当的行动。

你还需要信息吗?

无论如何 – logging:一个长期悬而未决的问题,这是在Drupal http://drupal.org/node/7881 – 真的很奇怪。 – 还有更多

我修补了我的核心代码,但是必须跟踪升级时的修补程序更改,这不是很好。

如果有办法让php使用全系统的代理,我不知道,search它是乏味的。

招呼