I wrote a simple function that takes a url to a RSS feed, creates a SimpleXmlElement object from the xml using simplexml_load_file, parses it and displays the info:
function readRSS($url)
{
//assign the file to an object
$rss = simplexml_load_file($url);
// format and output content
}
//call the function using CNN's top story feed
readRSS("http://rss.cnn.com/rss/cnn_topstories.rss");
This works fine for me but I am not sure how to check to see if the file is there before using simplexml_load_file. I have tried:
if(file_exists($url))
{
$rss = simplexml_load_file($url);
}
But that returns false even when the url is valid. I also tried
if($rss = simplexml_load_file($url))
{
// do stuff
}
But, as expected that didn't work. I could just suppress errors using @ so nothing gets displayed if the url is not valid but that seems like a sloppy solution to me. Could anyone suggest another way to handle broken urls when used in this manner? Any advice is appreciated, thanks much.

New Topic/Question
Reply




MultiQuote






|