SkyNT
SkyNT

Reputation: 803

How to read GTFS protocol buffer in PHP?

I have a GTFS protocol buffer message (VehiclePosition.pb), and the corresponding protocol format (gtfs-realtime.proto), I would like to read the message in PHP alone (is that even possible?).

I looked at Google's python tutorial https://developers.google.com/protocol-buffers/docs/pythontutorial and encoding documentation https://developers.google.com/protocol-buffers/docs/encoding and https://github.com/maxious/ACTBus-ui/tree/master/lib/Protobuf-PHP, but I am having a really hard time conceptualizing what is going on. I think I understand that gtfs-realtime.php is a compiled instruction set of the encoding defined in gtfs-realtime.proto (please correct me if I am wrong), but I have no clue how to get it to decode VehiclePosition.pb. Also, what are the dependencies of gtfs-realtime.php (or the python equivalent for that matter)? Is there anything else I have to compile myself or anything that is not a simple php script if all I want to do is read VehiclePosition.pb?

Thanks.

Upvotes: 8

Views: 2473

Answers (4)

iniro
iniro

Reputation: 31

Sorry Harry Truong, I tried your executable but it returns always NULL. What I am doing wrong?

Edit: The problem is that I have no permission to execute in my server. Thanks for your executable.

Upvotes: 0

Harry Truong
Harry Truong

Reputation: 51

edmonscommerce and Julian are on the right track.

However, I've gone down the same path and I've found that the PHP implementation of Protocol Buffers is cumbersome (especially in the case of NYCT's MTA feed).


Alternative Method (Command Line + JSON):

If you're comfortable with command line tools and JSON, I wrote a standalone tool that converts GTFS-realtime into simple JSON: https://github.com/harrytruong/gtfs_realtime_json

Just download (no install), and run: gtfs_realtime_json <feed_url>

Here's a sample JSON output.

To use this in PHP, just put gtfs_realtime_json in the same directory as your scripts, and run the following:

<?php 

$json = exec('./gtfs_realtime_json "http://developer.mbta.com/lib/GTRTFS/Alerts/VehiclePositions.pb"');
$feed = json_decode($json, TRUE);

var_dump($feed);

Upvotes: 2

Julian
Julian

Reputation: 9140

You can use the official tool: https://developers.google.com/transit/gtfs-realtime/code-samples#php

It was released very recently. I've been using it for a few days and works like a charm.

Upvotes: 1

edmondscommerce
edmondscommerce

Reputation: 2011

I would assume something along the lines of this snippet:

<?php
require_once 'DrSlump\Protobuf.php';

use DrSlump\Protobuf;

$data = file_get_contents('data.pb');
$person = new Tutorial\Person($data);
echo $person->getName();

as taken from the man page: http://drslump.github.io/Protobuf-PHP/protobuf-php.3.html

Before that step, I think you need to generate your PHP classes using the CLI tool as described here: http://drslump.github.io/Protobuf-PHP/protoc-gen-php.1.html

so something along the lines of:

protoc-gen-php gtfs-realtime.proto

Upvotes: 0

Related Questions