我有一个数据获取模块,该模块可根据http请求为我提供一些xml数据。 该请求需要具有用户密码。 如果我尝试通过浏览器上的javascript获取数据,则可以正常运行。这是工作代码:
var url = "http://192.168.0.10/analoginput/all/range";
var x = httpGet(url);
function httpGet(theUrl, callback){
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, true ); // false for synchronous request
var szAuthValue = make_basic_auth("root","00000000");//a function to encode user and password
xmlHttp.setRequestHeader("Authorization", szAuthValue);
xmlHttp.withCredentials = true; //for cross domain
xmlHttp.send( null );
return xmlHttp;
}
但是当我尝试获取数据服务器端时,它给出了404:
$username='root';
$password='00000000';
$URL='http://192.168.0.10/analoginutput/all/range';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
$headers = [
'Access-Control-Allow-Origin: *'
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result=curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
curl_close ($ch);
echo "result: ".$result;
echo "code: ".$status_code;
我在做什么错? 谢谢!