Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a HTTP GET request with HTTP-Basic authentication

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?

like image 558
Naftuli Kay Avatar asked Oct 11 '11 21:10

Naftuli Kay


People also ask

How do I access API with basic authentication?

With Basic Authentication, you pass your credentials (your Apigee account's email address and password) in each request to the Edge API. Basic Authentication is the least secure of the supported authentication mechanisms. Your credentials are not encrypted or hashed; they are Base64-encoded only.

How do I get HTTP authentication?

A client that wants to authenticate itself with the server can then do so by including an Authorization request header with the credentials. Usually a client will present a password prompt to the user and will then issue the request including the correct Authorization header.

How do I set basic authentication in HTTP header?

Basic Auth: The client sends HTTP requests with the Authorization header that contains the word Basic, followed by a space and a base64-encoded(non-encrypted) string username: password. For example, to authorize as username / Pa$$w0rd the client would send. Note: Base64 encoding does not mean encryption or hashing!


2 Answers

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!

like image 168
clone45 Avatar answered Sep 17 '22 14:09

clone45


Using file_get_contents() with a stream to specify the HTTP credentials, or use curl and the CURLOPT_USERPWD option.

like image 26
Marc B Avatar answered Sep 16 '22 14:09

Marc B