2014-10-10 23 views
0

我在使用file_get_contents時遇到問題,當我從URL獲取內容時使用X-AUTH身份驗證。我確實有一個X-AUTH標記,但我不確定如何將其綁定到file_get_contents。由於這個原因,在訪問瀏覽器中的頁面時出現錯誤,其中包括:帶有X-Auth的file_get_contents

警告:無法打開流:HTTP請求失敗! HTTP/1.1 401未授權

我從來沒有和file_get_contents打過很多交易,從來沒有將它與X-AUTH一起使用,所以我有點卡住了!

任何幫助將不勝感激。

感謝

+0

我懷疑你可以用' file_get_contents()'做到這一點,請看看這裏,而不是:http://stackoverflow.com/questions/13187462/sending-auth-in-headers-php-curl – Crontab 2014-10-10 19:12:04

回答

1

請閱讀foloowing博客文章:

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);