3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 class CRM_Utils_Sunlight
{
36 static $_apiURL = 'http://api.sunlightlabs.com/';
37 static $_apiKey = NULL;
42 * @return SimpleXMLElement
45 public static function makeAPICall($uri) {
46 require_once 'HTTP/Request.php';
48 'method' => HTTP_REQUEST_METHOD_GET
,
49 'allowRedirects' => FALSE,
52 $request = new HTTP_Request(self
::$_apiURL . $uri, $params);
53 $result = $request->sendRequest();
54 if (PEAR
::isError($result)) {
55 CRM_Core_Error
::fatal($result->getMessage());
57 if ($request->getResponseCode() != 200) {
58 CRM_Core_Error
::fatal(ts('Invalid response code received from Sunlight servers: %1',
59 array(1 => $request->getResponseCode())
62 $string = $request->getResponseBody();
63 return simplexml_load_string($string);
71 public static function getCityState($zipcode) {
72 $key = self
::$_apiKey;
73 $uri = "places.getCityStateFromZip.php?zip={$zipcode}&apikey={$key}&output=xml";
74 $xml = self
::makeAPICall($uri);
76 return array($xml->city
, $xml->state
);
80 * @param int $peopleID
84 public static function getDetailedInfo($peopleID) {
85 $key = self
::$_apiKey;
86 $uri = "people.getPersonInfo.php?id={$peopleID}&apikey={$key}&output=xml";
87 $xml = self
::makeAPICall($uri);
92 'firstname' => 'first_name',
93 'lastname' => 'last_name',
96 'congress_office' => 'address',
99 'congresspedia' => 'url',
100 'photo' => 'image_url',
101 'webform' => 'contact_url',
104 foreach ($fields as $old => $new) {
105 $result[$new] = (string ) $xml->$old;
108 $result['image_url'] = 'http://sunlightlabs.com/widgets/popuppoliticians/resources/images/' . $result['image_url'];
118 public static function getPeopleInfo($uri) {
119 $xml = self
::makeAPICall($uri);
122 foreach ($xml->entity_id_list
->entity_id
as $key => $value) {
123 $result[] = self
::getDetailedInfo($value);
134 public static function getRepresentativeInfo($city, $state) {
140 $key = self
::$_apiKey;
141 $city = urlencode($city);
142 $uri = "people.reps.getRepsFromCityState.php?city={$city}&state={$state}&apikey={$key}&output=xml";
143 return self
::getPeopleInfo($uri);
151 public static function getSenatorInfo($state) {
156 $key = self
::$_apiKey;
157 $uri = "people.sens.getSensFromState.php?state={$state}&apikey={$key}&output=xml";
158 return self
::getPeopleInfo($uri);
164 * @param null $zipcode
168 public static function getInfo($city, $state, $zipcode = NULL) {
170 list($city, $state) = self
::getCityState($zipcode);
173 $reps = self
::getRepresentativeInfo($city, $state);
174 $sens = self
::getSenatorInfo($state);
177 if (is_array($reps)) {
178 $result = array_merge($result, $reps);
180 if (is_array($sens)) {
181 $result = array_merge($result, $sens);