PTV Timetable API sample code for PHP

API Page: https://www.ptv.vic.gov.au/about-ptv/ptv-data-and-reports/digital-products/ptv-timetable-api/ptv-timetable-api-reference/

 

1. Request API key by sending email

How to register for a key and developer ID
> Send an email to [email protected] with the following information in the subject line of the email:
• “PTV Timetable API – request for key”
> Once we’ve got your email request, we’ll send you a key and a developer ID by return email.

2. Run sample code

<?php 
	$userID = "<userID as developer ID>";
	$apiKey = "<API Key>";

	$requestURLHost = "http://timetableapi.ptv.vic.gov.au";
	$requestURL = "/v2/nearme/latitude/-37.82392124423254/longitude/144.9462017431463?devid=".$userID;
	$signature = hash_hmac("sha1", $requestURL, $apiKey);

	echo "signature => ".$signature.PHP_EOL;

	$finalURL = $requestURLHost.$requestURL."&signature=".$signature;
	echo "finalURL => ".$finalURL.PHP_EOL;

	$ch = curl_init(); 
	curl_setopt($ch, CURLOPT_URL, $finalURL); 
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
	$response = curl_exec ($ch); 
	curl_close ($ch); 

	echo "response => ".$response;

Response

 

$ php PTVTimetableAPI.php                                                                                                 
signature => XXXXXXXXXXX                                                          
finalURL => http://timetableapi.ptv.vic.gov.au/v2/nearme/latitude/-37.82392124423254/longitude/144.946201743146
3?devid=XXXXXXXX&signature=XXXXXXXXXXXX                                             
response => [                                                                                                  
  {                                                                                                            
    "result": {                                                                                                
      "distance": 158.73349,                                                                                   
      "suburb": "Southbank",                                                                                   
      "transport_type": "bus",                                                                                 
      "route_type": 2,                                                                                         
      "stop_id": 21722,                                                                                        
      "location_name": "West Gate Fwy/Lorimer St ",                                                            
      "lat": -37.82509,                                                                                        
      "lon": 144.94516                                                                                         
    },                                                                                                         
    "type": "stop"                                                                                             
  },                                                                                                           
  {                                                                                                            
    "result": {                                                                                                
      "distance": 274.8378,                                                                                    
      "suburb": "Docklands",                                                                                   
      "transport_type": "tram",                                                                                
      "route_type": 1,                                                                                         
      "stop_id": 2490,                                                                                         
      "location_name": "D17-Merchant St/Collins St ",                                                          
      "lat": -37.82153,                                                                                        
      "lon": 144.945419                                                                                        
    },                                                                                                         
    "type": "stop"                                                                                             
  },                                                                                                           
  .....
]