SimplePie 1.5 is now available!

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

Bypass “hotlink blocking” when displaying a feed (Beta 2) 3 Jun 2006 

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. We’ve added functionality that allows you to bypass hotlink protection for feeds that you’re trying to read online. But that isn’t what’s important. What’s important is that it’s been built in as a configuration option that is enabled by default. You don’t need to do anything to get this to work (and actually, if you’re using the JavaScript from the old version of the article, it may mess this up so be sure to remove it).

However, if you want to make sure it’s working, or otherwise force it to be on, take a look at the following code:

<?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->bypass_image_hotlink();
	$feed->init();
}
$feed->handle_content_type();

?><!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

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

Posted by Ryan Parman at 10:15 am. Comments (0)

Display non-english feeds correctly (Beta 2)  

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 handles this for you: handle_content_type().

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']);
	$feed->init();
}

// This is the part to pay attention to
$feed->handle_content_type();

?><!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:

$feed->handle_content_type();

What this does is:

  1. Checks to see if any content has been sent to the browser yet.
  2. If not, it checks SimplePie’s output encoding (which as of this release is always UTF-8).
  3. Creates the proper HTTP Headers that tell the browser to handle this page as UTF-8 and text/html.

You can also set the <meta> tag if you’d like, although the HTTP specification says that the information that gets sent by the server will override any <meta> tags that try to do the same thing.

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

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 all languages are translated into UTF-8 inside SimplePie anyways.

Posted by Ryan Parman at 9:59 am. Comments (1)

SimplePie Beta 2 is now available! 2 Jun 2006 

Grin

After several months of toiling over this release, and the past few weekends pouring over the documentation, we are now very proud to release SimplePie Beta 2!

Nearly every major feature has been enhanced, as well as the addition of several new ones. Be sure to read the upgrade docs if you’re upgrading from Beta 1 or the Preview Release. People who were using trunk builds should be able to just drop this file in as a replacement for any previous simplepie.inc file.

We’ve also added a WordPress plugin and a MediaWiki extension to the mix. And as always, if you have any questions, comments, or need clarification on anything, be sure to swing by the support forums. We’re there a lot. 🙂

Enjoy!

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

« Newer Posts

Older Posts »