该SDK使开发人员更容易在其PHP项目中使用GTFS数据。目前,仅支持静态文件。
您可以通过作曲家安装此软件包
composer require trafiklab/gtfs-php-sdk
打开GTFS文件
您可以加载本地GTFS ZIP文件,也可以通过HTTP下载
$ gtfsArchive = GtfsArchive:: createFromPath ( " gtfs.zip " );
$ gtfsArchive = GtfsArchive:: createFromUrl ( " http://example.com/gtfs.zip " );可选地,您只有在上次检索以来就可以选择下载GTFS ZIP文件。在尝试自动化需要存储在数据库中的GTFS检索时,这很有用,而无需每次不断重写相同的数据。使用以下HTTP标头:
大多数代理商都提供了最后修饰的标头,但是如果不存在,ETAG是最好的路线。如果由于某种原因也没有提供ETAG,则它将正常继续,好像您要使用原始的GTFSarchive :: CreateFromurl()方法。
$ gtfsArchive = GtfsArchive:: createFromUrlIfModified (
" http://example.com/gtfs.zip " ,
" Wed, 10 Jun 2020 15:56:14 GMT " ,
" 99fa-5a7bce236c526 "
);如果您没有ETAG或最后修饰的值,只需将它们排除在外,该方法将为您检索它们。
if ( $ gtfsArchive = GtfsArchive:: createFromUrlIfModified ( " http://example.com/gtfs.zip " ) {
// Get Methods return null if GTFS Url does not contain the specified Header: ETag, Last-Modified.
$ lastModified = $ gtfsArchive -> getLastModified (); // Wed, 10 Jun 2020 15:56:14 GMT | null
$ eTag = $ gtfsArchive -> getETag (); // "99fa-5a7bce236c526" | null
// You can get the Last-Modified datetime PHP Object (Useful for storing in databases) by doing the following:
$ datetime = $ gtfsArchive ->getLastModifiedDateTime() // DateTime Object | null
// Or Convert it back to a String using the standard Last-Modified HTTP header format.
if ( $ datetime ) {
$ lastModified = GtfsArchive:: getLastModifiedFromDateTime ( $ datetime ); // Wed, 10 Jun 2020 15:56:14 GMT
}
}
将文件提取到临时目录(/tmp/gtfs/),并在破坏GTFSarchive对象时进行清理。您可以调用$gtfsArchive->deleteUncompressedFiles()以手动删除未压缩的文件。
读取文件
$ agencyTxt = $ gtfsArchive -> getAgencyFile (); // read agency.txt
$ calendarTxt = $ gtfsArchive -> getCalendarFile (); // read calendar.txt
$ routesTxt = $ gtfsArchive -> getRoutesFile (); // read routes.txt
$ stopTimesTxt = $ gtfsArchive -> getStopTimesFile (); // read stop_times.txt
. . .所有文件均已懒惰和缓存。这意味着仅在调用诸如getStopTimesFile()之类的方法之后加载数据。请记住,第一次阅读数据可能需要一段时间。读取一个大stop_times.txt文件可能需要长达一分钟的时间。
读取文件数据
每个文件类都包含一种读取该文件中所有数据的方法。一些类包含用于常用查询的其他助手方法,例如通过ID或外键进行过滤。
每个(受支持)文件都有一个PHP类,而该文件中包含的数据有另一个类。每个字段的定义都包含在PHPDOC中的每个GETTER函数,使您可以专注于编码,而更少的是规格和代码之间的Alt tabbing。
$ stopTimesTxt = $ gtfsArchive -> getStopTimesFile (); // The file is represented by a StopTimesFile object
$ allStopTimes = $ stopTimesTxt -> getStopTimes (); // a method like this is always available
$ stopTimesForStopX = $ stopTimesTxt -> getStopTimesForStop ( $ stopId ); // this is a helper method for foreign keys
$ stopTime = $ allStopTimes [ 0 ]; // Get the first row
$ headsign = $ stopTime -> getStopHeadsign (); // One row of data is represented by a StopTime object 我们接受拉的请求,但请首先创建一个问题,以讨论添加或修复。如果您想查看添加的新功能,也可以通过创建问题来创建功能请求。
如果您遇到问题,请随时通过问题跟踪器寻求帮助。