PHP file_get_contents在一台服务器上工作,但在上出现授权错误


PHP file_get_contents works from one server, but gives an authorization error on

我有一个脚本,它使用以下代码从另一个页面加载信息:

    $creds = 'user:password';
$base64 = base64_encode($creds);
$base_url = 'http://example.com/page.html';
$data = file_get_contents($base_url.$path, false, $auth);

这在一台服务器上运行良好(我的本地服务器,使用Ubuntu),但在外部服务器上运行时会给我以下标题:

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

我在这里有什么特别需要找的吗?如果有帮助的话,我已经在两台服务器上都将allow_url_fopen设置为true。

编辑:抱歉,其中还有一些其他代码需要删除$auth如下:

$auth = stream_context_create(array(
  'http'    => array(
  'header'  => 'Authorization: Basic '.$base64
  )
));

$auth是一个流上下文,包含授权头。

根据函数的文档,没有"auth"参数。

HTTP凭据通常以以下格式添加:

http://username:password@example.com/page.html