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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3892635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:23:46  来源:igfitidea点击:

How to have PHP to use Proxy Setting to connect to internet?

phpproxy

提问by Alexar

I'm behind a proxy server that does not allow a direct connection to internet. All of my PHP applications fail to connect to internet for their update checks and etc.

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

How can I tell PHP my Proxy Settings?

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

I don't want to enter proxy settings into the code, I want PHP itself use it via a global config setting or something similar.

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

回答by Ian Hu

if almost all of you internet access need a proxy, I'd prefer do like this.

如果几乎所有的互联网访问都需要代理,我宁愿这样做。

//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']]);

the proxy will work for file_get_contentsbut not curl_exec

代理将工作file_get_contents但不curl_exec

here is a official document.

这是官方文件。

回答by Tramov

This depends on how your PHP application connects to the internet.

这取决于您的 PHP 应用程序如何连接到 Internet。

If taking the most likely situation using PHP cUrl. In that case the following options will help you:

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

curl_setopt($handle, CURLOPT_PROXY, $proxy_url); 
curl_setopt($handle, CURLOPT_PROXYUSERPWD, "[username]:[password]"); 

See also: http://www.php.net/manual/en/function.curl-setopt.php

另见:http: //www.php.net/manual/en/function.curl-setopt.php

回答by Evan Lee

Sample code:

示例代码:

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");

回答by Martin Tournoij

I use the PEAR HTTP_Request2module.

我使用 PEAR HTTP_Request2模块。

Here is a simplified version of my UrlOpen() function:

这是我的 UrlOpen() 函数的简化版本:

function UrlOpen($url)
{
  $request = new HTTP_Request2($url);

  $request->setConfig(array(
    'proxy_host' => '192.168.1.6',
    'proxy_port' => 8080,
    'proxy_user' => 'MYUSER',
    'proxy_password' => 'MYPASS',
    'ssl_verify_peer' => False,
    'connect_timeout' => 3,
  );

  return $request;
}

$req = UrlOpen($url);
$res = $req->send();
if ($res->getStatus() == '200')
  $data = $request->getBody();

回答by Thomas Decaux

Yes it's possible!

是的,这是可能的!

You can configure stream_context_set_defaultin a file, and include this file in all your Php program, by using auto_prepend_filephp.ini property.

您可以stream_context_set_default在文件中进行配置,并使用auto_prepend_filephp.ini 属性将此文件包含在您的所有 PHP 程序中。

I wrote a small gist about:

我写了一个小要点:

https://gist.github.com/ebuildy/381f116e9cd18216a69188ce0230708d

https://gist.github.com/ebuildy/381f116e9cd18216a69188ce0230708d

And an article in french:

还有一篇法语文章:

https://medium.com/@thomasdecaux/param%C3%A9trer-par-d%C3%A9faut-un-proxy-pour-php-file-get-content-3e9a32416979#.6zbg605cx

https://medium.com/@thomasdecaux/param%C3%A9trer-par-d%C3%A9faut-un-proxy-pour-php-file-get-content-3e9a32416979#.6zbg605cx

This technique is "cool" because, this allows your sys-admin to configure the host, so developer don't have to change anything in the code.

这种技术很“酷”,因为它允许您的系统管理员配置主机,因此开发人员不必更改代码中的任何内容。

回答by Tarik

I searched over the internet and could not find anything about stream_context_set_default()with a password protectedproxy server.

我在互联网上搜索并找不到任何关于stream_context_set_default()密码保护的代理服务器的信息。

Then I thought that password in basic authorization is sent in headers. So I modified the headers with the password extracted from a CURL request and it worked perfect!!!

然后我认为基本授权中的密码是在标题中发送的。因此,我使用从 CURL 请求中提取的密码修改了标头,并且效果很好!!!

Here is how you do it:

这是你如何做到的:

First send this request to any domain (example.com) as below:

首先将此请求发送到任何域(example.com),如下所示:

curl -v -U user:pass -x your_proxy_ip:port --url https://example.com

See the curl sent headers and I got these proxy lines to use later:

查看 curl 发送的标头,我得到了这些代理行以供稍后使用:

>   Trying XXX.XXX.XXX.XXX...
> Connected to XXX.XXX.XXX.XXX (XXX.XXX.XXX.XXX) port YYYY (#0)
> Establish HTTP proxy tunnel to example.com:443
> Proxy auth using Basic with user 'your_username_here'
> CONNECT example.com:443 HTTP/1.1
> Host: example.com:443
> Proxy-Authorization: Basic YW1hem9uOnXXXXXXXXXXXXXXX25SMg
> User-Agent: curl/7.47.0
> Proxy-Connection: Keep-Alive
>
< HTTP/1.1 200 Connection established
<
< Proxy replied OK to CONNECT request

OK now it is time to build our custom header:

好的,现在是时候构建我们​​的自定义标头了:

$default_opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Proxy-Authorization: Basic YW1hem9uOnXXXXXXXXXXXXXXX25SMg\r\n" .
              "Proxy-Connection: Keep-Alive",
    'proxy'=>"XXX.XXX.XXX.XXX:YYYY"
  )
);

$default = stream_context_set_default($default_opts);


$result = file_get_contents("https://ipleak.net/json/");
print_r(json_decode($result));

And it works perfect, you get the IP of your proxy server in response!

它运行完美,您将获得代理服务器的 IP 作为响应!

回答by rustycar

For some scripts, all you have to do is set the environment variable HTTP_PROXY. That was the case for both composer.pharand media-wiki's maintenance/update.phpscripts.

对于某些脚本,您所要做的就是设置环境变量HTTP_PROXYcomposer.phar和 media-wiki 的维护/update.php脚本都是这种情况。

E.g.:

例如:

setenv HTTP_PROXY http://1.2.3.4:3934

If your proxy is at 1.2.3.4 listening on port 3934. Worked for me.

如果您的代理在 1.2.3.4 监听端口 3934。为我工作。

回答by Jokerius

For Drupal you can set proxy configuration in your settings.php file.

对于 Drupal,您可以在 settings.php 文件中设置代理配置。

$conf['proxy_server']and so on.

$conf['proxy_server']等等。

More details here

更多细节在这里