Overview
Last month the City of Chicago arranged for a Open Data Hackaton in which a collection of programmers gathered together to develop and write programs that utilize a new resource, open access to city information.
For my part, I spent the data writing a PHP class file that wraps around the Chicago Transit Authority‘s web-based application programming interface, enabling access to CTA bus, rail and service information for PHP driven applications. As I’ve noted in the README file, “this class brings all three APIs together into one object with related methods.”
The following in a quick rundown of how to incorporate this new class file into a working PHP application.
Installation
The first step is to download the class.cta.php file from GitHub and save it in a location that the PHP application has read access.
The next step is to include the file using the include (or similar require) function in the PHP application itself:
Once the class file has been loaded, the next step is to instantiate the class:
Notice that initialization of transit includes providing two API keys. API Keys can be requested from the CTA. For an API Key for Train Tracker, use the Train Tracker API
Application form. For Bus Tracker, first sign into Bus Tracker, then
request an Developer Key under “My Account”.1
If no valid API keys are provided the only methods that will return valid information are the Customer Alert functions for system status information. Specificity the two functions statusRoutes and statusAlerts. This is because the Customer Alert API does not require an API key for access.
Execution
To invoke a method simply use the object and related function, providing any additional information as parameters, if required. For example, to get information about all of the bus stops the east-bound route 81 bus makes:
All methods return an array which can be accessed to retrieve desired information. PHP’s print_r or var_dump functions provide insight into all information returned by a specific function:
The output will look something akin to this:
In order to generate the following output listing the location of the Lawrence & Kimball stop:
The following PHP code will provide the latitude and longitude of the Kimball stop, which is also a transfer point to the El’s Brown Line:
Notice that while the list of stops is provided in an array, each element in the array is a SimpleXMLElement object, thus the use of the object syntax for accessing each element.
The train function will allow for the determination of rail information, for example when the next Brown line train will be leaving the Kimball stop. However, while the previous example included a stop id for the route 81 bus at Kimball, the stop id is unique to the route 81 bus and does not translate to the stop id of the Brown line El at Kimball. Therefore, the first step is to locate the relevant GTFS2 data for the Kimball station:
Which provides output similar to the following:
One should note that the Brown line stop at Kimball is the northern end point for the Brown line, which means any trains leaving the station will only be bound in one direction, south, toward the Loop. If the string comparison is changed to ‘irving’ for the Irving Park station, the output changes to something similar, with trains running in both directions:
In Review
class.cta.php is a single PHP class file that provides access to all three CTA APIs for Bus, Train and Service information. The class implement functions for access to all API methods and returns an array of SimpleXMLElement objects that a PHP developer can use to incorporate real-time information about Chicago’s public transit system.
Additional information about the CTA’s APIs, including terms of use and how to request API Keys, can be found on the CTA’s Developer page.
1 Why
two different API Keys, one for train and one for bus information?
Due to the evolution of the CTA’s API interfaces, there are three
distinct APIs, one for Bus, Train and Customer Alerts information.
As a result there are three distinct URI endpoints and two distinct
API keys.
2 The
CTA provides its data based on the Google Transit Feed Specification
(GTFS) which is becoming a common format for public transportation
agencies to publishing schedules and associated geographic
information. The CTA generates and distributes, about once a week,
an up-to-date zip compressed collection of files that includes basic
agency information, route, transfer and stop locations, and other
related service information. Note that ids 0-29999 are bus stops,
ids 30000-39999 are train stops and 40000-49999 train stations
(parent stops).