GTFS Realtime php

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

I know Google has instructions for using realtime gtfs with php ( ), but I can't figure out how to get it to work.

What I want to do (using whatever information I need: trip_id, stop_id, etc.), return the delayed value trip_update 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?


source to share

2 answers

A more complete PHP GTFS-realtime example might look like this:

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 fields in the GTFS-realtime base schema:

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



It seems to me that you don't need to access the Protobuf files directly as you already have the data in your database. So someone else will take care of reading the information.

GTFS real-time data structure Protobuff Protocol and you need to know exactly how it works. Here is a diagram of a typical Protobuf reading process. These instructions don't work for PHP, but it gives you an overview of the process.

  • First you need to install the Protobuf compiler. If you are using Mac OS X read this .
  • Use the command protoc

    to create a protocol buffer API for your GTFS-realtime data. The command line only supports C ++, Java and Python.
  • You can use this API to read your data.

As far as I understand the repo you linked is for GTFS-realtime data only, so the protocol buffer API has already been created. The only thing you need to do is use the code snippet that is presented on the page:

require_once 'vendor/autoload.php';

use transit_realtime\FeedMessage;

$data = file_get_contents("URL OF YOUR GTFS-REALTIME SOURCE GOES HERE");
$feed = new FeedMessage();
foreach ($feed->getEntityList() as $entity) {
  if ($entity->hasTripUpdate()) {
    error_log("trip: " . $entity->getId());



GTFS-realtime is a special kind of data in the Protobuf protocol that processes transit information. You can create a Protobuf API to read data from Protobuf files. But if you already have them in your database, there is nothing to do.



All Articles