Created
December 3, 2014 05:48
-
-
Save timbennett/0646e39eba3766657fcd to your computer and use it in GitHub Desktop.
Download Roads and Maritime Services traffic report data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// this gets the data used to power charts at roadsreport.rms.nsw.gov.au | |
date_default_timezone_set('Australia/Sydney'); // doesn't matter | |
$date = '2013-09-02'; // earliest date for available data | |
// Set up the query loop (I should really do this by end date) | |
for ($i = 1; $i <= 460; $i++) { | |
// The data to send to the RMS API | |
$postData = array( | |
'routes' => array('44'), // 44 = M2 route number | |
'combineRoutes' => FALSE, | |
'dateRange' => array('start' => $date, 'end' => $date), | |
'timePeriod' => array('peak' => 'pm') | |
); | |
// Setup cURL | |
$ch = curl_init('http://roadsreport.rms.nsw.gov.au/backend/data-view.php'); | |
curl_setopt_array($ch, array( | |
CURLOPT_POST => TRUE, | |
CURLOPT_RETURNTRANSFER => TRUE, | |
CURLOPT_HTTPHEADER => array( | |
'Content-Type: application/json' | |
), | |
CURLOPT_POSTFIELDS => json_encode($postData) | |
)); | |
// Send the request | |
$response = curl_exec($ch); | |
// Check for errors | |
if($response === FALSE){ | |
die(curl_error($ch)); | |
} | |
// Decode the response | |
$responseData = json_decode($response, TRUE); | |
// Print the data from the response | |
echo $date .",".$responseData['aggregate']['locations']['0']['speed']['average'].",".$responseData['aggregate']['locations']['0']['speed']['percentOfSpeedLimit'].",".$responseData['aggregate']['locations']['0']['travelTime']['average']."\n"; | |
// increment the date | |
$date = strtotime("+1 day", strtotime($date)); | |
$date = date("Y-m-d", $date); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment