Making a HTTP GET request with HTTP-Basic authentication

Naftuli Kay picture Naftuli Kay · Oct 11, 2011 · Viewed 63.6k times · Source

I need to build a proxy for a Flash Player project I'm working on. I simply need to make a HTTP GET request with HTTP-Basic authentication to another URL, and serve the response from PHP as if the PHP file was the original source. How can I do this?

Answer

clone45 picture clone45 · Feb 5, 2014

Marc B did a great job of answering this question. I recently took his approach and wanted to share the resulting code.

<?PHP

$username = "some-username";
$password = "some-password";
$remote_url = 'http://www.somedomain.com/path/to/file';

// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header' => "Authorization: Basic " . base64_encode("$username:$password")                 
  )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents($remote_url, false, $context);

print($file);

?>

I hope that this is helpful to people!