【问题标题】:file_get_contents behind a proxy?代理后面的file_get_contents?
【发布时间】:2010-11-23 02:29:04
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: php proxy


    【解决方案1】:

    要通过/通过不需要身份验证的代理使用file_get_contents(),应该这样做:

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

    $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;
    

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

    如果您遇到这种错误:

    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
    

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

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

    $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;
    

    IP 和端口也一样,而且这次还有 LOGIN 和 PASSWORD ;-) 检查所有有效的http options

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

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

    【讨论】:

    • Martin,我有一个代理自动配置 (pac) 文件,而不是单个代理服务器。它还需要 NTLM 身份验证。你能帮帮我吗?
    • 我收到以下错误:无法打开流:无法找到套接字传输“http” - 您在配置 PHP 时是否忘记启用它?我正在使用启用 curl 的 PHP5.5/Apache2.4。
    • 非常感谢。通常 cURL 让我满意,但我正在试验 Appengine PHP,并且没有 cURL laode,所以带有流上下文的 file_get_content 救了我:D
    • @codeomnitrix,可能有点晚了,但我遇到了同样的问题,我将 pac 文件的 url 粘贴到浏览器中,在文件中我可以找到代理 IP 地址。
    • 对于任何人:在我的情况下,还需要使用tcp 作为协议。在我使用http 之前,是什么给了我错误:无法打开流:无法找到套接字传输“http” - 您在配置 PHP 时是否忘记启用它?
    【解决方案2】:

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

    blog post 解释了如何使用它。这是该页面的代码。

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        这里有一个类似的帖子: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;
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-10
          • 2023-03-05
          • 2011-04-24
          • 2014-03-09
          • 2017-10-19
          相关资源
          最近更新 更多