开发者

Error handling in "simplexml_load_file"

开发者 https://www.devze.com 2023-01-29 06:25 出处:网络
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:

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 l开发者_JAVA百科oad 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消