Wednesday, March 24, 2010

Broken Twitterfeed URLs

Recently, a friend of mine introduced me to Twitterfeed, a service that consolidates RSS feeds with Twitter, a popular notification system.

Though I'm not an avid follower of RSS feeds, I use Twitter as a primary method of receiving news, updates from friends, and application-related notifications, so my friend's immediate suggestion - to use Twitterfeed as a way to keep up on Craigslist jobs - seemed like a great idea.

Unfortunately, Twitterfeed adds two Google Analytics variables to website URLs, which, while not an issue with dynamic pages, presents problems with Craigslist's static HTML pages.

Example:

http://www.craigslist.org/about/best/nyc/1584333712.html?utm_source=twitterfeed&utm_medium=twitter

The bolded section above shows the problem; adding variables to a static page breaks the page, and if you click the link above you'll see that the page cannot be found.

There's a Craigslist-specific fix for this below; it's not great, but it gets the job done.


<?php
$website="yoursite.com";
if ($_GET['forum']&&$_GET['city'])
{
$forum=$_GET['forum'];
$city=$_GET['city'];
$rss=file_get_contents("http://$city.craigslist.org/$forum/index.rss");
$new_link="http://$website/craigslistrss.php?link=";
$rss=preg_replace("/<link>(.*?)<\/link>/", "<link>$new_link$1", $rss);
echo $rss;
}
else if ($link=$_GET['link'])
{
header("Location: $link");
}
?>


Just upload the above code to your server as craigslistrss.php and change $website to the correct URL and path. You can now integrate RSS feeds from Craigslist with Twitter without breaking the page; just link to your script instead of directly to the RSS feed in the following format:

http://yoursite.com/craigslistrss.php?city=columbus&forum=sad

That's it! Hopefully Twitterfeed will integrate an option to disable Analytics variables soon, but in the meantime that will get the job done.