【问题标题】:3rd party file_get_contents behind proxy代理后面的第 3 方 file_get_contents
【发布时间】:2015-11-13 07:36:19
【问题描述】:

我正在使用第 3 方库,它使用 file_get_contents() 来检索外部文档。因为我在代理后面,所以我收到了错误:

file_get_contents(http://json-ld.org/contexts/person.jsonld): failed to open stream: Connection timed out

我通过 stream_context_create() 使用代理设置测试了 file_get_contents():

$context = stream_context_create([
  'http' => [
    'proxy' => 'tcp://SERVERNAME:PORT',
    'request_fulluri' => true
  ]
]);

这很好用!!

问题是我无法使用我的代理设置配置 3rd 方库。有没有办法在系统范围内配置这个代理设置,以便 file_get_contents() 可以使用这个设置,就像 curl 一样?

export http_proxy=http://SERVERNAME:PORT

使用来自 3rd 方库的 curl 可以正常工作。但是 file_get_contents() 似乎无法识别环境变量。

export http_proxy=tcp://SERVERNAME:PORT
export tcp_proxy=tcp://SERVERNAME:PORT

两者都不起作用。

【问题讨论】:

    标签: php curl proxy environment-variables file-get-contents


    【解决方案1】:

    您是否尝试过使用 stream_context_set_default ?它可以帮助全局设置上下文。

    【讨论】:

      猜你喜欢
      • 2010-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-10
      相关资源
      最近更新 更多