-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.sunlightlabs.php
568 lines (501 loc) · 19.7 KB
/
class.sunlightlabs.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
<?php
/**
* class SunlightData
* PHP Wrapper class around the Sunlight Labs API
* Docs for the Sunlight API is at http://wiki.sunlightlabs.com/index.php/Main_Page
* @author Aaron Brazell <[email protected]>
* @version 1.0
* @package sunlightlabs-php
*/
class SunlightData {
/**
* Type of data request. Either XML or JSON. JSON is strongly recommended but requires PHP 5.2+
* @var $type
**/
var $type = 'json';
/**
* The Sunlight Labs API URL
* @var $request_url
**/
var $request_url = 'http://services.sunlightlabs.com/api/';
/**
* Sunlight Labs supplied unique API key. Register at http://services.sunlightlabs.com/api/register/
* @var $api_key
**/
var $api_key;
/**
* The User Agent of your appplication. It is considered best practice to identify your application when making requests
* @var $user_agent
**/
var $user_agent = 'Sunlight Labs-PHP Class/Written by Aaron Brazell [[email protected]]';
/**
* Not utilized with Sunlight Labs API at this time. Future restrictions may apply.
* @var $rate_limit
**/
var $rate_limit = 0;
/**
* Utilize this variable if you need to pass other headers in your HTTP request. Must be an array of headers, if used.
* @var $headers
**/
var $headers;
/**
* Debugging and header return if needed
* @var $debug
**/
var $debug = false;
/**
* Internal request method. Sends an HTTP request and returns data to the calling method in the form of an object
* @access private
* @param string $request required. The dynamically formed API request URL
* @return $object
**/
function _request( $request )
{
return $this->_objectify( $this->_process( $request ) );
}
/**
* Internal helper method for piecing together a query string and urlencoding as needed
* @access private
* @param array $array required. An array of key/value combinations to pass in a query string
* @return string
**/
function _glue( $array )
{
$query_string = '';
foreach( $array as $key => $val ) :
if( is_array( $val ) || is_object( $val ) ) :
foreach( $val as $skey => $sval ) :
$query_string .= $key . '=' . rawurlencode( $sval ) . '&';
endforeach;
else :
$query_string .= $key . '=' . rawurlencode( $val ) . '&';
endif;
endforeach;
return '?apikey=' . $this->api_key . '&' . substr( $query_string, 0, strlen( $query_string )-1 );
}
/**
* Internal helper method for returning data in the form of an object
* @access private
* @param string $data required
* @return object
**/
function _objectify( $data )
{
if( $this->type == 'json' )
return json_decode( $data );
else if( $this->type == 'xml' )
{
if( function_exists('simplexml_load_string') ) :
$obj = simplexml_load_string( $data );
endif;
return $obj;
}
else
return false;
}
/**
* An internal method for sending an HTTP request utilizing cURL
* @access private
* @param string $url required. A properly formed and encoded URL string to send as an HTTP request
* @param array $postargs optional. Not currently used with Sunlight Labs API. If provided, the HTTP request is a POST, not GET
* @return mixed. If the HTTP request fails, the return value is false. Otherwise, the HTTP Code is returned. i.e. '200'
**/
function _process($url,$postargs=false)
{
$ch = curl_init($url);
if($postargs !== false)
{
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postargs);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_NOBODY, 0);
if( $this->debug ) :
curl_setopt($ch, CURLOPT_HEADER, true);
else :
curl_setopt($ch, CURLOPT_HEADER, false);
endif;
curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
if( $this->headers )
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
$response = curl_exec($ch);
$this->responseInfo=curl_getinfo($ch);
curl_close($ch);
if( $this->debug ) :
$debug = preg_split("#\n\s*\n|\r\n\s*\r\n#m", $response);
echo'<pre>' . $debug[0] . '</pre>'; exit;
endif;
if( intval( $this->responseInfo['http_code'] ) == 200 )
return $response;
else
return false;
}
}
/**
* class SunlightLegislator
* PHP Wrapper class around the Sunlight Labs API
* Docs for the Sunlight API is at http://wiki.sunlightlabs.com/index.php/Main_Page
* @author Aaron Brazell <[email protected]>
* @version 1.0
* @package sunlightlabs-php
*/
class SunlightLegislator extends SunlightData {
/**
* Wrapper around the legislators.getList method documented at http://wiki.sunlightlabs.com/index.php/Legislators.get(List)
* @param array $args optional. An array of arguments to be passed. Possible argument keys are:
* title, firstname, middlename, lastname, name_suffix, nickname, party, state, district, in_office,
* gender, phone, fax, website, webform, email, congress_office, bioguide_id, votesmart_id, fec_id,
* govtrack_id, crp_id, eventful_id, sunlight_old_id, congresspedia_id, twitter_id, youtube_url.
*
* Array values can be strings, ints, or arrays (multiple values for same key) - ie.
* array('firstname' => array('John', 'Hillary'))
* For more information, visit wiki.sunlightlabs.com
* @return object
**/
function legislatorList( $args = array() )
{
$qs = $this->_glue( $args );
$data = $this->_request( $this->request_url . 'legislators.getList.' . $this->type . $qs );
return (object) $data->response->legislators;
}
/**
* This method behaves identically to the legislatorList() method except it only returns the first result
* @param array $args optional. See legislatorList() for arguments.
* @return object
**/
function legislatorInfo( $args = array() )
{
$data = $this->legislatorList( $args );
foreach( $data->response->legislators as $legislator )
return $legislator;
}
/**
* A search method that looks for legislators by name.
* @param string $name required. The legislators name, i.e. 'John McCain', 'Mikulski', 'A Specter'
* @return object
**/
function legislatorSearch( $name )
{
if( !is_array( $name ) )
$name = array( 'name' => $name );
$qs = $this->_glue( $name );
$data = $this->_request( $this->request_url . 'legislators.search.' . $this->type . $qs );
return (object) $data->response->results;
}
/**
* A search method for finding which legislators have a district that encompasses a portion of or all of a 5 digit zip code
* @param integer $zip required. A five digit zip code. I.e, 21224, 08743
* @return object
**/
function legislatorZipCode( $zip )
{
if( !is_array( $zip) )
$zip = array( 'zip' => (int) $zip );
$qs = $this->_glue( $zip );
$data = $this->_request( $this->request_url . 'legislators.allForZip.' . $this->type . $qs );
return (object) $data->response->legislators;
}
}
/**
* class SunlightDistrict
* PHP Wrapper class around the Sunlight Labs API
* Docs for the Sunlight API is at http://wiki.sunlightlabs.com/index.php/Main_Page
* @author Aaron Brazell <[email protected]>
* @version 1.0
* @package sunlightlabs-php
*/
class SunlightDistrict extends SunlightData {
/**
* A search method for finding which legislative districts exist in a given 5-digit zip code
* @param integer $zip required. A 5-digit zip code to search within. I.e, 21224, 08743
* @return object
**/
function districtsByZipCode( $zip )
{
if( !is_array( $zip ) )
$zip = array( 'zip' => (int) $zip );
$qs = $this->_glue( $zip );
$data = $this->_request( $this->request_url . 'districts.getDistrictsFromZip.' . $this->type . $qs );
return (object) $data->response->districts;
}
/**
* A search method for finding which zip codes exist within a given legislative district
* @param string $state required. A two letter postal-code standardized state abbreviation. I.e 'MD','AK','CA'
* @param integer $district_id required. An integer designating which congressional district number. I.e. 3, 12, 1
* @return object
**/
function districtsZipCodes( $state, $district_id )
{
$args = array( 'state' => (string) $state, 'district' => (int) $district_id );
$qs = $this->_glue( $args );
$data = $this->_request( $this->request_url . 'districts.getZipsFromDistrict.' . $this->type . $qs );
return (object) $data->response->zips;
}
/**
* A search method for finding the legislative district containing designated latitude and longitude coordinates
* @param double $latitude required.
* @param double $longitude required
* @return object
**/
function districtsGeoloc( $latitude, $longitude )
{
$qs = $this->_glue( array( 'latitude' => $latitude, 'longitude' => $longitude ) );
$data = $this->_request( $this->request_url . 'districts.getDistrictFromLatLong.' . $this->type . $qs );
foreach( $data->response->districts as $district )
return $district->district;
}
}
/**
* class SunlightCommittee
* PHP Wrapper class around the Sunlight Labs API
* Docs for the Sunlight API is at http://wiki.sunlightlabs.com/index.php/Main_Page
* @author Aaron Brazell <[email protected]>
* @version 1.0
* @package sunlightlabs-php
*/
class SunlightCommittee extends SunlightData {
/**
* A search method for finding committees assigned to the House, Senate or Joint chambers
* @param string $chamber required. Possible values are House, Senate or Joint
* @return object
**/
function committeesList( $chamber )
{
if( !is_array( $chamber ) )
$chamber = array( 'chamber' => $chamber );
$qs = $this->_glue( $chamber );
$data = $this->_request( $this->request_url . 'committees.getList.' . $this->type . $qs );
return (object) $data->response->committees;
}
/**
* A method for retriving data for a specific committee
* @param string $committee_id required. I.e. 'JSPR'
* @return object
**/
function committeesInfo( $committee_id )
{
if( !is_array( $committee_id ) )
$committee_id = array( 'id' => $committee_id );
$qs = $this->_glue( $committee_id );
$data = $this->_request( $this->request_url . 'committees.get.' . $this->type . $qs );
return (object) $data->response->committee;
}
/**
* A method to retrieve data about which committees a given legislator is assigned to
* @param string $bioguide_id required. I.e. S000148 -You can retrieve the bioguide_id
* information with the SunlightLegislator::legislatorSearch() method if you don't know
* @return object
**/
function committeesLegislator( $bioguide_id )
{
if( !is_array( $bioguide_id ) )
$bioguide_id = array( 'bioguide_id' => $bioguide_id );
$qs = $this->_glue( $bioguide_id );
$data = $this->_request( $this->request_url . 'committees.allForLegislator.' . $this->type . $qs );
return (object) $data->response->committees;
}
}
/**
* class SunlightLobbyist
* PHP Wrapper class around the Sunlight Labs API
* Docs for the Sunlight API is at http://wiki.sunlightlabs.com/index.php/Main_Page
* @author Aaron Brazell <[email protected]>
* @version 1.0
* @package sunlightlabs-php
*/
class SunlightLobbyist extends SunlightData {
/**
* A method to retrieve data about a specific lobbyist filing
* @param string $filing_id required. A Senate assigned filing id. I.e 29D4D19E-CB7D-46D2-99F0-27FF15901A4C
* @return object
**/
function lobbyistFiling( $filing_id )
{
if( !is_array( $filing_id ) )
$filing_id = array( 'id' => $filing_id );
$qs = $this->_glue( $filing_id );
$data = $this->_request( $this->request_url . 'lobbyists.getFiling.' . $this->type . $qs );
return (object) $data->response->filing;
}
/**
* A method to retrieve data about a lobbyist
* @param string $lobbyist required. The name or organization of either a lobbyist or client
* @param boolean $is_registrant optional. If true, $lobbyist is a lobbyist. If false, $lobbyist is a client. Default: true
* @param integer $year optional. Restrict results to a 4-digit year. Default: current year
* @return object
**/
function lobbyistInfo( $lobbyist, $is_registrant = true, $year = null )
{
$lobby = array();
if( !$is_registrant ) :
$lobby['registrant_name'] = (string) $lobbyist;
else :
$lobby['client_name'] = (string) $lobbyist;
endif;
if( $year )
$lobby['year'] = (int) $year;
$qs = $this->_glue( $lobby );
$data = $this->_request( $this->request_url . 'lobbyists.getFilingList.' . $this->type . $qs );
return (object) $data->response->filings;
}
/**
* A search method for finding data about lobbyists
* @param string $search required. A search term(s) relating to the name of a lobbyist
* @param integer $year optional. Restricts results to a given search year. Default: current year
* @param double $threshold optional. Restricts results to a given "point score" in relaventivity. Default: 0.9
* @return object
**/
function lobbyistSearch( $search, $year = null, $threshold = '0.9' )
{
if( !$year )
$year = date('Y');
$qs = $this->_glue( array( 'name' => $search, 'year' => (int) $year, 'threshold' => $threshold ) );
$data = $this->_request( $this->request_url . 'lobbyists.search.' . $this->type . $qs );
return (object) $data->response->results;
}
}
/**
* class OpenSecretsMember
* PHP Wrapper class around the OpenSecrets API
* Docs for the OpenSecrets API is at http://www.opensecrets.org/action/api_doc.php
* @author Aaron Brazell <[email protected]>
* @version 1.0
* @package sunlightlabs-php
*/
class OpenSecretsMember extends SunlightData {
/**
* A method to retrieve data about Congress members Personal Financial Disclosures
* @param string $member_ID required. A Member ID as provided by data at OpenSecrets. Example: N00000019
* @param integer $year. A four-digit calendar year. If not provided, the current year is assumed. Example: 2008
* @return object
**/
function memberDisclosure( $member_ID, $year = null )
{
if( !$year )
$year = date('Y');
$qs = $this->_glue( array( 'method' => 'memPFDprofile', 'year' => $year, 'cid' => $member_ID, 'output' => $this->type ) );
$data = $this->_request( $this->request_url . $qs );
return $data->response->member_profile;
}
/**
* A method to retrieve data about Congress members privately financed travel
* @param string $member_ID required. A Member ID as provided by data at OpenSecrets. Example: N00000019
* @param integer $year. A four-digit calendar year. If not provided, the current year is assumed. Example: 2008
* @return object
**/
function memberTravel( $member_ID, $year = null )
{
if( !$year )
$year = date('Y');
$qs = $this->_glue( array( 'method' => 'memTravelTrips', 'year' => $year, 'cid' => $member_ID, 'output' => $this->type ) );
$data = $this->_request( $this->request_url . $qs );
return $data->response->trips;
}
}
/**
* class OpenSecretsCandidate
* PHP Wrapper class around the OpenSecrets API
* Docs for the OpenSecrets API is at http://www.opensecrets.org/action/api_doc.php
* @author Aaron Brazell <[email protected]>
* @version 1.0
* @package sunlightlabs-php
*/
class OpenSecretsCandidate extends SunlightData {
/**
* A method to retrieve summary data about a candidate
* @param string $member_ID required. A Member ID as provided by data at OpenSecrets. Example: N00000019
* @param integer $cycle. A four-digit calendar year. If not provided, the current year is assumed. Example: 2008
* @return object
**/
function candidateInfo( $member_ID, $cycle = null )
{
if( !$cycle )
$cycle = date('Y');
$qs = $this->_glue( array( 'method' => 'candSummary', 'cycle' => (int) $cycle, 'cid' => $member_ID, 'output' => $this->type ) );
$data = $this->_request( $this->request_url . $qs );
return $data->response->summary;
}
/**
* A method to retrieve information about campaign contributions from contributors
* @param string $member_ID required. A Member ID as provided by data at OpenSecrets. Example: N00000019
* @param integer $cycle. A four-digit calendar year. If not provided, the current year is assumed. Example: 2008
* @return object
**/
function candidateContributors( $member_ID, $cycle = null )
{
if( !$cycle )
$cycle = date('Y');
$qs = $this->_glue( array( 'method' => 'candContrib', 'cycle' => (int) $cycle, 'cid' => $member_ID, 'output' => $this->type ) );
$data = $this->_request( $this->request_url . $qs );
return $data->response->contributors;
}
/**
* A method to retrieve information about campaign contributions from sectors
* @param string $member_ID required. A Member ID as provided by data at OpenSecrets. Example: N00000019
* @param integer $cycle. A four-digit calendar year. If not provided, the current year is assumed. Example: 2008
* @return object
**/
function candidateIndustry( $member_ID, $cycle = null )
{
if( !$cycle )
$cycle = date('Y');
$qs = $this->_glue( array( 'method' => 'candIndustry', 'cycle' => (int) $cycle, 'cid' => $member_ID, 'output' => $this->type ) );
$data = $this->_request( $this->request_url . $qs );
return $data->response->industries;
}
/**
* A search method to retrieve summary data about contributions to a specified candidate from a specified industry
* @param string $member_ID required. A Member ID as provided by data at OpenSecrets. Example: N00000019
* @param string $industry_ID required. An industry code for a specified industry as provided by data at OpenSecrets. Example: A01
* @param integer $cycle. A four-digit calendar year. If not provided, the current year is assumed. Example: 2008
* @return object
**/
function candidateIndustryContribution( $member_ID, $industry_ID, $cycle = null )
{
if( !$cycle )
$cycle = date('Y');
$qs = $this->_glue( array( 'method' => 'candIndByInd', 'cycle' => (int) $cycle, 'cid' => $member_ID, 'ind' => $industry_ID, 'output' => $this->type ) );
$data = $this->_request( $this->request_url . $qs );
return $data->response->candIndus;
}
/**
* A method to retrieve information about top sector contributions to a specified candidate
* @param string $member_ID required. A Member ID as provided by data at OpenSecrets. Example: N00000019
* @param integer $cycle. A four-digit calendar year. If not provided, the current year is assumed. Example: 2008
* @return object
**/
function candidateSector( $member_ID, $cycle = null )
{
if( !$cycle )
$cycle = date('Y');
$qs = $this->_glue( array( 'method' => 'candSector', 'cycle' => (int) $cycle, 'cid' => $member_ID, 'output' => $this->type ) );
$data = $this->_request( $this->request_url . $qs );
return $data->response->sectors;
}
}
/**
* class OpenSecretsCommittee
* PHP Wrapper class around the OpenSecrets API
* Docs for the OpenSecrets API is at http://www.opensecrets.org/action/api_doc.php
* @author Aaron Brazell <[email protected]>
* @version 1.0
* @package sunlightlabs-php
*/
class OpenSecretsCommittee extends SunlightData {
/**
* A matrix method to retrieve data about fundraising activities by a specified committee, involving a specific industry, during a specific session of Congress
* @param string $committee_ID required. A Committee ID as provided by data at OpenSecrets or the Sunlight Labs API. Example: HARM
* @param integer $congress_number required. An integer assigned to the session of Congress. Example: 110
* @param string $industry_ID required. An industry code for a specified industry as provided by data at OpenSecrets. Example: A01
* @return object
**/
function committeeFundraisingNexus( $committee_ID, $congress_number, $industry_ID )
{
$qs = $this->_glue( array( 'method' => 'congCmteIndus', 'congno' => (int) $congress_number, 'indus' => $industry_ID, 'cmte' => $committee_ID, 'output' => $this->type ) );
$data = $this->_request( $this->request_url . $qs );
return $data->response->committee;
}
}
?>