I am making a request to a CDN API which flushes the cache for specific items. I am trying to parse the response correctly, but the response is not only returned (from how I can see it) incorrectly, but I am struggling to parse such a large amount of information. My aim is to get a list of unsuccessful flushes from the response, along with any error codes.
This is the kind of URL that you run for their API to process the request:
https://openapi.us.cdnetworks.com/OpenAPI/services/CachePurgeAPI/executeCachePurge?userId=***&password=***&purgeUriList=http://urlhere.com?param1=here¶m2=here&purgeUriList=http:://url2here.com?param1=here
The documentation states that the response is in XML. Which it IS when you type the address into the browser. But performing a request from PHP brings back something that isn't XML.
It should look like this:
But what I actually get back is this:
000REQUEST ACCEPTEDhttp://cdn.oursite.com/blah.php?alt=1&cid=6291&campaign=126400&width=25010REQUEST ACCEPTEDhttp://cdn.oursite.co.uk/blah.php?alt=1&cid=6292&campaign=126401&width=250122';
Now that's just an example of two successful flushes. The API docs show a load of extra errors that could be returned:
0: Successful; 2: Successful only in some URLs; 999: Temporary Error; 194 Too many URLs
I don't want to have to parse the string for each individual response. The first part returned is a resultCode, the second a totalURLNum, third successes, fourth fails, and then a result list containing even more information.
My initial PHP code to retrieve the string:
A simple file_get_contents request.
$result = file_get_contents(urlencode($url));
Then I got some RED TEXT back!!!
I used cURL with CURLOPT_SSL_VERIFYPEER
and CURLOPT_SSL_VERIFYHOST
set to false:
$aData = curl_exec($rData);
and this returned red text:
string '<ns:executeCachePurgeResponse xmlns:ns="http://control.webif.server.openapi.cdnetworks.com"><ns:return xmlns:ax21="http://cachepurge.purge.response.server.openapi.cdnetworks.com/xsd" type="com.cdnetworks.openapi.server.response.purge.cachepurge.CachePurge"><ax21:failureURLNum>2</ax21:failureURLNum><ax21:resultCode>2</ax21:resultCode>(etc)(etc) (length=1678)
Tried using SimpleXML:
So I tried using simplexml on this. I've tried:
$xml = simplexml_load_string($aData);
also
$properties = $xml->xpath('//Property');
and I've tried using SimpleXML to load the initial file:
$result = simplexml_load_file($url)
How can I go about parsing this? It's clear I'm not getting back XML like I want to, and the string is going to be a nightmare to parse through.
What I'm currently up to:
I ran the following PHP on the cURL output.
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($aData);
$dom->formatOutput = TRUE;
echo $dom->saveXml();
This gave me a slightly more formatted output, but it's still not XML so not easy to parse!:
2 2 9999 Check your input. If it's not wrong, contact us please. http://cdn.whatever.co.uk/blah.php?alt=1&cid=6291&campaign=126400&width=250 0 0 REQUEST ACCEPTED http://cdn.whatever.co.uk/blah.php?alt=1&cid=6292&campaign=126401&width=250 1 9999 Check your input. If it's not wrong, contact us please. http://cdn.whatever.co.uk/blah.php?alt=1&cid=6291&campaign=126400&width=250 0 1 3
Using <pre>
on either side of the XML, viewing the source, and then putting the code into an XML validator returns that it is false xml.
Namespaces CAN be retrieved, but that's as far as grabbing data from the XML can go. The XML is not valid, the dev team are working on this.
The 'XML' returned is not valid if it cannot be handled properly by simplexml.
Try using an xml validator to make sure that you are indeed trying to parse valid xml.