SimplePie 1.5 is now available!

SimplePie Developer Weblog.  Not that we really have anything to say, but if you'll listen, why not?

SimplePie has a new license 30 Jan 2006 

License

I had a couple of people ask me about the license that we were using. Some suggested that since it was a Share-alike license, that they wouldn’t be able to use SimplePie in their project because the licenses were different.

After taking another look, I discussed possible solutions with Geoffrey. He suggested I look at the licenses at opensource.org. I looked through some of them—namely the zlib/libpng license, as well as the MIT license, but I felt that they were both a bit unstructured for what I was looking for. I went back to the Creative Commons site, and decided to settle on the Creative Commons Attribution License. Essentially, it’s as open as any of the “open source” licenses as far as use, copying, and distribution go, but it requires that users must leave all copyright notices intact—which was the main thing I was looking for.

So there you go. You can use SimplePie however you want, in whatever projects you want, as long as the copyright notice in the source code stays in place. Happy distributing!

Posted by Ryan Parman at 12:15 am. Comments (1)

Display non-english feeds correctly (PR/Beta 1) 29 Jan 2006 

This tutorial has been superceded by a newer version.

Have you ever had problems displaying feeds properly on your pages? I ran into an issue a few times when I was trying to display an iTunes Music Store feed, and Beyoncé kept coming out as Beyonc[enter-garbled-text-here]. I soon realized that the problem occurred because my pages were being served as ISO-8859-1, and the iTMS feed was being sent as UTF-8.

The solution is really quite simple.

All you have to do is make sure that the page is being served and handled in the same character set that the feed is. Fortunately, SimplePie has a built-in function that lets you determine what the feed is being served in: get_encoding().

Ideally, when you’re loading a page with SimplePie in it, you’ll do that part at the very beginning of the page. Doing so will help you serve the page correctly.

<?php
// Start counting time for loading...
$starttime = explode(' ', microtime());
$starttime = $starttime[1] + $starttime[0];

include('../simplepie.inc');

// Parse it
$feed = new SimplePie();
if (!empty($_GET['feed'])) {
	$feed->feed_url($_GET['feed']);
	if (isset($_GET['xmldump'])) {
		$feed->enable_xmldump($_GET['xmldump']);
	}
	$feed->init();

	// This is the part to pay attention to
	if (!headers_sent() && $feed->get_encoding()) {
		header('Content-type: text/html; charset=' . $feed->get_encoding());
	}
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>

We specifically want to focus in on this chunk:

if (!headers_sent() && $feed->get_encoding()) {
	header('Content-type: text/html; charset=' . $feed->get_encoding());
}

The logic here is simple:

  1. As long as page headers haven’t been sent yet
  2. And the feed encoding has been detected
  3. Then add the encoding to the headers and send them to the browser

Make sense? Hopefully so, because it can’t get too much simpler. 🙂

There’s another part of this that is also important: the <meta> tag. All we have to do is make sure that the right encoding is set in the right meta tag. Let’s take a look:

<meta http-equiv="Content-Type" content="text/html;charset=<?php echo $feed->get_encoding(); ?>" />

Just make sure that this meta tag is included in your SimplePie-enabled pages, and your page will always be served with the correct character encoding for the page (as long as the character encoding is supported by SimplePie, and as of 1.0 Beta, a large number are).

If you’re loading a feed into a page dynamically (like in our Delicious AJAX demo), your best bet is to serve the page as UTF-8, since most languages are translated into UTF-8 inside SimplePie anyways. “When in doubt, serve as UTF-8.”

Posted by Ryan Parman at 11:46 pm. Comments (0)

Bypass “hotlink blocking” when displaying a feed (Beta 1)  

This tutorial has been superceded by a newer version.

In my quest for making a feed parser that is intelligent, simple, and graceful, one thing that has always bugged me about online feed readers is that some people disable the ability to hotlink images.

Now, I understand why they do this, because I do it too. You don’t want a bunch of your bandwidth sucked up by people who are stealing your images for their own nefarious purposes (mwah-hah-hah!). Rather, you’d prefer to keep the images for your readers who are reading your content. Well, that’s exactly what a feed parser is for, right?

Desktop aggregators like Feed Demon and NetNewsWire are always able to just load up the images in context with the post that they’re reading, and it all makes sense. The only reason why online feed readers have a problem is because the browsers that run them respect the hotlinking rules—even if the reasons don’t make sense for the context (like trying to apply laws about CD’s to MP3’s—although they’re related, they’re different, and the rules need to be modified for the new medium).

So, we’ve decided to solve the problem in our latest release. Here’s how to bypass hotlink protection for feeds that you’re trying to read online. We’ve added a new function called display_image() that will take the URL of the image and display it—blocked or not.

Here’s how it’s done. First we want to take our page (I’ll be using the demo page that is included with the SimplePie download), and make a small modification to the head of the page. We’re going to take the source code, and add the bolded part below:

<?php
// Start counting time for loading...
$starttime = explode(' ', microtime());
$starttime = $starttime[1] + $starttime[0];

include('simplepie.inc');

// Parse it
$feed = new SimplePie();
if (!empty($_GET['feed'])) {
	$feed->feed_url($_GET['feed']);
	$feed->init();
	if (!headers_sent() && $feed->get_encoding()) {
		header('Content-type: text/html; charset=' . $feed->get_encoding());
	}
}
else if (!empty($_GET['i'])) {
	$feed->display_image($_GET['i']);
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<title>SimplePie: Demo

What did we just do? We told the page that:

  1. If we're not passing the feed parameter to the page (like index.php?feed=url_goes_here), then check for the i parameter. (I chose i because it's short for "image". You can choose anything you want.)
  2. If the i parameter exists, pass it to the display_image() function.

The display_image() function is an exception to standard SimplePie processing in that it doesn't return any value. It automatically echo's the image content to the page. So, if load up our page like index.php?i=url_of_image, we should see the image— and only the image—on the page.

Now, we just need to modify the value of all the <img src="" /> tags on the page, so that they all get passed through our new script. Specifically, we're looking for all off-site images, not our own local ones, so let's make sure to target those.

The simplest way is with JavaScript, although you're free to use other languages if you prefer. I wrote something that looks like this (I assumed an XHTML page, and I'm using an anonymous function):

<script type="text/javascript">
//<![CDATA[

(function(){

	// Get an array of all of the <img> tags in the page
	var img = document.getElementsByTagName('img');

	// Count how many there are, and store that number for faster processing
	var imgLength = img.length;

	// Loop through all of the images.  Unfortuately, JavaScript doesn't have a foreach() function.
	for (var x=0; x<imgLength; x++) {

		// Check to make sure that the image we have is off-site
		if (img[x].src.substring(0,4) == 'http') {

			// Pass the URL as the value for the 'i' parameter.
			img[x].src = '?i=' + img[x].src;
		}
	}
})();

//]]>
</script>

If you place this script at the very bottom of your page, right above the closing </body> tag, the script will re-write your external image URL's to have them pass through our function, and they'll all show up on your feed reader page, just like they do in the desktop aggregators.

Have fun! This code has been implemented in the demo that comes with the SimplePie 1.0 Beta download.

Posted by Ryan Parman at 11:46 pm. Comments (0)

« Newer Posts

Older Posts »