Grow your CSS skills. Land your dream job.

DOMDocument load eror handling

  • # July 3, 2013 at 10:41 am

    I want to protect my function from displaying errors when something is wrong loading external files but I can’t find any documentation about this.

    Here’s the code:
    $rss = new DOMDocument();
    $rss->load($feedSource);

    $feed = array();
    foreach ($rss->getElementsByTagName(‘item’) as $node) {
    $item = array (
    ‘title’ => $node->getElementsByTagName(‘title’)->item(0)->nodeValue,
    ‘desc’ => $node->getElementsByTagName(‘description’)->item(0)->nodeValue,
    ‘link’ => $node->getElementsByTagName(‘link’)->item(0)->nodeValue,
    ‘date’ => $node->getElementsByTagName(‘pubDate’)->item(0)->nodeValue,
    );
    array_push($feed, $item);
    }

    return $feed;

    # July 3, 2013 at 3:51 pm

    Set [`display_errors`](http://php.net/display_errors) (in your php.ini file) to `Off`.

    just to be clear, however, _hiding_ the errors **does not qualify** as “error handling.”

    What error are you getting?

    # July 4, 2013 at 5:46 am

    In _this_ case hidding _is_ error handling. But not with display_errors off.

    I want to ensure that when the source of my feed goes down or change the structure of their xml document I’m able to hide the feed box from the website. Isn’t there any “try{ } catch{}” similar in PHP?

    # July 4, 2013 at 2:16 pm

    >In this case hidding is error handling. But not with display_errors off.

    Meaning, you want to make sure any _possible_ errors are not shown to the user? Yes, that’s good practice. The correct method to do so is to set `display_errors = off`.

    (It is possible to do change this setting programatically from within your php script, by using [`ini_set('display_errors','off')`](http://php.net/ini_set), but it is not always effective (because some errors occur before the script is actually executed, so the ini_set hasn’t happened yet).)

    However, to reiterate: _hiding_ errors, and leaving it at that, is not “error handling.” It’s “error ignoring.”

    >I want to ensure that when the source of my feed goes down or change the structure of their xml document I’m able to hide the feed box from the website. Isn’t there any “try{ } catch{}” similar in PHP?

    Yes, there is a `try{ … }catch` in PHP. However, it works with [exceptions](http://php.net/exceptions), not normal php errors. You would have to use [`set_error_handler()`](http://php.net/set_error_handler/) to convert your errors to exceptions, and then catch them.

    A better (simpler) approach would be to simply check if you’ve successfully fetched your feed before trying to use, instead of just assuming that everything is going to work:

    illustrative example; untested

    // loop through “rss”
    foreach ($rss->getElementsByTagName(‘item’) as $node) {

    // loop through each “item” for child nodes with desired names
    foreach(
    array(
    ‘title’ => ‘title’
    ,’desc’ => ‘description’
    ,’link’ => ‘link’
    ,’date’ => ‘pubDate’
    )
    as $indexName => $tagName
    ){

    // get the node list; _do not_ try to access it yet
    // (in case any expected nodes are missing)
    $childList = $node->getElementsByTagName( $tagName );

    // check length of nodelist
    $item[$indexName] = $childList->length?
    // if there is at least 1 item, assign its value
    $childList->item(0)->nodeValue:
    // assign a default value if the list is empty
    ‘some default value’
    ;
    }

    // add $item to $feed list
    array_push($feed, $item);
    }

    // I don’t know how you’re getting $rss,
    // but you can use a similar approach to handle any potential errors
    // (e.g., if the feed you retrieve is malformed).

    // if you’re using something like
    /*
    $rss = new DOMDcoument( ’1.0′,’utf-8′ );
    $rss->loadXML( $yourXMLstring );
    */
    // then you can just do
    /*
    $rss = new DOMDcoument( ’1.0′,’utf-8′ );
    if( $rss->loadXML( $yourXMLstring ) ){

    // all code below above goes in here

    }
    else{
    print “unable to load XML.”;
    }
    */

    # July 12, 2013 at 6:52 am

    The if solves it, thank you!

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".