'Get' webservice implementation

aurevo1r - October 23, 2008 - 06:12
Project:GeoNames
Version:6.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:closed
Description

I'm looking to do a lookup for a non-cached item on a geonamesId using the get webservice. Since it doesn't exist, I'm trying my hand at implementing it.

  $config['get'] = array(
      'service_name'        => 'get',
      'service_full_name'   => 'Get using geonameId',
      'description'         => 'Find Geoname using geonameId',
      'service_path'        => 'get',
      'credit_cost'         => 1,
      'result_cache_prefix' => 'getres:',
      'data_cache_prefix'   => 'getdat:',
      'allowed_parameters'  => array(
        'geonameid' => 'geonameId',
        'style' => 'style'
      ),
      'columns' => array(
        'name',
        'lat',
        'long',
        'geonameId',
        'countryCode',
      ),
    );

The while the webservice returns data, the Geonames module does not correctly parse the XML result. I suspect in part it has to do with totalResultsCount not returned in the XML for the get service. The XML traversal appears to be ok, though I haven't tested it explicitly against the result set with one less parent node.

#1

aurevo1r - October 23, 2008 - 06:20

Also, here's the result of a get query.

stdClass Object
(
    [standalone] => no
    [total_results_count] => 0
    [service] => get
    [request] => Array
        (
            [url] => http://ws.geonames.org/get?geonameId=4275586&style=FULL
            [bytes] => 664
            [cached] => result
            [seconds] => 0.00544309616089
        )

    [query] => Array
        (
            [geonameid] => 4275586
            [style] => full
        )

)

#2

SeroSero - October 23, 2008 - 06:46

Hi,

The 'get' service is structured diffently than the others -- so the current parser won't be able to produce the proper output. I have actually implemented this service myself, but not tested it properly yet. Here's a custom parser for the get service;

/**
* Parser for the Get service
*/
function geonames_parse_get_xml($data) {
  $xml = new SimpleXMLElement($data);
  foreach ($xml->children() as $key => $entry) {
    $k = (string) strtolower($key);
    $v = (string) $entry;
    if ($key != 'status') {
      $fields[$k] = $v;
    }
    else {
      $result->status['message'] = (string) $xml->status['message'];
      $result->status['value']   = (string) $xml->status['value'];
    }
  }
  if (!empty($fields)) {
    $result->results[] = $fields;
    $result->total_results_count = 1;
  }   
  else {
    $result->total_results_count = 0;
  }
  return $result;       
}

My service config looks like this:

  $config['get'] = array(
    'service_name'        => 'get',
    'service_full_name'   => 'Get',
    'description'         => 'Retrieve GeoNames record by geonameid',
    'service_path'        => 'get',
    'credit_cost'         => 1,
    'result_cache_prefix' => 'gres:',
    'data_cache_prefix'   => 'gdat:',
    'allowed_parameters'  => array(
      'geonameid' => 'geonameId',
      'lang'      => 'lang',
    ),
    'columns' => array(
      'geoname',
    ),
  );

..and you will have to trigger the proper parser - change

    $result = geonames_parse_xml($data, $fields);

to
    $result = ($service == 'get') ? geonames_parse_get_xml($data) : geonames_parse_xml($data, $fields);

I'm unsure if this is complete -- I've just copied and pasted from my dev-version. (I wasn't aware of the style parameter.. just add it like you did so any queries using this won't fail.)....

Have a nice day!

Sero

#3

SeroSero - October 23, 2008 - 07:25
Category:bug report» feature request
Assigned to:Anonymous» SeroSero
Status:active» needs work

#4

aurevo1r - October 23, 2008 - 16:31

Thanks. I'll try it out and report any issues I have with the patch and if I can, implement a fix.

#5

BWPanda - September 8, 2009 - 06:48
Title:http://ws.geonames.org/get webservice implementation not parsing» 'Get' webservice implementation
Version:6.x-1.1» 6.x-1.x-dev
Assigned to:SeroSero» Anonymous
Status:needs work» needs review

Here's a patch of SeroSero's changes. Seems to work alright, though haven't tested everything...

AttachmentSize
geonames_get.patch 2.53 KB

#6

lyricnz - September 14, 2009 - 14:42
Status:needs review» fixed

Fixed in http://drupal.org/cvs?commit=262756

#7

System Message - September 28, 2009 - 14:50
Status:fixed» closed

Automatically closed -- issue fixed for 2 weeks with no activity.

 
 

Drupal is a registered trademark of Dries Buytaert.