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

How to upload a file from the front end (view) in an iphone app?

iphone objective-c cocoa-touch file-upload ios4

In my iphone app, It is a requirement that I enable the user to upload the database file (.sql) from the front end of the app or through iTunes and the app should display the data based on the file

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.