molokolillana
New Member
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:\[code\]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\[/code\]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:\[code\]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';\[/code\]Now that's just an example of two successful flushes. The API docs show a load of extra errors that could be returned:\[code\]0: Successful; 2: Successful only in some URLs; 999: Temporary Error; 194 Too many URLs\[/code\]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.\[code\]$result = file_get_contents(urlencode($url));\[/code\]Then I got some RED TEXT back!!!I used cURL with \[code\]CURLOPT_SSL_VERIFYPEER\[/code\] and \[code\]CURLOPT_SSL_VERIFYHOST\[/code\] set to false:\[code\]$aData = http://stackoverflow.com/questions/12725975/curl_exec($rData);\[/code\]and this returned red text:\[code\]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)\[/code\]Tried using SimpleXML:So I tried using simplexml on this. I've tried:\[code\]$xml = simplexml_load_string($aData);\[/code\]also \[code\]$properties = $xml->xpath('//Property');\[/code\]and I've tried using SimpleXML to load the initial file:\[code\]$result = simplexml_load_file($url)\[/code\]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.\[code\]$dom = new DOMDocument;$dom->preserveWhiteSpace = FALSE;$dom->loadXML($aData);$dom->formatOutput = TRUE;echo $dom->saveXml();\[/code\]This gave me a slightly more formatted output, but it's still not XML so not easy to parse!:\[code\]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\[/code\]