来自javascript的API调用失败,并且从PHP卷曲它们没问题。
用于检查身份验证的PHP脚本
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="Test auth"');
header('HTTP/1.0 401 Unauthorized');
echo '{"status":"401","message":"Unauthorized"}';
exit;
} else {
$user= $_SERVER['PHP_AUTH_USER'];
$pass = $_SERVER['PHP_AUTH_PW'];
}
var xhr = new XMLHttpRequest();
var apiid = 'admin';
var apipassword = '123';
var requesturl = 'http://localhost/man/api/man/GetLanguages';
xhr.open('POST', requesturl, false, apiid, apipassword);
xhr.send('{}');
if(xhr.status == 200){
alert(xhr.responseText);
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($data),'Authorization: Basic YWRtaW46MTIz'));
echo $result = curl_exec($ch);