API XML Parsing

I’m trying hard to read data from the API and pull the values from it using PHP. To make it easy, I’ve tried to get to the returned XML to an array using a variety of code - including code I’ve used on other XML data.

However, no matter what I throw at the returned XML, it refuses to convert to an array. Have anyone else had these problems, as I’m not even having any errors returned what so ever.

Any help would be massively appreciated.

Is the raw XML coming back OK? I’m not sure the response would neatly fit into an array object, as it’s not always two-dimensional. I think if you post your code samples to somewhere like Stack Overflow you’ll get a pretty rapid response.

Hi Glenn. The XML is coming back fine, and even outputted fine. The class I’m using is called xml2array, and works fine with other APIs that return much larger blocks of xml than Quickfile, which is why I’m not sure its not working.

My first port of call for help was stack exchange, but I only searched it, finding a few different ways of converting xml to an array. none of them seem to work, which is why I’m a little bit puzzled!

I’m afraid we don’t have any experience with PHP here. Although have you tried grabbing the response XML in plain text saving to notepad and changing the file to .xml, then open this in Explorer or any other XML viewer and see if it loads OK? First thing is to rule out if our API is not returning malformed XML…What method are you calling?

Hi @Glenn. I’ve tried feeding the returned XML straight into the xml2array function, and it works great. So, it would appear to me it’s the way the data is being returned.

At present, I’m using cURL to post the request to your side, and retrieve the response.

$urltopost = "https://quickfile.co.uk/WebServices/API/invoices.ashx";
$datatopost = ''; // Actual XML request

$ch = curl_init ($urltopost);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $datatopost);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
$returndata = curl_exec ($ch);

I’m not sure if this is the way I’m pulling the data, or the way your servers are returning it… Will keep trying other things, but would like to know your (or your teams) opinion.

For the record - this was client search. Also tried Invoice Get, which appears to be the same, except Invoice_get doesn’t form the array… Hope that makes sense!

[Edit]
I just tried another method, and it returned the following error:

Warning: SimpleXMLElement::__construct(): Entity: line 1: parser error
: Document labelled UTF-16 but has UTF-8 content

This is proving to a bit a little bit tricky!

When we return the XML from the handler we set the content-type to text/xml and the encoding to UTF-8.

'SEND RESPONSE BACK TO CLIENT
Sub sendResponse(xml As String)
    System.Web.HttpContext.Current.Response.ContentType = "text/xml"
    System.Web.HttpContext.Current.Response.Write(xml)
End Sub

If I just called the ASHX endpoint in my browser I can see the following response headers:

Your second error indicates that your code was expecting UTF-16 not UTF-8. Maybe somewhere in your code you can tell it to expect UTF-8?

Actually Glenn, looking at the raw response, it starts with this line, which may be the cause of the problem:

<?xml version="1.0" encoding="utf-16"?>

“utf-16”. The browser is returning utf-8, and the code states utf-16. Manually changing this with a simple search and replace, seems to fix the issue. Not sure how wide spread this is though?

Mmm, that’s strange. We certainly need to get this fixed, leave this with me and I’ll get this looked at tomorrow.

1 Like

We’ve fixed this issue and rolled it out. Please let me know if that now works ok with your XML parsing component?

Hi Glenn,

I can confirm that this now works - thank you :smile:

1 Like