Ali Ashik
Ali Ashik

Reputation: 33

decoding xml output using gzuncompress(): data error in

I'm currently building a web app that's pulling data from an API. The API uses XML as form of accepting requests and sending responses. I've got he web app working perfectly fine on my WAMP server. I even deployed it online on a free PHP web server as way of testing it and it works seamlessly. However when I deployed it the live server, I got the following errors in my php log file.

Tue Apr 07 17:28:12 2015] [error] [client 94.236.7.190] PHP Notice: Undefined offset: 1 in /home/jdsites/camping.blacks.co.uk/api.php on line 214 [Tue Apr 07 17:28:12 2015] [error] [client 94.236.7.190] PHP Warning: gzuncompress(): data error in /home/jdsites/camping.blacks.co.uk/api.php on line 214 [Tue Apr 07 17:28:12 2015] [error] [client 94.236.7.190] PHP Notice: Trying to get property of non-object in /home/jdsites/camping.blacks.co.uk/api.php on line 89 [Tue Apr 07 17:28:12 2015] [error] [client 94.236.7.190] PHP Notice: Trying to get property of non-object in /home/jdsites/camping.blacks.co.uk/api.php on line 89 [Tue Apr 07 17:28:12 2015] [error] [client 94.236.7.190] PHP Notice: Trying to get property of non-object in /home/jdsites/camping.blacks.co.uk/api.php on line 89

This is the PHP code in my script:

// decode the attachment data and this should return a valid xml string
line 214: $attachment_xml = gzuncompress(base64_decode($matches[1]));
line 215: $responseXml = new SimpleXMLElement($attachment_xml);

I Can't seem to figure out the issue :-|

Upvotes: 2

Views: 1462

Answers (1)

hakre
hakre

Reputation: 198109

Your code misses fundamental error checking (sorry for the boldness here :)).

// decode the attachment data and this should return a valid xml string
$attachment_xml = gzuncompress(base64_decode($matches[1]));
$responseXml = new SimpleXMLElement($attachment_xml);

This might be working PHP code but only under certain circumstances. This is exactly what you experience: On your WAMP box, it's fine but elsewhere it spits a lot of errors and warnings. Let's see where the problem is first:

$match = $matches[1];
if (!strlen($match)) {
    throw new UnexpectedValueException('No match found');
}

$compress = base64_decode($match);
if (false === $compress) {
    throw new RuntimeException('Failed to decode base64');
}

$uncompress = gzuncompress($compressed);
if (false === $uncompress) {
    throw new RuntimeException('Failed to gzuncompress');
}

$xml = simplexml_load_string($uncompress);
if (false === $xml) {
    throw new RuntimeException('Failed to parse XML');
}

As this code obviously shows, it will tell on its own which operation it executes fails because each operation's return value is properly checked before the code continues.

As you interact here with foreign data this is really important because an error in the data could be intermingled into the different encodings you have here:

  • base64
  • gzip
  • XML

at each stage something just can fail, but you didn't even check in your original code.

Next to assuming that input data is always faulty, the code also checks for the match, that is the unknown encoding you're using you parse out from something I guess with a regular expression. So it's important you find the first place of error as early as possible. Otherwise you perhaps look in the wrong place to find the cause of error.

Upvotes: 2

Related Questions