代理

时间:2015-11-13 07:36:20

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

我正在使用第三方库,它使用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
  ]
]);

这很好用!!

问题是我无法使用我的代理设置配置第三方库。有没有办法在系统范围内配置此代理设置,以便file_get_contents()可以使用此设置,如curl?

export http_proxy=http://SERVERNAME:PORT
来自第三方库的curl工作得很好。但是file_get_contents()似乎没有识别环境变量。

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

都不起作用。

1 个答案:

答案 0 :(得分:0)

您尝试使用$ch = curl_init(); curl_setopt_array($ch, [ CURLOPT_URL => 'https://www.google.com/recaptcha/api/siteverify', CURLOPT_POST => true, CURLOPT_POSTFIELDS => [ 'secret' => $secret, 'response' => $captcha, 'remoteip' => $ip ], CURLOPT_RETURNTRANSFER => true ]); $output = curl_exec($ch); curl_close($ch); $arr = json_decode($output); if ( $arr->success !== true ) { //Submission errored out } else { //Send Email } 了吗?它可以帮助全局设置上下文。