-
Notifications
You must be signed in to change notification settings - Fork 1
/
muni.php
137 lines (121 loc) · 3.33 KB
/
muni.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?
/*
* PHP NextMuni
*
* Author:
* Dan Masquelier - http://twitter.com/danmasq
*
* API Docs:
* http://www.nextbus.com/xmlFeedDocs/NextBusXMLFeed.pdf
* (This class uses the undocumented JSON feed)
*/
class NextMuni {
const API_BASE = "http://webservices.nextbus.com/service/publicJSONFeed";
private $options = array();
static $remote = array(
'timeout' => 5,
);
function __construct(array $options=array()) {
$this->options = array_merge(array(
'agency' => null, # e.g. 'sf-muni'. Use agency_list() to find.
'route' => null, # routeTag e.g. '38L'. Use route_list() to find.
'routes' => array(), # e.g. array('N', '38L'). For messages()
'stop_id' => null, # e.g. 1111. Use route_config()->stop to find.
'stops' => array(), # '$this->route|stopId' e.g. array('{$this->route}|3840', "{$this->route}|4001"). For predictions_multiple
), $options);
}
function __get($name) {
return $this->options[$name];
}
function __set($name, $value) {
$this->options[$name] = $value;
}
function agency_list() {
$api = array(
'command' => 'agencyList',
);
return $this->get($api);
}
function route_list() {
$api = array(
'command' => 'routeList',
'a' => $this->agency,
);
return $this->get($api);
}
function route_config() {
$api = array(
'command' => 'routeConfig',
'a' => $this->agency,
'r' => $this->route,
);
return $this->get($api);
}
function predictions() {
$api = array(
'command' => 'predictions',
'a' => $this->agency,
's' => $this->stop_id,
'r' => $this->route,
);
return $this->get($api);
}
function predictions_multiple() {
$stops = $this->build_repeating_params($this->stops, 'stops');
$api = array(
'command' => 'predictionsForMultiStops',
'a' => $this->agency,
't' => time(),
'stops' => $stops,
);
return $this->get($api);
}
function schedule() {
$api = array(
'a' => $this->agency,
'r' => $this->route,
);
return $this->get($api);
}
function messages() {
$routes = $this->build_repeating_params($this->routes, 'r');
$api = array(
'a' => $this->agency,
'r' => $routes,
);
return $this->get($api);
}
function vehicle_locations() {
$api = array(
'command' => 'vehicleLocations',
'a' => $this->agency,
'r' => $this->route,
't' => time(),
);
return $this->get($api);
}
private function build_repeating_params($items, $key) {
# hack to match NextMuni api index-less "stops" keys
$out = '';
$len = count($items) - 1;
foreach ($items as $k => $item) {
$last = $len == $k ? '' : "&{$key}=";
$out .= "{$item}{$last}";
}
return $out;
}
private function get($api) {
$api = urldecode(http_build_query($api, '', '&'));
$full_url = self::API_BASE .'?'. $api;
$curl = curl_init();
$curl_o = array(
CURLOPT_URL => $full_url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => self::$remote['timeout'],
);
curl_setopt_array($curl, $curl_o);
$response = curl_exec($curl);
curl_close($curl);
return json_decode($response);
}
}