In Accessing the CTA’s API with PHP I outlined a class file I created1
for querying the Chicago Transit Authority’s three web-based application programming interfaces (APIs). However, that isn’t the only open data development I’ve been working on recently, I’ve also been working on a class file for accessing the City of Chicago’s Open Data Portal.
The City of Chicago’s Open Data Portal is driven by a web application developed by Socrata. Socrata’s platform provides a number of web-based API methods for retrieving published datasets2. class.windy.php provides the definition for a PHP object that wraps around Socrata’s API providing PHP written apps access in turn to the city’s open data.
Installation and Instantiation
The first step is to download the class.windy.php file from GitHub and save it in a location that the PHP application can access.
The next step is to include the file using the include function in the PHP application itself:
Once the class file has been loaded, the next step is to instantiate the class:
Note that initialization of of the new object requires providing the keyword ‘city’. Since the City of Chicago, Cook County and the State of Illinois all currently use Socrata’s platform for sharing government data, class.windy.php supports accessing all three data portals – more on accessing county and state data later.
Extra parameters can be provided during the construction of the object for additional customization. For example, by default, this class will request data in JSON form. But, Socrata does support other data formats3 so the PHP developer can have a bit of a choice as to how to handle and process the raw data, if desired.
Chicago Data
Let’s say for example that one is looking for information about Chicago’s roughly 200 neighborhoods. More specificity information about the rough boundaries around Chicago neighborhoods. Using the getViews4 method that class.windy.php provides, one can query for any dataset where the description might include the keywords ‘neighborhood boundaries’ and are tagged as KML:
Depending on what datasets are available the output of this query may include the follow result:
Perhaps, using Google Maps, one wishes to create an interactive map of Chicago neighborhoods? Or better yet a closer look at the boundaries of a specific area in the city? Retrieval of the KML file can be done via the getFileByViewID function:
The above chunk of code nested in the previous example code’s foreach loop compared the $view->id value of each dataset looking for a match to the specific KML file of Chicago neighborhoods. Once found, the getFileByViewID method, given the file id, found in $view->blobID and the known view id sends a query for and retrieval of the file from data portal.
Once the file has been loaded, the KML file, a type of XML format, can be parsed using the simplexml_load_string function which returns a SimpleXML object and can be traversed as needed:
Thus the output of our search for the boundaries of Chicago’s Albany Park neighborhood results in the the following geographic data points:
Cook and Illinois Data Portals
As previously mentioned the Cook County and the State of Illinois also currently use Socrata’s platform for sharing government data. For the moment that makes supporting city, county and state open data with one class file a trivial manner:
In Review
class.widy.php is a single PHP class file that primarily provides access to City of Chicago’s data portal. The class implements functions for accessing all API methods and by default returns an object that a PHP developer can use to incorporate information about the City of Chicago into their PHP based application.
Since Cook County and the State of Illinois have also adopted Socrata’s data platform, support for these additional data portals using Socrata’s Open Data Platform has been included as a secondary feature of the class file.
1 And which can be found on GtHub
2 Known as SODA, a standards-based, RESTful application programming interface.
3 Such as XML, RDF, XLS and XLSX (Execl), CSV, TXT and PDF.
4 getViews is one of a collection of methods Socrata refers to as ViewsService. This collection of API calls provide for the retrieval and manipulation of datasets and metadata about datasets.
I just discovered a typo on line 449 of class.windy.php. There’s a missing / after $viewID that makes for an incorrect URL that leads to 404.
Sure looks like it.
I’ve updated the code on GitHub to fix this typo, https://github.com/pdweinstein/PHP-Wrapper-for-Chicago-s-Data-Portal/commit/2265fe65624fac331b61ac6d82562056ce645db4
Thanks for letting me know.