Reputation: 9222
I have the following cUrl function call that is returning the response code.
$result = curl_exec($ch);
I need to check the $result for the following response property.
<isValidPost>true</isValidPost>
I could always parse through and check for that exact string, but is there an alternative way to check this?
Upvotes: 1
Views: 413
Reputation: 1599
Are you getting XML response? Then parse the XML
$xml = new SimpleXMLElement($result);
if(isset($xml->your_element->isValidPost)){
//do what you need
}
Upvotes: 2
Reputation: 237905
Presuming that the API is set in stone (i.e. won't change at a moment's notice), in this case it's probably safe to a simple string search
if (false !== strpos($result, '<isValidPost>true</isValidPost>')) {
However, to do this in a more reliable way, you may want to do DOM parsing:
$dom = new DOMDocument;
$dom->parseXML($result);
$isValidPostTag = $dom->getElementsByTagName('isValidPost')->item(0);
if ($isValidPostTag) {
$isValidPost = $isValidPostTag->nodeValue == 'true';
} else {
$isValidPost = false;
}
Upvotes: 3