Web Developer, Bristol, England, UK
Transport Central
To setup Transport Central for yourself, you need to create a HTML file (use this as a starting point) with the stop IDs for the stops you use. You can get the stop IDs from NextBusBristol. Get the stop ID which I've helpfully highlighted in red.
Create a h2, div and table for each stop. Replace [STOPID] with the stop ID if that isn't obvious.
STOP NAME ←
You also need to use this PHP script that proxies the request to NextBusBristol and turns HTML into JSON for easy parsing by your phone browser.
class Transport
{
private $ch;
static $usecookie = '../cookie.txt';
public function __construct()
{
$this->ch = curl_init();
}
public function fetchURL($url)
{
if($this->ch) {
curl_setopt($this->ch, CURLOPT_URL, $url);
curl_setopt($this->ch, CURLOPT_HEADER, 0);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6');
curl_setopt($this->ch, CURLOPT_COOKIEJAR, Transport::$usecookie);
curl_setopt($this->ch, CURLOPT_COOKIEFILE, Transport::$usecookie);
$data = curl_exec($this->ch);
$info = curl_getinfo($this->ch);
curl_close($this->ch);
if (array_key_exists('http_code', $info) and $info['http_code'] == 200) {
return $data;
}
}
}
public function fetchStop($stop)
{
$data = $this->fetchURL('http://bristol.acislive.com/pip/stop_simulator.asp?naptan=' . $stop);
$data = $this->fetchURL('http://bristol.acislive.com/pip/stop_simulator_table.asp?NaPTAN=' . $stop . '&bMap=1&offset=0&refresh=20&pscode=&dest=&duegate=0&PAC=' . $stop);
$matches = array();
preg_match_all("/<td width=\"25%\" nowrap>(\d+?) <\/td>td width=\"35%\" class=\"destination\" nowrap>([\w ]+?) <\/td><td width=\"20%\" align=\"right\" nowrap>(\d+?) min/", $data, $matches);
$data4json = array();
$c = count($matches[0]);
for ($i = 0; $i < $c; $i++) {
$data4json[] = array( 'number' => $matches[1][$i], 'destination' => $matches[2][$i], 'waittime' => $matches[3][$i] );
}
return json_encode($data4json);
}
}
header('Content-type: application/json');
$transport = new Transport();
if (array_key_exists('stop', $_GET)) {
echo $transport->fetchStop($_GET['stop']);
}
It's not the most advanced thing in the world but handy when you are waiting at a bus stop.
View the Transport Central.
blog comments powered by Disqus