HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials

HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials (
-   Programming/Scripts (
-   -   PHP check the response of the api (

alleks 31st October 2011 20: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:

message>No Videos found!</message>

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 20: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"?>
        <message>No Videos found!</message>

alleks 1st November 2011 09:54

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

PHP Code:

if (strpos($videosJson'<error>'))
show_error("<strong>$tag</strong>: $match[1]");

till 1st November 2011 09: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);

alleks 1st November 2011 11: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,

All times are GMT +2. The time now is 10:47.

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