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 only in PHP (is this possible?).

I looked at the Google python tutorial https://developers.google.com/protocol-buffers/docs/pythontutorial and the coding documentation https://developers.google.com/protocol-buffers/docs/encoding and https: // github .com / maxious / ACTBus-ui / tree / master / lib / Protobuf-PHP , but it's very difficult for me to conceptualize what is happening. I think I understand that gtfs-realtime.php is a compiled set of encoding instructions defined in gtfs-realtime.proto (please correct me if I'm wrong), but I don’t know how to make it decode VehiclePosition. pb. Also, what are the dependencies of gtfs-realtime.php (or the python equivalent for that matter)? Is there anything else I need to compile or something that is not a simple PHP script if all I want to do is read VehiclePosition.pb?

Thanks.

+6
source share
4 answers

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

It was released recently. I use it for several days and it works like a charm.

+1
source

edmonscommerce and Julian are on the right track.

However, I went the same way, and I found that implementing the PHP protocol protocols is cumbersome (especially when submitting the NYTT MTA).


Alternative method (Command line + JSON):

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

Just download (without installation) and run: gtfs_realtime_json <feed_url>

Here is 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); 
+1
source

I would suggest 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(); 

taken from the man page: http://drslump.imtqy.com/Protobuf-PHP/protobuf-php.3.html

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

so something like:

 protoc-gen-php gtfs-realtime.proto 
0
source

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

Edit: The problem is that I do not have permission to execute on my server. Thanks for your executable.

0
source

Source: https://habr.com/ru/post/925444/


All Articles