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...

1
  • 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. Commented Feb 25, 2014 at 3:17

1 Answer 1

39

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);
Sign up to request clarification or add additional context in comments.

1 Comment

According to comment by chris dot vigelius at gmx dot net in PHP documentation (php.net/manual/en/function.stream-context-create.php) the header parameter MUST be an array (bug = bugs.php.net/bug.php?id=41051).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.