19

I need to reach a foreign .php page protected by a regular .htaccess file (Auth type Basic, htpasswords etc...).

I'd like to send the user and password needed through the request. Is it possible? I would like to avoid cURL and all pecl_http dependent functions if possible...

Sebas
  • 21,192
  • 9
  • 55
  • 109
  • 1
    Thank you @jack, I couldn't find that one in the search results. Let's hope that from now on the keywords cumulate and our fellow developers will find their way to this answer. – Sebas Feb 25 '14 at 03:17

1 Answers1

38

Off-hand, I believe you can use a context to do that:

$context = stream_context_create(array (
    'http' => array (
        'header' => 'Authorization: Basic ' . base64_encode("$username:$password")
    )
));
$data = file_get_contents($url, false, $context);
bishop
  • 37,830
  • 11
  • 104
  • 139
  • According to comment by `chris dot vigelius at gmx dot net` in PHP documentation (https://www.php.net/manual/en/function.stream-context-create.php) the `header` parameter MUST be an array (bug = https://bugs.php.net/bug.php?id=41051). – Jose Manuel Abarca Rodríguez Jan 19 '22 at 20:26