我有一个curl函数,它连接到一个API并返回xml。
我从另一个脚本调用此函数,并希望通过xml并挑选出一些url,但我得到一个I/O错误,所以我认为我没有正确处理xml。
这是旋度函数
function &connect($url) {
//If token is not set skip to else condition to request a new token
if(!empty($_SESSION['token'])) {
//Initiate a new curl session
$ch = curl_init($url);
//Don't require header this time as curl_getinfo will tell us if we get HTTP 200 or 401
curl_setopt($ch, CURLOPT_HEADER, 0);
//Provide Token in header
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic '.$_SESSION['token']));
//Execute the curl session
$data = curl_exec($ch);
//Close the curl session
curl_close($ch);
return $data;
} else {
//Run the getToken function above if we are not authenticated
getToken($url);
return 'error';
}
}
它被称为
//build url creates the api url required based on parameters passed into GET
$link = build_url;
//call the connect function and pass it the built link
$xml = connect($link);
//load the returned xml
$oxml = simplexml_load_file($xml);
连接函数肯定是得到xml,因为我已经在函数中回声它,也当我在浏览器中运行脚本,它输出xml屏幕以及"警告:simplexml_load_file(): I/O警告:未能加载外部实体"1"在"
我不知道我错过了什么:-(
simplexml_load_file();
通过传递文件名从文件加载XML。
因为您已经有了XML字符串,所以您想要simplexml_load_string();
。
您还需要使用curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
来告诉curl返回响应,而不仅仅是返回状态码。您看到的1
是状态码。