Jimmy8t8
Jimmy8t8

Reputation: 55

Check whether an xml response contains a certain value php

I am posting xml to a server that sends out a response xml. My problem is that I do not know how to deal with the incoming xml (check the value of the 'purchased' element and redirect user (based on criteria) to the 'redirect_url' element). Here is a response code example:

<?xml version="1.0" encoding="UTF-8"?>
<result>
    <posting_error>0</posting_error>
    <purchased>1</purchased>
    <redirect_url>http://redirect.php?id=123</redirect_url>
</result>

And here is the snippet of PHP I have:

<?php
    # $headercontent is not referencing the response in any way, how would I do this
    # (if need be)?
    if($headercontent->result[0]->purchased == 1)
    {
        #redirect the user to the 'redirect_url' in the response xml 
    }
    else
    {
       echo "the application was unsuccessful";
    }
?>

Any help on this problem would be appreciated.

Upvotes: 0

Views: 197

Answers (1)

Prix
Prix

Reputation: 19528

You can read the resulting XML using simplexml like this:

<?php
$string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<result>
    <posting_error>0</posting_error>
    <purchased>1</purchased>
    <redirect_url>http://redirect.php?id=123</redirect_url>
</result>
XML;

$result = simplexml_load_string($string);
if (isset($result->purchased))
{
    echo $result->purchased;
}
else
{
    echo "no purchased value is present...";
}

As the name says simplexml_load_string reads an string of XML as an object that you can easily access.

Upvotes: 1

Related Questions