dacoto/gtfs-rt-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dacoto/gtfs-rt-php

最新稳定版本:v1.0.0

Composer 安装命令:

composer require dacoto/gtfs-rt-php

包简介

GTFS-Realtime library for PHP 8.4+ based on Google Protocol Buffers v5

README 文档

README

GTFS-RT PHP

GTFS RT PHP

GTFS-Realtime library for PHP 8.4+ based on Google's Protocol Buffers v5.

A modern PHP implementation of the GTFS Realtime specification. The proto definition was downloaded from gtfs.org and converted to proto3 syntax for compatibility with the latest google/protobuf PHP library.

Requirements

Installation

composer require dacoto/gtfs-rt-php

Usage

Creating a Feed Message

use Google\Transit\Realtime\FeedMessage;
use Google\Transit\Realtime\FeedHeader;
use Google\Transit\Realtime\FeedHeader\Incrementality;
use Google\Transit\Realtime\FeedEntity;
use Google\Transit\Realtime\TripUpdate;
use Google\Transit\Realtime\TripUpdate\StopTimeUpdate;
use Google\Transit\Realtime\TripUpdate\StopTimeEvent;
use Google\Transit\Realtime\TripDescriptor;

$feedMessage = new FeedMessage();

$header = new FeedHeader();
$header->setGtfsRealtimeVersion('2.0');
$header->setIncrementality(Incrementality::FULL_DATASET);
$header->setTimestamp(time());
$feedMessage->setHeader($header);

$entity = new FeedEntity();
$entity->setId('entity-1');

$tripDescriptor = new TripDescriptor();
$tripDescriptor->setTripId('trip-123');
$tripDescriptor->setRouteId('route-42');

$arrivalEvent = new StopTimeEvent();
$arrivalEvent->setDelay(120); // 2 minutes late

$stopTimeUpdate = new StopTimeUpdate();
$stopTimeUpdate->setStopSequence(1);
$stopTimeUpdate->setStopId('stop-A');
$stopTimeUpdate->setArrival($arrivalEvent);

$tripUpdate = new TripUpdate();
$tripUpdate->setTrip($tripDescriptor);
$tripUpdate->setStopTimeUpdate([$stopTimeUpdate]);

$entity->setTripUpdate($tripUpdate);
$feedMessage->setEntity([$entity]);

// Serialize to binary protobuf
$binaryData = $feedMessage->serializeToString();

Parsing a Feed Message

use Google\Transit\Realtime\FeedMessage;

$binaryData = file_get_contents('https://your-agency.com/gtfs-rt/tripupdates.pb');

$feedMessage = new FeedMessage();
$feedMessage->mergeFromString($binaryData);

echo "GTFS-RT version: " . $feedMessage->getHeader()->getGtfsRealtimeVersion() . PHP_EOL;

foreach ($feedMessage->getEntity() as $entity) {
    if ($entity->hasTripUpdate()) {
        $tripUpdate = $entity->getTripUpdate();
        echo "Trip ID: " . $tripUpdate->getTrip()->getTripId() . PHP_EOL;

        foreach ($tripUpdate->getStopTimeUpdate() as $stu) {
            if ($stu->hasArrival()) {
                echo "  Stop " . $stu->getStopId() . " delay: " . $stu->getArrival()->getDelay() . "s" . PHP_EOL;
            }
        }
    }
}

See full examples in the examples/ directory.

Proto3 Conversion Notes

The official GTFS Realtime proto uses proto2 syntax. This library converts it to proto3 to be compatible with the latest google/protobuf PHP library. The key changes are:

  1. Changed syntax = "proto2" to syntax = "proto3"
  2. Removed all optional and required field labels (all fields are optional in proto3)
  3. Removed all extensions ranges (not supported in proto3)
  4. Removed all explicit [default = ...] values
  5. Added UNKNOWN_CAUSE = 0, NO_SERVICE = 0, and UNKNOWN_SEVERITY = 0 as first enum values (proto3 requires first value to be 0)
  6. Added PHP namespace options: php_namespace and php_metadata_namespace

Running Tests

composer install
vendor/bin/phpunit

Regenerating PHP Classes

The PHP classes in src/ are generated automatically from gtfs-realtime.proto using protoc.

Automatic (GitHub Actions)

Every time composer.json or gtfs-realtime.proto is changed on main, the Regenerate PHP Classes workflow runs automatically, downloads the correct protoc binary, regenerates all PHP classes, and commits the result.

You can also trigger it manually from the Actions tab, optionally overriding the protoc version.

Manual (local)

Run the generation script — it resolves the protoc version from composer.json (or composer.lock) automatically:

bash bin/generate.sh

Or override the protoc version explicitly:

bash bin/generate.sh --protoc-version 34.1

The script:

  1. Reads the google/protobuf version from composer.lock (or composer.json)
  2. Derives the matching protoc release (e.g. PHP v5.34.1protoc v34.1)
  3. Downloads the protoc binary from github.com/protocolbuffers/protobuf
  4. Regenerates all PHP classes in src/ from gtfs-realtime.proto

License

MIT — See LICENSE

统计信息

  • 总下载量: 26
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固