Last active
December 31, 2019 15:25
-
-
Save barryhughes/83d5637c3a3e004df79759418e8cfaad to your computer and use it in GitHub Desktop.
HTTP authentication with file_get_contents() | PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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