1

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?

2 Answers 2

3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

2

Are you getting XML response? Then parse the XML

$xml = new SimpleXMLElement($result);
if(isset($xml->your_element->isValidPost)){
   //do what you need
}

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.