Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.