【发布时间】:2013-02-18 09:50:34
【问题描述】:
我有一个从另一个页面加载信息的脚本,使用以下代码:
$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是什么?您是否提供了正确的用户和密码? -
有趣的是,你从不使用
$base_64,而你在哪里初始化$auth? -
编辑了问题以显示 $auth 和 $base_64 的使用位置。可能应该首先包括这些。
-
@AlvinWong:是的,细节是正确的——我要说的第一件事是它在一台服务器上运行良好,但在另一台服务器上运行良好。我将此脚本复制到新服务器上,然后它就坏了。
标签: php http authentication