Search code examples
phpmysqlreal-timegtfs

GTFS Realtime php


I have a MySQL database setup with GTFS data and I have php scripts that return stop times and I'm looking to add realtime data.

I know Google has instructions for how to use gtfs realtime with php (https://github.com/google/gtfs-realtime-bindings-php) but I can't seem to figure out how to get it working.

What I want to do is (using whatever informations is needed: trip_id, stop_id, etc.) return the trip_update delayed value so that I can update the stop time accordingly.

Does anyone know of a good tutorial? Or can someone help me figure out how to do this?


Solution

  • A more complete GTFS-realtime PHP example might look like the following:

    foreach ($feed->getEntityList() as $entity) {
      if ($entity->hasTripUpdate()) {
        $trip = $entity->getTripUpdate();
        error_log("trip id: " . $trip->getTrip()->getTripId());
        foreach ($trip->getStopTimeUpdateList() as $stu) {
          if ($stu->hasArrival()) {
            $ste = $stu->getArrival();
            error_log("    arrival delay: " . $ste->getDelay());
            error_log("    arrival time: " . $ste->getTime());
          }
          if ($stu->hasDeparture()) {
            $ste = $stu->getDeparture();
            error_log("    departure delay: " . $ste->getDelay());
            error_log("    departure time: " . $ste->getTime());
          }
        }
      }
    }
    

    Notice how the method names correspond to the fields in the underlying GTFS-realtime schema:

    https://developers.google.com/transit/gtfs-realtime/gtfs-realtime-proto

    You can see the PHP source that was generated from the schema at:

    https://github.com/google/gtfs-realtime-bindings-php/blob/master/src/gtfs-realtime.php