带有X-Auth的file_get_contents

时间:2014-10-10 18:52:34

标签: php file-get-contents

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

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

我从来没有与file_get_contents进行任何交易,因为我在X-AUTH中使用它,所以我有点卡住了!

非常感谢任何帮助。

由于

1 个答案:

答案 0 :(得分: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);