Skip to content

Instantly share code, notes, and snippets.

@gabrysiak
Created August 14, 2014 19:40
Show Gist options
  • Save gabrysiak/ff94b0732332192d271d to your computer and use it in GitHub Desktop.
Save gabrysiak/ff94b0732332192d271d to your computer and use it in GitHub Desktop.
PHP - HTTP Request with Basic Authentication
$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);
@AndresReyesDev
Copy link

You're my new hero...

@kvnZero
Copy link

kvnZero commented Feb 24, 2021

You're my new hero...

+1

@bohnelang
Copy link

Perfect - thanks 👍

@unitycoder
Copy link

getting " failed to open stream: HTTP request failed! HTTP/1.0 411 Length Required"

@narukoshin
Copy link

narukoshin commented Feb 3, 2022

@unitycoder add Content-Length to your request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment