let Geonames clients use commercial Web service

This commit is contained in:
Evan Prodromou 2009-12-04 15:30:43 -05:00
parent e7e9dfceb4
commit 99b23782ef

View File

@ -51,6 +51,10 @@ class GeonamesPlugin extends Plugin
{ {
const LOCATION_NS = 1; const LOCATION_NS = 1;
public $host = 'ws.geonames.org';
public $username = null;
public $token = null;
/** /**
* convert a name into a Location object * convert a name into a Location object
* *
@ -75,12 +79,11 @@ class GeonamesPlugin extends Plugin
// XXX: break down a name by commas, narrow by each // XXX: break down a name by commas, narrow by each
$str = http_build_query(array('maxRows' => 1, $result = $client->get($this->wsUrl('search',
array('maxRows' => 1,
'q' => $name, 'q' => $name,
'lang' => $language, 'lang' => $language,
'type' => 'json')); 'type' => 'json')));
$result = $client->get('http://ws.geonames.org/search?'.$str);
if ($result->isOk()) { if ($result->isOk()) {
$rj = json_decode($result->getBody()); $rj = json_decode($result->getBody());
@ -135,10 +138,9 @@ class GeonamesPlugin extends Plugin
$client = HTTPClient::start(); $client = HTTPClient::start();
$str = http_build_query(array('geonameId' => $id, $result = $client->get($this->wsUrl('hierarchyJSON',
'lang' => $language)); array('geonameId' => $id,
'lang' => $language)));
$result = $client->get('http://ws.geonames.org/hierarchyJSON?'.$str);
if ($result->isOk()) { if ($result->isOk()) {
@ -205,12 +207,11 @@ class GeonamesPlugin extends Plugin
$client = HTTPClient::start(); $client = HTTPClient::start();
$str = http_build_query(array('lat' => $lat,
'lng' => $lon,
'lang' => $language));
$result = $result =
$client->get('http://ws.geonames.org/findNearbyPlaceNameJSON?'.$str); $client->get($this->wsUrl('findNearbyPlaceNameJSON',
array('lat' => $lat,
'lng' => $lon,
'lang' => $language)));
if ($result->isOk()) { if ($result->isOk()) {
@ -286,10 +287,9 @@ class GeonamesPlugin extends Plugin
$client = HTTPClient::start(); $client = HTTPClient::start();
$str = http_build_query(array('geonameId' => $location->location_id, $result = $client->get($this->wsUrl('hierarchyJSON',
'lang' => $language)); array('geonameId' => $location->location_id,
'lang' => $language)));
$result = $client->get('http://ws.geonames.org/hierarchyJSON?'.$str);
if ($result->isOk()) { if ($result->isOk()) {
@ -376,7 +376,7 @@ class GeonamesPlugin extends Plugin
{ {
$c = common_memcache(); $c = common_memcache();
if (!$c) { if (empty($c)) {
return null; return null;
} }
@ -387,7 +387,7 @@ class GeonamesPlugin extends Plugin
{ {
$c = common_memcache(); $c = common_memcache();
if (!$c) { if (empty($c)) {
return null; return null;
} }
@ -398,7 +398,7 @@ class GeonamesPlugin extends Plugin
{ {
$c = common_memcache(); $c = common_memcache();
if (!$c) { if (empty($c)) {
return null; return null;
} }
@ -411,4 +411,19 @@ class GeonamesPlugin extends Plugin
implode(',', array_keys($attrs)) . ':'. implode(',', array_keys($attrs)) . ':'.
common_keyize(implode(',', array_values($attrs)))); common_keyize(implode(',', array_values($attrs))));
} }
function wsUrl($method, $params)
{
if (!empty($this->username)) {
$params['username'] = $this->username;
}
if (!empty($this->token)) {
$params['token'] = $this->token;
}
$str = http_build_query($params);
return 'http://'.$this->host.'/'.$method.'?'.$str;
}
} }