Ben Dodson

Freelance iOS, macOS, Apple Watch, and Apple TV Developer

RSS Feed of the London Underground Tube Status

Please note that this page is now outdated - for any updates to the London Underground, you should use my dedicated Tube Status API and RSS feeds

I've been spending some time making an RSS feed of the London Underground tube status as I've been working on a few projects that could use one. You would think that Transport for Londons nice web 2.0 style website would provide this basic requirement but alas they don't. There are a few developers out there who have written some scraping scripts to convert this information, but most of them no longer work as they haven't been updated to work with the new site.

Previously I have used the script at conor.net to power any projects requiring a tube RSS feed but this went down a few weeks ago when I was in the middle of some crucial testing. So, like any other crazy developer would, I decided to write my own PHP scraping script that would generate me a nice RSS feed to basically tell me TFL had messed up my journey home!

For those of you just looking for an RSS feed of the London Underground tube delays, you can find it at http://tubeupdates.com/rss/ - This is automatically updated every minute and will email me if anything goes wrong so I can fix it! If you are going to use it for any projects, please let me know so that I can monitor bandwidth, etc. If you are interested in how it works, then the code that powers this is below - feel free to use and modify it for your own projects:

<?php

// This file should be run on a CRON job every 5 minutes.

// The server will need to be able to write to this folder in order to create the XML file.


$array = array(
        'bakerloo',
        'central',
        'circle',
        'district',
        'east london',
        'hammersmith & city',
        'jubilee',
        'metropolitan',
        'northern',
        'piccadilly',
        'victoria',
        'waterloo & city'
        );

$i = 0;
foreach ($array as $ref) {
    $id = str_replace(' ','',$ref);
    $id = str_replace('&','and',$id);

    $lines[$i]['id'] = $id;
    $lines[$i]['name'] = ucwords($ref);
    $lines[$i]['url'] = 
        'http://www.tfl.gov.uk/tfl/livetravelnews/realtime/tube/tube-' . $id . '-now.html';

    $i++;
}

$i = 0;
foreach ($lines as $line) {
    $file = file_get_contents($line['url']);
    $needle = strtoupper($line['ref']) . ' LINE:';
    $pos = strpos($file,$needle);
    if ($pos !== false) {
        $pos += strlen($needle);
        $end = strpos($file,'</div>',$pos);
         $status = strip_tags(substr($file,$pos,($end-$pos)));
    } else {
        $status = 'A good service is currently running.';
    }
    
    $lines[$i]['status'] = $status;
    $i++;
}

$now = date('r');

$xml = <<<XML

<rss version="2.0">
    <channel>
        <title>London Tube Updates</title>
        <link>http://www.tfl.gov.uk/tfl/livetravelnews/realtime/tube/default.html</link>
        <description>The latest London Underground tube update information</description>
        <language>en</language>
        <pubDate>$now</pubDate>
        <lastBuildDate>$now</lastBuildDate>
        <docs>http://tube.bendodson.com/weblog/</docs>
        <webMaster>ben@bendodson.com (Ben Dodson)</webMaster>
        <generator>PHP script by Ben Dodson</generator>
        <ttl>5</ttl>
        <image>
            <title>Transport for London</title>
            <url>http://www.tfl.gov.uk/tfl-global/images/roundel.gif</url>
            <link>http://www.tfl.gov.uk/</link>
            <width>52</width>
            <height>44</height>
            <description>Transport for London Logo</description>
        </image>
XML;

foreach ($lines as $line) {
    $xml .= '<item>' . "\r\n";
    $xml .= '<title>' . htmlentities(ucwords($line['name'])) . '</title>' . "\r\n";
    $xml .= '<link>' . $line['url'] . '</link>' . "\r\n";
    $xml .= '<description>' . htmlentities($line['status']) . '</description>' . "\r\n";
    $xml .= '</item>' . "\r\n";
}
        
$xml .= '</channel>' . "\r\n";
$xml .= '</rss>';

file_put_contents('rss.xml',$xml);

?>

As I mentioned earlier, I am working on a few projects that utilise this script and I shall be making those available shortly. However, for a sneak peek, you can check out Tube Updates, an API for any developer that would like to make use of this data. There are still a few improvements to be made but you get the general idea!

New website goes live! » « Firefox extensions updated for Beta 3 compatibility

Want to keep up to date? Sign up to my free newsletter which will give you exclusive updates on all of my projects along with early access to future apps.