Created
September 10, 2010 16:21
-
-
Save HansF/573920 to your computer and use it in GitHub Desktop.
php wrapper for iRail api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// | |
// php wrapper for iRail, by Hans Fraiponts | |
// licence http://www.gnu.org/licenses/gpl-3.0.html | |
// More info here : http://project.irail.be/cgi-bin/trac.fcgi/wiki/ApiSpecification | |
// | |
function listStations (){ | |
$xml = simplexml_load_file("http://api.irail.be/stations.php"); | |
foreach ($xml->station as $station) { | |
$stationName = (string)$station[0] ; | |
$stationLocation = (string)$station->attributes()->location; | |
$stations[$stationName] = $stationLocation; | |
} | |
return $stations; | |
} | |
//print_r(listStations ()); | |
function trainSchedule ($station1,$station2,$date="ns", $time="ns", $lang="ns",$timesel="ns",$trainsonly="ns",$results="ns"){ | |
$url = "http://api.irail.be/trains.php?" . "to=$station1" . "&from=$station2"; | |
if ($date!="ns") $url = $url . "&date=$date"; | |
if ($time!="ns") $url = $url . "&time=$time"; | |
if ($lang!="ns")$url = $url . "&lang=$lang"; | |
if ($timesel!="ns") $url = $url . "×el=$timesel"; | |
if ($trainsonly!="ns") $url = $url . "&trainsonly=$trainsonly"; | |
if ($results!="ns") $url = $url . "&results=$results"; | |
$xml = simplexml_load_file($url); | |
foreach ($xml->connection as $connection) { | |
$departTime = $connection->departure->time; | |
$departDate = $connection->departure->date; | |
$depart = mktime (substr($departTime,0,2),substr($departTime,3,2), 0, substr($departDate,2,2),substr($departDate,0,2),substr($departDate,4,2)); | |
$arrivalTime = $connection->arrival->time; | |
$arrivalDate = $connection->arrival->date; | |
$arrival = mktime (substr($arrivalTime,0,2),substr($arrivalTime,3,2), 0, substr($arrivalDate,2,2),substr($arrivalDate,0,2),substr($arrivalDate,4,2)); | |
$delayed = (string)$connection->delay[0]; | |
$duration = (string)$connection->duration; | |
$resultid = (integer)$connection->attributes()->id; | |
foreach ($connection->trains->train as $train ){ | |
$result[$resultid]['trains'][]=(string)$train; | |
} | |
$result[$resultid]['depart'] = $depart; | |
$result[$resultid]['arrival'] = $arrival; | |
$result[$resultid]['duration'] = $duration; | |
$result[$resultid]['delayed'] = $delayed; | |
} | |
return $result; | |
} | |
//print_r(trainSchedule("CINEY","HASSELT")); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Outdated.