Reputation: 1137
I am using SimpleXML to load a response from an API call. However, if the server returns an error, it only returns a single XML tag with the error message:
<err>ERROR MESSAGE HERE</err>
I'm currently using this code to parse the API response:
$parsedresponse = simplexml_load_string($response);
The $parsedresponse variable contains only the error message. However, I need a way to check if the <err>
tag is present so I know if there was an error. I can't seem to figure out how to do this...
Thank you!
Upvotes: 1
Views: 265
Reputation: 57690
If err
tag is the root tag use the following condition to trace the error.
if ($parsedresponse->getName()=='err'){
// got it
}
If its the first child use
if (isset($parsedresponse->err)){
// got it
}
Upvotes: 1
Reputation: 1383
<?php
$xml = <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<err>ERROR MESSAGE HERE</err>
EOF;
$sxml = simplexml_load_string($xml);
if ($sxml->getName() != "err") print('not set');
else print('set');
?>
Upvotes: 0