Skip to content

Instantly share code, notes, and snippets.

@barryhughes
Last active December 31, 2019 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barryhughes/83d5637c3a3e004df79759418e8cfaad to your computer and use it in GitHub Desktop.
Save barryhughes/83d5637c3a3e004df79759418e8cfaad to your computer and use it in GitHub Desktop.
HTTP authentication with file_get_contents() | PHP
<?php
/**
* Make an HTTP request with basic authentication, using 'pure PHP'.
*
* @see https://www.php.net/manual/en/wrappers.http.php
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication
*/
$username = 'http_username';
$password = 'http_password';
$auth = base64_encode( "{$username}:{$password}" );
$response = file_get_contents( 'http://some.url', false, stream_context_create( [
'http' => [ 'header' => 'Authorization: Basic ' . $auth ]
] ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment