2

My site communicates with a remote API to synchronize databases. The data I receive is in XML and 999 out of 1000 times this is proper XML which I can directly parse using simplexml_load_string.

Yet 1 out of 1000 times I get incorrectly encoded XML as shown in the attached image below.

I must somehow parse this gibberish, because trying to GET the same record will again give the same result.

If you have a suggestion on how to convert this, please let me know. I have tried the following functions but I am not a decoding expert so there were just shots in the dark:

  • gzdeflate($result)
  • utf8_decode($result)
  • iconv("UTF-8", "UTF-8//IGNORE", $result)
  • base64_decode($result)
  • iconv(mb_detect_encoding($result, mb_detect_order(), true), "UTF-8", $result)

    wrong encoding

4
  • 1
    Do the HTTP headers of the response received from the API call give any clues? If you are not sure how to see these, provide more details of your API call: is it GET or POST or ???; is id/password required, etc. Commented Dec 24, 2015 at 19:33
  • Thanks for reminding me about the headers because this solved my problem! See my updated question Commented Dec 24, 2015 at 21:17
  • Still, it's a mystery why they send this response body... Commented Dec 24, 2015 at 21:24
  • How do you authenticate? How long does it take for the script to time out? Commented Dec 24, 2015 at 21:56

1 Answer 1

1

BareNakedCoder suggested looking at the response headers and the answer was there: apparently the authentication tokens for this api expire very fast, even during the execution of my script. See attached image for the headers. So the solution is to re-authenticate every time when this happens.

Pity the api doesn't just say this in the regular XML output.

This happens in the api of Exact Online (accountancy software), so I will add that as a keyword to the original question.

headers

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.