Created
October 5, 2014 12:26
TripAPI.java
This file contains hidden or 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 | |
require_once('BartebussAPI.class.php'); | |
class TripAPI extends BartebussAPI { | |
public function __construct(){ | |
parent::__construct(); | |
} | |
public function getTrip($tripID, $tripDate=''){ | |
if(!is_numeric($tripID)) | |
return false; | |
// Parse out data | |
$url = 'http://rp.atb.no/scripts/TravelMagic/TravelMagicWE.dll/turinfo?trip='.$tripID.'&date='.$tripDate; | |
$doc = new DOMDocument(); | |
$doc->loadHTML(utf8_decode($this->curl($url))); | |
$xpath = new DOMXPath($doc); | |
$h1text = trim($xpath->query('//h1/text()[last()]')->item(0)->nodeValue); | |
$matches = array(); | |
preg_match("/(\d+) mot (.+?),/i", $h1text, $matches); | |
$line = trim($matches[1]); | |
$destination = trim($matches[2]); | |
// Prepare object | |
$trip = array('tripID' => $tripID, 'date' => $tripDate, 'line' => $line, 'destination' => $destination, 'stops' => array()); | |
$rows = $xpath->query("//div[@id='tm-triplist']//h3"); | |
for($i=0 ; $i < $rows->length ; $i++){ | |
$time = $xpath->query('./span/text()', $rows->item($i))->item(0)->nodeValue; | |
$destination = trim(str_replace(" (Trondheim)", "", $xpath->query('.//a/text()', $rows->item($i))->item(0)->nodeValue)); | |
$matches = array(); | |
$href = $xpath->query('.//a/@href', $rows->item($i))->item(0)->nodeValue; | |
preg_match("/from=(\d+)/i", $href, $matches); | |
$busstopID = $matches[1]; | |
$trip['stops'][] = array('time' => $time, 'destination' => $destination, 'busstopID' => $busstopID); | |
} | |
// Return trip data | |
return json_encode($trip); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment