root/trunk/bustracker.departures.getNext.php

Revision 3, 1.3 kB (checked in by olly, 6 months ago)

Renaming files to .php extension.

Line 
1 <?
2 header("Content-type: text/xml");
3
4 $bus_stop_code = $_GET['bus_stop_code'];
5
6 if (!$bus_stop_code) die("Missing parameter: bus_stop_code");
7
8 $junk = getData($baseurl."display.php?clientType=b&&numberOfPassage=100&busStopCode=".$bus_stop_code);
9 $xml = simplexml_load_string($junk);
10 $xpath = $xml->xpath("//*[@id=\"displayDepartures\"]");
11
12 $returnxml = new SimpleXmlElement("<bustracker></bustracker>");
13 foreach (($xpath[0]->pre) as $pre) {
14     // behold my mad reg-ex skillz
15     preg_match("/(N*\d*) *([A-Z ]*)\**(\d*(:\d\d)*)/",$pre,$matches);
16     $departure_node = $returnxml->addChild("departure");
17     $departure_node->addChild("service",trim($matches[1]));
18     $departure_node->addChild("destination",trim($matches[2]));
19     if (strpos($matches[3],":")) {
20         // if hours is less < current hour then add tomorrow's day before we strtotime it
21         if (substr($matches[3],0,2) < date("h") ) {
22             $tomorrow  = mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"));
23             $timediff = strtotime(date("m/d/y",$tomorrow)." ".$matches[3]) - time();
24         }
25         else {
26             $timediff = strtotime($matches[3]) - time();
27         }
28         $minstogo = round($timediff/60,0);
29         $timechild = $departure_node->addChild("mins",$minstogo);
30         $timechild->addAttribute("estimated","true");
31     }
32     else {
33         $departure_node->addChild("mins",trim($matches[3]));
34     }
35 }
36 echo $returnxml->asXML();
37 ?>
38
Note: See TracBrowser for help on using the browser.