Getting JSON from National Rail Enquiries SOAP API

14th April 2017

As someone who's skills lie on the side of design and front-end development, I was initially overwhelmed when first looking at the National Rail Enquiries API. Using an API of any sort was new territory for me—my initial research into SOAP confused me, to say the least. Fortunately, I came across a couple of projects to help with this exact situation. Before continuing, I must mention that you're going to need an API access token, to get one you must register for OpenLDBWS on the National Rail Enquiries website.


The first solution I discovered was a project named "Huxley" by James Singleton. Once set up, Huxley acts as a middle man between your application and the SOAP API. Information can be retrieved from National Rail Enquires with a far simpler REST API and is returned in a JSON document which can also easily be converted to a PHP array with the json_decode() function. I've noticed the JSON returned from a Huxley response also seems to have been cleaned up somewhat, in comparison with a direct response from the SOAP API.

An example request to Huxley, requesting the next 25 departures from London Victoria:


However, there is a downside—Huxley runs as a separate application. Although this isn't difficult to setup, the response time can drop significantly in comparison to directly accessing the API. This is especially noticeable if hosted on a free Microsoft Azure instance, whereby the application effectively goes offline(or "cold") after around 15–20 minutes of inactivity. When a request is made in this state, the Azure instance takes a few seconds to load back up again, at which point it resumes to the usual speed. This is, of course, a criticism of Azure rather than of Huxley.


Although Huxley is a fantastic project, the response time just didn't cut it for the application I was building, especially as it is likely to be used in rural areas with next to no mobile phone reception. As I was building my application in PHP, I was lucky to stumble across "phpOpenLDBWS", written by RailAleFan, which describes itself as a simple PHP object encapsulation of Live Departure Boards Web Service. Effectively a series of functions that can be used to access the API directly with no knowledge of SOAP—phpOpenLDBWS takes care of that for you!

An example of phpOpenLDBWS being used to retrieve the next 10 departures from Leeds:


Similar to Huxley, the response is formatted far nicer than that of a direct SOAP request—with phpOpenLDBWS, a PHP object is returned. This can easily be converted to JSON with the json_encode() function. And to convert to a PHP array, just wrap the response in json_decode(json_encode()).


Overall, I would say Huxley is an excellent choice when first experimenting with the API. It allows you to get stuck into building your application with very little setup and easy to follow documentation.

After trying out both, I ultimately chose to use phpOpenLDBWS due to the speed and simplicity of the setup. Also, being able to include a single lightweight file into my project to access the National Rail Enquiries SOAP API was a no brainer.

← Back to all posts