Browse Source

times: Switch from Navitia to custom GTFS source

We can probably make an option to use either Navitia or GTFS since we
have the code for both anyway.
master
Stéphane Bérubé 1 year ago
parent
commit
83987db0ba
2 changed files with 47 additions and 6 deletions
  1. +40
    -0
      classes/Gtfs.php
  2. +7
    -6
      public/times.php

+ 40
- 0
classes/Gtfs.php View File

@ -0,0 +1,40 @@
<?php
class Gtfs {
private $endpoint;
private $secret;
public function __construct($endpoint, $secret) {
$this->endpoint = $endpoint;
$this->secret = $secret;
}
public function getStopSchedule($stopId) {
// TODO: Error handling
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
$this->endpoint . '?stop=' . $stopId . '&agency=1');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'secret: ' . $this->secret
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
// TODO: Error handling
$json = json_decode($response);
$tmp_dateTimes = $json;
$dateTime = array();
foreach($tmp_dateTimes as $tmp_dateTime) {
$dateTime[] = strtotime($tmp_dateTime);
}
return json_encode($dateTime);
}
}

+ 7
- 6
public/times.php View File

@ -1,16 +1,17 @@
<?php
require_once('../classes/Navitia.php');
require_once('../classes/Gtfs.php');
$config = json_decode(file_get_contents('../config.json'));
// Navitia creds
$token = $config->navitia->token;
$stopId = $config->navitia->stopId;
// Gtfs configs
$secret = $config->gtfs->secret;
$endpoint = $config->gtfs->endpoint;
$stopId = $config->gtfs->stopId;
$navitia= new Navitia($token);
$gtfs = new Gtfs($endpoint, $secret);
$nextTrips = $navitia->getStopSchedule($stopId);
$nextTrips = $gtfs->getStopSchedule($stopId);
header('Content-Type: application/json');


Loading…
Cancel
Save