HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials

HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials (http://www.howtoforge.com/forums/index.php)
-   Programming/Scripts (http://www.howtoforge.com/forums/forumdisplay.php?f=7)
-   -   PHP check the response of the api (http://www.howtoforge.com/forums/showthread.php?t=54716)

alleks 31st October 2011 19:06

PHP check the response of the api
 
Hi, I am trying to check that if the response of the api contains something like

PHP Code:

<error>
<
message>No Videos found!</message>
<
code>####</code>
</error

To do a show_error() in codeigniter. I've tried it with preg_match but no success for me. Any help would be greatly appreciated.

alleks 31st October 2011 19:46

I have tried this but with no success

PHP Code:

if (preg_match('#<?xml version="1.0"  standalone="yes"?>\s*<error>\s*<message>(.*)</message>\s*<code>(.*)</code>\s*</error>#i', $videosJson));

for this

PHP Code:

<?xml version="1.0"  standalone="yes"?>
    <error>
        <message>No Videos found!</message>
        <code>2001</code>
    </error>


alleks 1st November 2011 08:54

Ok I've managed to do it by doing this:

PHP Code:


if (strpos($videosJson'<error>'))
            {
                
preg_match('/<message>(.+?)<\/message>/'$videosJson$match);
                
show_error("<strong>$tag</strong>: $match[1]");
            } 


till 1st November 2011 08:58

As the returned error is a XML document, you could have parsed it with simplexml like this to get a object of the error:

$xml = simplexml_load_string($videosJson);
print_r($xml);

alleks 1st November 2011 10:05

Oh cool, thanks till. I've never worked with xml till now so I didn't even bother on searching some function like json_decode for xml but seem that it's the one you just said.

But knowing that the solution I posted also works, I will keep that one for now.

Thanks again,
Alex


All times are GMT +2. The time now is 02:34.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.