Reading RSS with PHP

After using the JavaScript only and generally pretty poor facility within Feedburner to embed RSS feeds onto a website, I set about an alternative solutions.

Thankfully, there are a number available. However, many offer little embedding options and I wanted something that gave me full control. This narrowed it down to 2 free downloadable scripts. However, as much as I tried I couldn’t get them to work properly.

Everything went well until it came to a link in my item description – then the less than/greater than signs would mysteriously disappear. Bizarrely, if I used the option to run it from the authors script from their site, it worked. So I can only assume it’s a configuration on my server. However, I’ve not been able to work out what it is.

Now, PHP has built in XML parsing options and this is what these scripts were using – and I’m guessing the configuration option in question was to do with that. So I gave up and decided to write my own alternative. It doesn’t use XML parsing but simply reads the whole file in and extracts the details I need – in this case title, description and date. The code is below – pass the RSS feed into it by assigning it to the variable $rss_feed.

$array=file_get_contents($rss_feed);
$i=0;
while ($i!=1) {
  $item_start=strpos($array,"<item>");
  if ($item_start===false) {
      $i=1;
   } else {
      $item_end=strpos($array,"</item>");
      $item_length=$item_end+7-$item_start;
    $item_strip=substr($array,$item_start,$item_length);

      $title_start=strpos($item_strip,"<title>");
    $title_end=strpos($item_strip,"</title>");
     $title_length=$title_end+8-$title_start;
    $title=substr($item_strip, $title_start+7, $title_length-15);

      $pubdate_start=strpos($item_strip,"<pubDate>");
      $pubdate_end=strpos($item_strip,"</pubDate>");
    $pubdate_length=$pubdate_end+10-$pubdate_start;
      $pubdate=date("jS F Y",strtotime(substr($item_strip,
$pubdate_start+9, $pubdate_length-19)));

      $description_start=strpos($item_strip,"<description>");
    $description_end=strpos($item_strip,"</description>");
     $description_length=$description_end+14-$description_start;
      $description=substr($item_strip, $description_start+13,
$description_length-27);

    $array=substr($array,$item_end+7);
 }
}

I’ve left some of the code expanded – for example there is no need for each field to be calculating and storing the end position as this could simply be placed within the length calculation. However, so you can see what I’m doing I’ve left it as it is.

So, during each iteration you will have 3 fields available for you to use, and probably display. They are $title, $pubdate and $description. $pubdate is converted into a more readable format using the date command, but the parameters can be easily changed to format it in another way.

In

Talk to me!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from David Artiss

Subscribe now to keep reading and get access to the full archive.

Continue reading