Error handling in “simplexml_load_file”

I am having problem handling error while simplexml_load_file in my php. I am trying to parse the fox_sports xml, and store the date in my datebase. My code looks like below:

$foxs_url_breaking_news = 'http://feeds.news.com.au/public/rss/2.0/fs_breaking_news_13.xml';
    $foxs_xml_breaking_news = simplexml_load_file($foxs_url_breaking_news, 'SimpleXMLElement', LIBXML_NOCDATA);
    foreach($foxs_xml_breaking_news->channel[0]->item as $item)
    {   
        $date = date('Y-m-d H:i:s',strtotime($item->pubDate));
        $news->insert(array('source' => 'foxsports',
                            'headline' => addslashes($item->title),
                            'timestamp' => $date,
                            'description' => addslashes($item->description),
                            'category' => 'Breaking News',
                            'link' => addslashes($item->link)));            
    }

The code that i have works fine. But the problem is that I want to make it a long term solution. So I need some error handling for: 1. If the link for the xml is not available anymore. 2. If the simplexml_load_file is unable to load the xml file from the link. 3. If simplexml_load_file is unable to load the xml, then the foreach should not be executed either, as it will give "Invalid argument supplied for foreach()"

Or if you think I need to take care of something else too, to make this a permanent solution, do let me know that too.

Answers


The function returns false on failure, therefore, before your loop, check to see if $foxs_xml_breaking_news is false.

$foxs_xml_breaking_news = @simplexml_load_file($foxs_url_breaking_news, 'SimpleXMLElement', LIBXML_NOCDATA);
if($foxs_xml_breaking_new === false)
{
    //cannot fetch file
}
else
{
     // foreach loop...
}

Couple of things that helped me to solve my problem:

$foxs_url_breaking_news = 'http://feeds.news.com.au/public/rss/2.0/fs_breaking_news_13.xml';
$foxs_xml_breaking_news = @simplexml_load_file($foxs_url_breaking_news, 'SimpleXMLElement', LIBXML_NOCDATA);
if(@simplexml_load_file($foxs_url_breaking_news, 'SimpleXMLElement', LIBXML_NOCDATA))
{   
    foreach($foxs_xml_breaking_news->channel[0]->item as $item)
    {   
        $date = date('Y-m-d H:i:s',strtotime($item->pubDate));
        $news->insert(array('source' => 'foxsports',
                            'headline' => addslashes($item->title),
                            'timestamp' => $date,
                            'description' => addslashes($item->description),
                            'category' => 'Breaking News',
                            'link' => addslashes($item->link)));            
    }
}

So: I have used @ in front of simplexml_load_file to avoid error message in case it is unable to load the file And second: I have the whole "@simplexml_load_file($foxs_url_breaking_news, 'SimpleXMLElement', LIBXML_NOCDATA)" in my IF statement, instead of $foxs_xml_breaking_news. I dont have an idea why is this working and not when i have $foxs_xml_breaking_news in my IF was not working.

I even tried if($foxs_xml_breaking_news === FALSE) but even this did not work for me.


Need Your Help

Oracle date format — data reader not recognizing to_char function

c# oracle

Basically just pulling a date out in YYYYMMDD and I can do this fine in a query analyzer but when I try to run this query in C# I keep getting a column not found error it also seems to have a hard ...