Skip to content

Instantly share code, notes, and snippets.

@GrexAut
Last active September 28, 2016 21:43
Show Gist options
  • Select an option

  • Save GrexAut/7569810 to your computer and use it in GitHub Desktop.

Select an option

Save GrexAut/7569810 to your computer and use it in GitHub Desktop.
Get departure times for each line from a station
<?php
/*
* Author: Gregor Ganglberger
* E-Mail: gg@grexaut.net
* License: Creative Commons
* Notice: This class uses the following real-time API from Open Data: CC-BY-3.0: Stadt Linz - data.linz.gv.at
*
* Links:
* http://creativecommons.org/licenses/by/3.0/at/
* http://www.linz-abfahrten.at
* http://data.linz.gv.at/daten/LinzAGLinien/index.html?ckan_name=elektronische-fahrplanauskunft-der-linz-ag-linien--echtzeit
* http://data.linz.gv.at/nutzungsbedingungen/
*
*
*
* File: linzag.class.php v1.0
* Description: Get departure times for each line from a station
*
* Functions:
* getStationInfos($searchstring) => return one or more stations with following informations: ID, Name, PLZ (4020,4030,4040,..)
* $stationname => searchstring like = "Hauptplatz", results are: "Hautplatz" and "Hauptplatz Obere Donaulände"
* getDepTime($station_id) => return departure times each line in minutes (limited to 10),
*
*/
class Departure
{
protected $dm = "http://www.linzag.at/static/XML_DM_REQUEST?sessionID=[SESSION]&locationServerActive=1&type_dm=any&name_dm=[ORIGIN]&limit=10";
protected $dm_time = "http://www.linzag.at/static/XML_DM_REQUEST?sessionID=[SESSION]&requestID=1&dmLineSelectionAll=1";
public function getStationInfos($searchstring)
{
$pre_xml = str_replace("[ORIGIN]",$searchstring,$this->dm);
$pre_xml = str_replace("[SESSION]",0,$pre_xml);
$xml = new SimpleXMLElement(file_get_contents($pre_xml));
$station = array();
$i=0;
foreach ( $xml->itdDepartureMonitorRequest->itdOdv->itdOdvName->odvNameElem as $elem_station )
{
if($elem_station['anyType'] == "stop" || $xml->itdDepartureMonitorRequest->itdOdv->itdOdvName['state'] == "identified")
{
$station[$i]['ID'] = $elem_station['id'];
$station[$i]['Name'] = $elem_station['objectName'];
$station[$i]['PLZ'] = $elem_station['postCode'];
$i++;
}
}
return $station;
}
public function setSession($origin_id)
{
$pre_xml = str_replace("[ORIGIN]",$origin_id,$this->dm);
$pre_xml = str_replace("[SESSION]",0,$pre_xml);
$xml = new SimpleXMLElement(file_get_contents($pre_xml));
$this->session = $xml['sessionID'];
}
public function getDepTime($origin_id)
{
$this->setSession($origin_id);
$pre_xml = str_replace("[SESSION]",$this->session,$this->dm_time);
$xml = new SimpleXMLElement(file_get_contents($pre_xml));
$dep = array();
$i = 0;
foreach($xml->itdDepartureMonitorRequest->itdDepartureList->itdDeparture as $origin)
{
$dep[$i]['line'] = (string) $origin->itdServingLine['number'];
$dep[$i]['direction'] = (string) $origin->itdServingLine['direction'];
$dep[$i]['day'] = (string) $origin->itdDateTime->itdDate['day'];
$dep[$i]['month'] = (string) $origin->itdDateTime->itdDate['month'];
$dep[$i]['year'] = (string) $origin->itdDateTime->itdDate['year'];
$dep[$i]['hour'] = (string) str_pad($origin->itdDateTime->itdTime['hour'],2,0,STR_PAD_LEFT);
$dep[$i]['minute'] = (string) str_pad($origin->itdDateTime->itdTime['minute'],2,0,STR_PAD_LEFT);
$dep_time = new DateTime($dep[$i]['year']."-".$dep[$i]['month']."-".$dep[$i]['day']." ".$dep[$i]['hour'].":".$dep[$i]['minute']);
$now = new DateTime("now");
$diff = $now->diff($dep_time);
$minutes = $diff->format("%h")*60;
$minutes += $diff->format("%i");
$dep[$i]['depMinutes'] = $minutes;
$i++;
}
return $dep;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment