Skip to content

Instantly share code, notes, and snippets.

@sagormax
Forked from gabrysiak/php-http-basic-auth.php
Created January 22, 2020 16:56
Show Gist options
  • Save sagormax/91135eef3138b74a79ed1eb9fd14a960 to your computer and use it in GitHub Desktop.
Save sagormax/91135eef3138b74a79ed1eb9fd14a960 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment