PHP check the response of the api

Discussion in 'Programming/Scripts' started by alleks, Oct 31, 2011.

  1. alleks

    alleks Member

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

    PHP:
    <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.
     
  2. alleks

    alleks Member

    I have tried this but with no success

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

    PHP:
    <?xml version="1.0"  standalone="yes"?>
        <error>
            <message>No Videos found!</message>
            <code>2001</code>
        </error>
     
  3. alleks

    alleks Member

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

    PHP:

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

     
  4. till

    till Super Moderator Staff Member ISPConfig Developer

    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);
     
  5. alleks

    alleks Member

    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
     

Share This Page