简体   繁体   English

php file_get_contents授权标头

[英]php file_get_contents authorization header

Can anyone give me any explanation for why this authorization function for a private bitbucket repository is working on my local machine (running PHP Version 5.3.17) but is not authorizing on my remote server (running PHP Version 5.3.20) 任何人都可以解释为什么私有bitbucket存储库的授权功能在我的本地机器上运行(运行PHP版本5.3.17),但未在我的远程服务器上授权(运行PHP版本5.3.20)

I'm not getting an error per se -- i'm just getting a "forbidden" response from bitbucket. 我本身并没有得到错误 - 我只是从bitbucket得到了一个“禁止”的回应。 But everything works great running from my local server. 但是从我的本地服务器运行一切都很好。

function bitBucketConnect($url){
    global $bitPassword;
    global $bitUsername;
    $context = stream_context_create(array(
     'http' => array(
       'header' => "Authorization: Basic " . base64_encode("$bitUsername:$bitPassword")
       )
    ));

 // Make the request
 return file_get_contents($url, false, $context);
 }

Your proxy will respond that authentication is required. 您的代理将回复需要身份验证。 You may scratch your head and think "but I'm providing authentication!" 你可能会挠头并想“但我正在提供身份验证!”

The issue is that the 'header' value is only applicable to http connections. 问题是'header'值仅适用于http连接。 So to authenticate on a proxy, you first have to pull a file from HTTP, before the context is valid for using on FTP. 因此,要在代理上进行身份验证,首先必须从HTTP中提取文件,然后才能在FTP上使用上下文。

<?php 
$opts = array('ftp' => array( 
    'proxy' => 'tcp://vbinprst10:8080', 
    'request_fulluri'=>true, 
    'header' => array( 
        "Proxy-Authorization: Basic $auth" 
        ) 
    ), 
    'http' => array( 
    'proxy' => 'tcp://vbinprst10:8080', 
    'request_fulluri'=>true, 
    'header' => array( 
        "Proxy-Authorization: Basic $auth" 
        ) 
    ) 
); 
$context = stream_context_create($opts); 
$s = file_get_contents("http://www.example.com",false,$context); 
$s = file_get_contents("ftp://anonymous:anonymous@ftp.example.org",false,$context); 
?> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM