I have a curl function which connects to an API and returns xml.
I call this function from another script, and want to go through the xml and pick out some urls, but I get an I/O error so I think I am not handling the xml correctly.
This is the curl function
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';
}
}
This is it being called
//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);
The connect function is definitely getting the xml as I have echo'd it in the function and also when i run the script in my browser it outputs the xml to screen as well as "Warning: simplexml_load_file(): I/O warning : failed to load external entity "1" in "
Im not sure what im missing :-(
simplexml_load_file();
loads XML from a file by passing it the file name.
Since you already have the XML string, you want simplexml_load_string();
in stead.
edit:
Also you need to use curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
to tell curl to return the response, in stead of just returning a status code. The 1
that you are seeing is the status code.