【问题标题】:PHP file_get_contents works from one server, but gives an authorization error onPHP file_get_contents 在一台服务器上工作,但在
【发布时间】: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


【解决方案1】:

根据documentation of the function,没有“auth”参数。

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

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

【讨论】:

  • 虽然这与我最初阅读的授权示例相反,但这确实对我有用。谢谢。
  • 您的答案基本正确,不过,将变量命名为$auth 并不一定意味着参数为auth。 OP 代码中的$auth 是流上下文,没有错。
猜你喜欢
  • 2015-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-09
  • 2015-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多