該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 我們接受拉的請求,但請首先創建一個問題,以討論添加或修復。如果您想查看添加的新功能,也可以通過創建問題來創建功能請求。
如果您遇到問題,請隨時通過問題跟踪器尋求幫助。