I’ve got a script that loads information from another page, using the following code:
$creds = 'user:password';
$base64 = base64_encode($creds);
$base_url = 'http://example.com/page.html';
$data = file_get_contents($base_url.$path, false, $auth);
This works fine from one server (my local one, using Ubuntu) but gives me the following headers when run from an external server:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Authorization Required</title>
</head><body>
<h1>Authorization Required</h1>
<p>This server could not verify that you
are authorized to access the document
requested. Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
<hr>
<address>Apache/2.2.14 (Unix) mod_ssl/2.2.14 OpenSSL/0.9.7a DAV/2 SVN/1.6.6 PHP/5.3.14 JRun/4.0 Server at ww2.rch.org.au Port 80</address>
</body></html>
Is there anything in particular I should be looking for here? I already have allow_url_fopen set to true on both servers if it helps.
Edit: Sorry, there was some other code in there that needed removing. $auth is as follows:
$auth = stream_context_create(array(
'http' => array(
'header' => 'Authorization: Basic '.$base64
)
));
$auth is a stream context, containing the authorization header.
According to the documentation of the function there is no “auth” parameter.
HTTP credentials are usually added in this format: