There Must Be A Better Way…
I’m working on a photo gallery site that’s powered off Google’s Picasa service. Basically, you put in your email address, you name your gallery, and then you put in the RSS feed for your Picasa album, and out comes a nice, customizable gallery you can share with your friends. Three steps, thought it was a pretty cool idea, easy for anyone to use.
The trouble came when I tried to validate everything the user puts in. An email is easy, just check to make sure the email address follows the normal syntax xxx@xxx.xx and send it a test email to confirm. The gallery name is even easier, remove weird characters that would interfere with the URL and check that is doesn’t already exist. The Picasa RSS feed however, was a bit trickier.
I’m using PHP to power this app. To sort through the RSS feed, use a function called simplexml_load_file. That function takes a file I give it, and if it’s properly formatted XML, loads it into an easy to use format. For example, if I want to pull the title and body of each post out of the rss feed, I use the following:
$xml = simplexml_load_file("rssfeed.xml");
$items = $xml->channel;
foreach ($items->item as $item){
$title = $item->title;
$body = $item->description;
}
Pretty simple right? Well that’s the point of this simplexml_load_file function, it works real nice and easy. Here’s the problem though. Most php functions will present you with an error if you send them something they can’t read. For example, if I try to load a file into a page using an include(); function, I can use this code to catch any errors.
try {
if(!@include('/path/to/file.php')) {
throw new Exception('Failed to load');
}
}
catch(Exception $e) {
print $e->getMessage();
}
Again, makes sense right? Well that doesn’t seem to work with simplexml_load_file for some reason. If I pass an invalid file, or improperly formated XML, rather than catching an error and moving on, it takes down the code with crazy errors. I swear, I tried everything to get this to work, but no love. Now, there is a crazy complicated, 50 lines of code option, but that’s excessive for simple validation.
So if there’s no easy way to catch an error on a simplexml_load_file function, how can I possibly validate that the RSS feed a user passes is ok? Well, after hours of research and giving up for the night, it finally dawned on me. There’s no reason I need to load the XML to test if it’s valid. All I need to do is check the url for validity and test the file for something that’s specific to the RSS feed. Scanning through the XML, I found this little piece of markup:
<gphoto:allowPrints>true</gphoto:allowPrints>
Now I’m sure flickr isn’t using that XML tag anywhere. It’s gotta be from Google, and it’s gotta be for photos. All I need to do is confirm this XML exists in the file to confirm 99.9% that the feed is correct. The code for that looks something like this.
$filename = "rss.xml";
$string = "gphoto:allowPrints";
$feed = fopen ($filename,"r");
$contents = fread($feed,filesize($filename));
if(strstr($contents,$string)) {
echo "Valid";
} else {
echo "Invalid";
}
So what’s the point of this post? Well, I wasted at least 2 hours of my life trying to solve a problem the hard way, when I should have just thought about solving the problem the *right* way. I was so convinced that I had to figure out validation of that XML file that I never thought about any other options. Next time you’re stuck on something, just take a step back and think if there’s a better way of doing it instead.







