| 1 |
<?php // $Id: votesmart.api.inc,v 1.2 2008/08/10 01:42:51 vauxia Exp $
|
| 2 |
|
| 3 |
function _votesmart_api($request, $args = array(), $item = null) {
|
| 4 |
$key = variable_get('votesmart_key', '');
|
| 5 |
$url = variable_get('votesmart_host', 'http://api.votesmart.org/');
|
| 6 |
|
| 7 |
// Convert arguments into a request string.
|
| 8 |
$args = array_merge($args, array('key' => $key));
|
| 9 |
$url .= $request . '?';
|
| 10 |
foreach ($args as $k => $v) {
|
| 11 |
$url .= check_plain($k) . '=' . check_plain($v) . '&';;
|
| 12 |
}
|
| 13 |
|
| 14 |
// Contact the REST API.
|
| 15 |
$res = drupal_http_request($url);
|
| 16 |
$ret = (array) new SimpleXMLElement($res->data, LIBXML_NOCDATA);
|
| 17 |
|
| 18 |
// TODO check $ret['errorMessage']
|
| 19 |
if (isset($ret['errorMessage'])) {
|
| 20 |
drupal_set_message(t('Vote Smart API error: %err', array('%err' => $ret['errorMessage'])), 'error');
|
| 21 |
}
|
| 22 |
|
| 23 |
// Return specified element (e.g. 'details', 'list', etc.);
|
| 24 |
if ($item) {
|
| 25 |
foreach (explode('.', $item) as $key) {
|
| 26 |
$ret = (array) $ret[$key];
|
| 27 |
}
|
| 28 |
}
|
| 29 |
|
| 30 |
// Nothing specified, return the entire object.
|
| 31 |
return $ret;
|
| 32 |
}
|