【问题标题】:file_get_contents with X-Auth带有 X-Auth 的 file_get_contents
【发布时间】:2014-10-10 18:52:33
【问题描述】:

当我从中获取内容的 URL 使用 X-AUTH 身份验证时,我在使用 file_get_contents 时遇到问题。我确实有一个 X-AUTH 令牌,但我不确定如何将其绑定到 file_get_contents。因此,我在浏览器中访问页面时出现错误,其中一部分是:

警告:无法打开流:HTTP 请求失败! HTTP/1.1 401 未经授权

我从来没有和 file_get_contents 打过很多交道,没关系将它与 X-AUTH 一起使用,所以我有点卡住了!

任何帮助将不胜感激。

谢谢

【问题讨论】:

标签: php file-get-contents


【解决方案1】:

请阅读以下博文:

Using Authentication And file_get_contents()

博客条目包含一个很好的例子来说明如何实现这一点:

$username = 'username';
$password = 'password';

$context = stream_context_create(array(
    'http' => array(
        'header'  => "Authorization: Basic " . base64_encode("$username:$password")
    )
));
$data = file_get_contents($url, false, $context);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-17
    • 2015-03-31
    • 1970-01-01
    • 2015-11-10
    • 1970-01-01
    • 2022-08-19
    • 2010-12-31
    • 1970-01-01
    相关资源
    最近更新 更多