我正在尝试从需要身份验证的服务器获取文件。我有以下 PHP 代码:
//Import categories
$url = "http://$username:$password@www.example.com";
$categoriesXML = file_get_contents($url);
var_dump($url . " > " . $categoriesXML);
return;
这个页面的输出仅仅是:string(22) "Authorization Required"。我也尝试过:
$context = stream_context_create(array('http' => array('header' => "Authorization: Basic " . base64_encode("$username:$password"))));
$url = "http://www.example.com";
$categoriesXML = file_get_contents($url, false, $context);
var_dump($categoriesXML);
return;
我也尝试使用 cURL 以及以下代码:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
var_dump($output);
return;
cURL 在 $info 中返回 301 代码并且 $output 为空。我是 PHP/服务器新手,我做错了什么?