php 代理后面的 file_get_contents ?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1336262/
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 02:04:48  来源:igfitidea点击:

file_get_contents behind a proxy?

phpproxy

提问by meder omuraliev

At work we have to use a proxy to basically access port 80 for example, we have our own custom logins for each user.

在工作中,我们必须使用代理来基本访问端口 80,例如,我们为每个用户都有自己的自定义登录。

My temporary workaround is using curl to basically login as myself through a proxy and access the external data I need.

我的临时解决方法是使用 curl 基本上通过代理以我自己的身份登录并访问我需要的外部数据。

Is there some sort of advanced php setting I can set so that internally whenever it tries to invoke something like file_get_contents()it always goes through a proxy? I'm on Windows ATM so it'd be a pain to recompile if that's the only way.

是否有某种高级 php 设置我可以设置,以便在内部每当它尝试调用类似的东西时,file_get_contents()它总是通过代理?我在 Windows ATM 上,所以如果这是唯一的方法,重新编译会很痛苦。

The reason my workaround is temporary is because I need a solution that's generic and works for multiple users instead of using one user's credentials ( Ive considered requesting a separate user account solely to do this but passwords change often and this technique needs to be deployed throughout a dozen or more sites ). I don't want to hard-code credentials basically to use the curl workaround.

我的解决方法是临时性的原因是因为我需要一个通用的解决方案,适用于多个用户,而不是使用一个用户的凭据(我曾考虑请求一个单独的用户帐户来单独执行此操作,但密码经常更改,并且需要在整个过程中部署此技术)十几个或更多网站)。我不想硬编码凭据基本上使用 curl 解决方法。

回答by Pascal MARTIN

To use file_get_contents()over/through a proxy that doesn't require authentication, something like this should do :

file_get_contents()通过/通过不需要身份验证的代理使用,应该执行以下操作:

(I'm not able to test this one : my proxy requires an authentication)

(我无法测试这个:我的代理需要身份验证)

$aContext = array(
    'http' => array(
        'proxy'           => 'tcp://192.168.0.2:3128',
        'request_fulluri' => true,
    ),
);
$cxContext = stream_context_create($aContext);

$sFile = file_get_contents("http://www.google.com", False, $cxContext);

echo $sFile;

Of course, replacing the IP and port of my proxy by those which are OK for yours ;-)

当然,将我的代理的 IP 和端口替换为适合您的 IP 和端口;-)

If you're getting that kind of error :

如果您遇到这种错误:

Warning: file_get_contents(http://www.google.com) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 407 Proxy Authentication Required

It means your proxy requires an authentication.

这意味着您的代理需要身份验证。

If the proxy requires an authentication, you'll have to add a couple of lines, like this :

如果代理需要身份验证,则必须添加几行,如下所示:

$auth = base64_encode('LOGIN:PASSWORD');

$aContext = array(
    'http' => array(
        'proxy'           => 'tcp://192.168.0.2:3128',
        'request_fulluri' => true,
        'header'          => "Proxy-Authorization: Basic $auth",
    ),
);
$cxContext = stream_context_create($aContext);

$sFile = file_get_contents("http://www.google.com", False, $cxContext);

echo $sFile;

Same thing about IP and port, and, this time, also LOGIN and PASSWORD ;-) Check out all valid http options.

IP 和端口也是一样,这一次,还有登录和密码 ;-) 检查所有有效的http 选项

Now, you are passing an Proxy-Authorizationheader to the proxy, containing your login and password.

现在,您将一个Proxy-Authorization标头传递给代理,其中包含您的登录名和密码。

And... The page should be displayed ;-)

并且...应该显示该页面;-)

回答by Pirob.com

Use stream_context_set_defaultfunction. It is much easier to use as you can directly use file_get_contents or similar functions without passing any additional parameters

使用stream_context_set_default功能。它更容易使用,因为您可以直接使用 file_get_contents 或类似的函数而无需传递任何额外的参数

This blog postexplains how to use it. Here is the code from that page.

这篇博文解释了如何使用它。这是该页面的代码。

<?php
// Edit the four values below
$PROXY_HOST = "proxy.example.com"; // Proxy server address
$PROXY_PORT = "1234";    // Proxy server port
$PROXY_USER = "LOGIN";    // Username
$PROXY_PASS = "PASSWORD";   // Password
// Username and Password are required only if your proxy server needs basic authentication

$auth = base64_encode("$PROXY_USER:$PROXY_PASS");
stream_context_set_default(
 array(
  'http' => array(
   'proxy' => "tcp://$PROXY_HOST:$PROXY_PORT",
   'request_fulluri' => true,
   'header' => "Proxy-Authorization: Basic $auth"
   // Remove the 'header' option if proxy authentication is not required
  )
 )
);

$url = "http://www.pirob.com/";

print_r( get_headers($url) );

echo file_get_contents($url);
?>

回答by VolkerK

Depending on how the proxy login works stream_context_set_defaultmight help you.

根据代理登录的工作方式,stream_context_set_default可能对您有所帮助。

$context  = stream_context_set_default(
  array(
    'http'=>array(
      'header'=>'Authorization: Basic ' . base64_encode('username'.':'.'userpass')
    )
  )
);
$result = file_get_contents('http://..../...');

回答by Paul

There's a similar post here: http://techpad.co.uk/content.php?sid=137which explains how to do it.

这里有一个类似的帖子:http: //techpad.co.uk/content.php?sid=137解释了如何去做。

function file_get_contents_proxy($url,$proxy){

    // Create context stream
    $context_array = array('http'=>array('proxy'=>$proxy,'request_fulluri'=>true));
    $context = stream_context_create($context_array);

    // Use context stream with file_get_contents
    $data = file_get_contents($url,false,$context);

    // Return data via proxy
    return $data;

}