3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
35 class CRM_Utils_Sunlight
{
36 static $_apiURL = 'http://api.sunlightlabs.com/';
37 static $_apiKey = NULL;
39 static function makeAPICall($uri) {
40 require_once 'HTTP/Request.php';
42 'method' => HTTP_REQUEST_METHOD_GET
,
43 'allowRedirects' => FALSE,
46 $request = new HTTP_Request(self
::$_apiURL . $uri, $params);
47 $result = $request->sendRequest();
48 if (PEAR
::isError($result)) {
49 CRM_Core_Error
::fatal($result->getMessage());
51 if ($request->getResponseCode() != 200) {
52 CRM_Core_Error
::fatal(ts('Invalid response code received from Sunlight servers: %1',
53 array(1 => $request->getResponseCode())
56 $string = $request->getResponseBody();
57 return simplexml_load_string($string);
60 static function getCityState($zipcode) {
61 $key = self
::$_apiKey;
62 $uri = "places.getCityStateFromZip.php?zip={$zipcode}&apikey={$key}&output=xml";
63 $xml = self
::makeAPICall($uri);
65 return array($xml->city
, $xml->state
);
68 static function getDetailedInfo($peopleID) {
69 $key = self
::$_apiKey;
70 $uri = "people.getPersonInfo.php?id={$peopleID}&apikey={$key}&output=xml";
71 $xml = self
::makeAPICall($uri);
76 'firstname' => 'first_name',
77 'lastname' => 'last_name',
80 'congress_office' => 'address',
83 'congresspedia' => 'url',
84 'photo' => 'image_url',
85 'webform' => 'contact_url',
88 foreach ($fields as $old => $new) {
89 $result[$new] = (string ) $xml->$old;
92 $result['image_url'] = 'http://sunlightlabs.com/widgets/popuppoliticians/resources/images/' . $result['image_url'];
97 static function getPeopleInfo($uri) {
98 $xml = self
::makeAPICall($uri);
101 foreach ($xml->entity_id_list
->entity_id
as $key => $value) {
102 $result[] = self
::getDetailedInfo($value);
107 static function getRepresentativeInfo($city, $state) {
113 $key = self
::$_apiKey;
114 $city = urlencode($city);
115 $uri = "people.reps.getRepsFromCityState.php?city={$city}&state={$state}&apikey={$key}&output=xml";
116 return self
::getPeopleInfo($uri);
119 static function getSenatorInfo($state) {
124 $key = self
::$_apiKey;
125 $uri = "people.sens.getSensFromState.php?state={$state}&apikey={$key}&output=xml";
126 return self
::getPeopleInfo($uri);
129 static function getInfo($city, $state, $zipcode = NULL) {
131 list($city, $state) = self
::getCityState($zipcode);
134 $reps = self
::getRepresentativeInfo($city, $state);
135 $sens = self
::getSenatorInfo($state);
138 if (is_array($reps)) {
139 $result = array_merge($result, $reps);
141 if (is_array($sens)) {
142 $result = array_merge($result, $sens);