Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

I'm not getting an error per se -- i'm just getting a "forbidden" response from 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);
 }
like image 298
Jeff Avatar asked Jan 29 '13 20:01

Jeff


1 Answers

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. So to authenticate on a proxy, you first have to pull a file from HTTP, before the context is valid for using on 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:[email protected]",false,$context); 
?> 
like image 166
Teun Lassche Avatar answered Nov 09 '22 15:11

Teun Lassche