TheJediCowboy
TheJediCowboy

Reputation: 9222

Checking Response from php curl_exec()?

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

Answers (2)

criticus
criticus

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

lonesomeday
lonesomeday

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

Related Questions