Archive

Posts Tagged ‘rss’

Creating A Personalised Events List With SimplePie

November 28th, 2008 No comments

One nice feature for any blog or site (especially news sites) is to have a little list of forthcoming events that would interest your readers. One way of collating this information is to scour the web and update the list manually. However, this is time consuming and tedious, especially as there is an easier way to do it.

Have a look at the Yahoo site called Upcoming. On this site you can search for events on lots of different subjects in lots of different locations all over the world. Also, if you have an event you can put it up on the site.

Because all of the searches can be accessed via RSS it is possible to use SimplePie to collate these results together. The following script will take two arrays, one of locations and the other being the subjects. It will then create the necessary RSS URLs and use the multi-feed aggregator feature of SimplePie to collate all of the different events into a single array. All the script needs to do after this is run through the array and remove duplicates and sort the data by date.

<?php
include('simplepie.inc');
 
// enter a list of areas
$locations = array('england','britain','europe');
// enter a list of subjects
$subjects = array('php','web development','code','javascript');
 
// initialise feed array
$feeds = array();
 
// set up feed array
foreach($locations as $location){
  foreach($subjects as $subject){
    $feeds[] = 'http://upcoming.yahoo.com/syndicate/v2/search_all/?q='.urlencode($subject).'&loc='.$location.'&rt=1';
  }
}
 
// create SimplePie object
$feed = new SimplePie();
// add feeds to SimplePie
$feed->set_feed_url($feeds);
// turn off feed ordering by date
$feed->enable_order_by_date(false);
 
// get the feed contents
$feed->init();
 
// initialise previously done feed ID array
$prevIds = array();
 
// initialise item array
$list = array();
 
// for each feed item
foreach ( $feed->get_items() as $item ) {
  // check if we have not done this item before
  if ( !in_array($item->get_id(true), $prevIds) ) {
 
    // extract and convert the xCal:dtstart variable
    $when = $item->get_item_tags('urn:ietf:params:xml:ns:xcal', 'dtstart');
    $date = $when[0]['data'];
    $sortDate = SimplePie_Misc::parse_date($date);
    $gCalDate = date('j M Y', $sortDate);
 
    // check if the date is already in the list
    if ( isset($list[$sortDate]) ) {
      // try 10 attempts to increase the sort date timestamp and insert date
      for ( $i=0 ; $i < 10 ; ++$i ) {
        ++$sortDate;
        if ( !isset($list[$sortDate]) ) {
          $list[$sortDate] = '<li><a href="'.$item->get_permalink().'" title="'.$item->get_title().'">'.$item->get_title().'</a></li>'. "\n";
          break;
        }
      }
 
    }else{
    // store item in list array using the date as a
    $list[$sortDate] = '<li><a href="'.$item->get_permalink().'" title="'.$item->get_title().'">'.$item->get_title().'</a></li>'. "\n";
    }
    // store a hash of the ID so that the same thing isn't added twice.
    $prevIds[] = $item->get_id(true);
  }
}
 
// sort array by keys
ksort($list);
 
$list = array_slice($list,0,10);
 
echo '<ul>';
echo implode(' ',$list);
echo '</ul>';
 
?>

Rather than explain every step in turn I have just added comments to explain what is going on. Also, in this example I have searched for events in England and Europe that deal with php, web development, code and javascript. However, it seems that PHP isn’t such a good keyword to use as it comes up with any event that contains a link with a .php extension. I’m sure this case would be similar with things like HTML so it is best to avoid those keywords for now.

Categories: PHP Tags: , , , , , ,

Display WordPress Feeds On Your Site With SimplePie

February 20th, 2008 6 comments

You can display your latest WordPress posts anywhere on your site by using an RSS reader called SimplePie and a few lines of code. SimplePie is a fast and efficient RSS reader, and it will also cache feeds to reduce the amount of processing time taken.

Download simple pie from the website and upload the simplepie.inc file to your web server. Next include the following section of code anywhere on your site that you want to display the latest post on.

<?php
require_once('simplepie.inc');
 
$feed = new SimplePie();
$feed->set_feed_url('http://www.example.com/feed');
 
$feed->init();
 
echo '<ul>';
 
// Limit the items to be shown
$i = 1;
foreach($feed->get_items() as $item){
 if($i<3){
  echo '<li><a href="'.$item->get_permalink().'">'.$item->get_title().'</a><br />'. "\n";
  echo substr($item->get_description(),0,180).'...</li>'."\n";
  $i++;
 }
}
echo '</ul>';
?>

You can also pull out categories by setting the feed URL to something like this.

http://www.example.co.uk/category/example/feed

This will allow you to display a page will posts from different categories.

Of course you can use this technique on any RSS generating blog platform, it doesn’t have to be WordPress, but it is much better than trying to figure out database connections and the like.

Categories: PHP Tags: , , , , , ,

Linking An RSS Feed to a HTML Document

December 20th, 2007 No comments

Adding a hyper link for an RSS or Atom feed on your web page works, but that’s not all you can do. By adding a link to the head section of the page you can allow your users an alternative method of picking up your feed.

To add an RSS feed use this.

<link rel="alternate" type="application/rss+xml" href="http://www.example.com/rssfeed.xml" />

To add an Atom feed use this.

<link rel="alternate" type="application/atom+xml" href="http://www.example.com/JustBlog/wp-atom.php" />

These are both XHTML examples. To do this in HTML just remove the slash on the right hand side like this.

<link rel="alternate" type="application/rss+xml" href="http://www.example.com/rssfeed.xml" />
<link rel="alternate" type="application/atom+xml" href="http://www.example.com/JustBlog/wp-atom.php" />

You can add more than one link attribute to a page with no ill effects. Most modern browsers will give you a list of feeds to select from.

Categories: (X)HTML Tags: , , , , ,