-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenstate.php
385 lines (362 loc) · 11.2 KB
/
Openstate.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
/**
* Open State PHP Class to interact with the Open State Project
* Author: Nikki Snow
* Version: 0.01
* http://nikkisnow.com/code/php-openstate
* License: http://opensource.org/licenses/gpl-3.0.html
*
* The Open State Project provides data on state legislative activities,
* including bill summaries, votes, sponsorships and state legislator
* information.
*
*/
class Openstate
{
// An API key can be obtained at http://services.sunlightlabs.com/
public $api_key = '';
public $api_url = 'http://openstates.org/api/v1';
public $decode;
public $google_url = 'http://maps.googleapis.com/maps/api/geocode/json';
public function __construct($api_key = null)
{
if(!empty($api_key))
{
$this->api_key = $api_key;
}
$this->curl = new Curl();
}
/**
* State Information Metadata
*
* @param string $state optional The 2-letter abbreviation of the state
*/
public function getStateInfo($state=NULL)
{
$url = $this->api_url.'/metadata/';
if ($state) {
$url .= $state.'/';
}
$params['apikey'] = $this->api_key;
$query = $this->curl->get($url, $params);
return $this->parseQuery($query);
}
/**
* Search Bills by keyword
*
* @param string $search_term The keyword string to lookup
* @param string $state The 2-letter abbreviation of the state
* @param string $session The session this bill was introduced in.
*/
public function searchBills($search_term, $state, $session)
{
if ($search_term && $state && $session) {
$url = $this->api_url.'/bills/';
$params = array(
'q' => $search_term,
'state' => $state,
'session' => $session
);
return $this->billSearch($params);
}
return FALSE;
}
/**
* Bill lookup
*
* @param string $bill_id The identifier given to this bill by the state legislature (e.g. 'AB6667')
* @param string $state The 2-letter abbreviation of the state
* @param string $session Session this bill was introduced in.
* @param string $chamber optional Chamber ('upper' or 'lower')
*/
public function billLookup($bill_id, $state, $session, $chamber=NULL)
{
if ($bill_id) {
$bill_id = rawurlencode($bill_id);
$url = $this->api_url.'/bills/'.$state.'/'.$session.'/';
if ($chamber) {
$url .= $chamber.'/';
}
$url .= $bill_id.'/';
$params['apikey'] = $this->api_key;
$query = $this->curl->get($url, $params);
return $this->parseQuery($query);
}
return FALSE;
}
/**
* Bill search
*
* @param mixed $params Bill search parameters. Expecting associative array with the following possible keys:
* q - the keyword string to lookup (If set, state and session are required)
* state - filter results by given state (two-letter abbreviation)
* search_window - a string representing what time period to search across. Pass 'session' to search bills from the state's current or most recent legislative session, 'term' to search the current or most recent term, 'all' to search as far back as Open States has data for, or supply 'session:SESSION_NAME' or 'term:TERM_NAME' (e.g. 'session:2009' or 'term:2009-2010') to search a specific session or term.
* chamber - filter results by given chamber ('upper' or 'lower')
* bill_id__in - return all bills with ids in a set of bill ids that are pipe | separated (ex. HB 1|HB 2|SB 10)
* updated_since - only return bills that have been updated since a given date, YYYY-MM-DD format
* subject - filter by bills that are about a given subject. If multiple subject parameters are supplied then only bills that match all of them will be returned. See list of subjects at http://openstates.org/categorization/#subjects
* sponsor_id - only return bills sponsored by the legislator with the given id (corresponds to leg_id)
*/
public function billSearch($params)
{
if ($params) {
if(array_key_exists('q', $params) &&
(!array_key_exists('state', $params) && !array_key_exists('session', $params)))
{
return FALSE;
}
if(array_key_exists('bill_id', $params))
{
$params['bill_id'] = rawurlencode($params['bill_id']);
}
$url = $this->api_url.'/bills/';
$params['apikey'] = $this->api_key;
var_dump($params);
$query = $this->curl->get($url, $params);
return $this->parseQuery($query);
}
return FALSE;
}
/**
* Committee Lookup
*
* @param string $params Committee search parameters. Expecting associative array with the following possible keys:
* committee - name of a committee
* subcommittee - name of a subcommittee
* chamber - filter results by given chamber (upper, lower or joint)
* state - return committees for a given state (eg. ny)
*/
public function committeeSearch($params)
{
if ($params) {
$url = $this->api_url.'/committees/';
$params['apikey'] = $this->api_key;
$query = $this->curl->get($url, $params);
return $this->parseQuery($query);
}
return FALSE;
}
/**
* Committee lookup
*
* @param string $committee_id Committee's Open States ID.
*
*/
public function committeeLookup($committee_id)
{
if ($committee_id) {
$url = $this->api_url.'/committees/'.$committee_id.'/';
$params['apikey'] = $this->api_key;
$query = $this->curl->get($url, $params);
return $this->parseQuery($query);
}
return FALSE;
}
/**
* Event search
*
* @param mixed $params Event search parameters. Expecting associative array with the following possible keys:
* state - Filter by state (two-letter abbreviation)
* type - Filter by event type (e.g. 'committee:meeting'). Accepts multiple types separated by commas.
* format - Output format. Possible values: json, rss, ics
* See http://openstates.org/api/events/ for more information
*/
public function eventSearch($params)
{
if ($params) {
$url = $this->api_url.'/events/';
$params['apikey'] = $this->api_key;
var_dump($params);
$query = $this->curl->get($url, $params);
return $this->parseQuery($query);
}
return FALSE;
}
/**
* Event Lookup
*
* @param string $event_id An Open States event id, e.g. TXE00004925
*/
public function eventLookup($event_id)
{
if ($event_id) {
$url = $this->api_url.'/events/'.$event_id.'/';
$params['apikey'] = $this->api_key;
$query = $this->curl->get($url, $params);
return $this->parseQuery($query);
}
return FALSE;
}
/**
* Search events by state or type
*
* @param string $state optional The 2-letter abbreviation of the state
* @param string $type optional The type of event, e.g. 'committee:meeting', 'bill:action'
*/
public function searchEvents($state=NULL, $type=NULL)
{
$url = $this->api_url.'/events/';
if ($state) {
$params['state'] = $state;
}
if ($type) {
$params['type'] = $type;
}
$params['apikey'] = $this->api_key;
$query = $this->curl->get($url, $params);
return $this->parseQuery($query);
}
/**
* District lookup
*
* @param string $state The 2-letter abbreviation of the state
* @param string $chamber optional Chamber, i.e. 'upper' or 'lower'. (optional)
*/
public function districtLookup($state, $chamber=NULL)
{
if ($state) {
$url = $this->api_url.'/districts/'.$state.'/';
if ($chamber) {
$url .= $chamber.'/';
}
$params['apikey'] = $this->api_key;
$query = $this->curl->get($url, $params);
return $this->parseQuery($query);
}
return FALSE;
}
/**
* District Boundary Lookup
*
* @param string $boundary_id A unique ID used in District Boundary Lookup to get geographical details about the district
* See http://openstates.org/api/districts/#district-boundary-lookup for Distruct Boundary Lookup details.
*/
public function districtBoundaryLookup($boundary_id)
{
if ($boundary_id) {
$url = $this->api_url.'/districts/boundary/'.$boundary_id.'/';
$params['apikey'] = $this->api_key;
$query = $this->curl->get($url, $params);
return $this->parseQuery($query);
}
return FALSE;
}
/**
* Parse query from json to an array
*/
private function parseQuery($query, $decode=NULL)
{
$decode = isset($decode) ? $decode : $this->decode;
switch ($query) {
case 'Not Found':
$query = FALSE;
break;
default:
if ($decode) {
$query = json_decode($query);
}
break;
}
return !empty($query) ? $query : FALSE;
}
/**
* Legislator lookup
*
* @param string $leg_id A permanent, unique identifier for this legislator within the Open States system.
*/
public function legislatorLookup($leg_id)
{
if ($leg_id) {
$url = $this->api_url.'/legislators/'.$leg_id.'/';
$params['apikey'] = $this->api_key;
$query = $this->curl->get($url, $params);
return $this->parseQuery($query);
}
return FALSE;
}
/**
* Legislator Search
*
* @param mixed $params Legislator search parameters. Expecting associative array with the following possible keys:
* state - Filter by state served in (two-letter state abbreviation)
* first_name - Filter by name
* last_name - Filter by name
* chamber - Filter by legislator's chamber, i.e. 'upper' or 'lower'.
* active - Restrict the search to currently-active legislators (the default) - 'true' or 'false'.
* term - Filter by legislators who served during a certain term.
* district - Filter by legislative district.
* party - Filter by the legislator's party, e.g. 'Democratic' or 'Republican'.
* active - Boolean
*/
public function legislatorSearch($params)
{
if ($params) {
$url = $this->api_url.'/legislators/';
$params['apikey'] = $this->api_key;
var_dump($params);
$query = $this->curl->get($url, $params);
return $this->parseQuery($query);
}
return FALSE;
}
/**
* Use Google Geocoding API to get the lat and long for an address.
*
* @param string $address Address to locate
* @return array $location Associative array containing 'lat' and 'long'.
*/
private function getLatLong($address)
{
if($address)
{
$gurl = $this->google_url;
$params['address'] = urlencode($address);
$params['sensor'] = 'false';
if($query = $this->curl->get($gurl, $params))
{
$rslt = json_decode($query);
if($rslt->status == 'OK')
{
$out = array('lat' => $rslt->results[0]->geometry->location->lat, 'long' => $rslt->results[0]->geometry->location->lng);
return $out;
}
// @todo add stuff here to better handle the other possible responses.
}
}
return FALSE;
}
/**
* Use getLatLong to get the lat and long for an address, then legislatorGeoLookup to get the legislators that represent the address.
*
* @param string $address Street address to find legislators for
*/
public function legislatorLookupByAddress($address)
{
if($address)
{
if($loc = $this->getLatLong($address))
{
return $this->legislatorGeoLookup($loc['lat'], $loc['long']);
}
}
return FALSE;
}
/**
* Legislator Geo lookup
*
* @param string $lat Latitude of point to use for district lookup
* @param string $long Longitude of point to use for district lookup
*/
public function legislatorGeoLookup($lat, $long)
{
if ($lat && $long) {
$url = $this->api_url.'/legislators/geo/';
$params['apikey'] = $this->api_key;
$params['lat'] = $lat;
$params['long'] = $long;
$query = $this->curl->get($url, $params);
return $this->parseQuery($query);
}
return FALSE;
}
}