2

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

    $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 是一个流上下文,包含授权标头。

4

1 回答 1

3

根据该函数的文档,没有“auth”参数。

HTTP 凭据通常以这种格式添加:

http://username:password@example.com/page.html
于 2013-02-18T10:16:02.777 回答