/** * Use the Geonames web service to look up an administrative name. * * @since 1.4 * * @param string $country_code * @param string $admin_code Admin area name to look up. If empty, look up the country name. * @return string|WP_Error The administrative name. */ public function get_administrative_name($country_code, $admin_code = '') { if (empty($admin_code)) { // Look up a country name $country_info_url = 'http://api.geonames.org/countryInfoJSON?username='******'&country=' . urlencode($country_code) . '&lang=' . urlencode($this->language); $country_info_response = $this->http->get($country_info_url, $this->request_params); if (is_wp_error($country_info_response)) { return $country_info_response; } $status = $country_info_response['response']['code']; if ('200' != $status) { return new WP_Error('geocoder_http_request_failed', $status . ': ' . $country_info_response['response']['message'], $country_info_response); } $data = json_decode($country_info_response['body']); if (empty($data->geonames)) { return ''; } $country_name = $data->geonames[0]->countryName; $country_id = $data->geonames[0]->geonameId; if (!empty($country_name)) { GeoMashupDB::cache_administrative_name($country_code, '', $this->language, $country_name, $country_id); } return $country_name; } else { // Look up an admin name $admin_search_url = 'http://api.geonames.org/searchJSON?maxRows=1&style=SHORT&featureCode=ADM1&username='******'&country=' . urlencode($country_code) . '&adminCode1=' . urlencode($admin_code); $admin_search_response = $this->http->get($admin_search_url, $this->request_params); if (is_wp_error($admin_search_response)) { return $admin_search_response; } $status = $admin_search_response['response']['code']; if ('200' != $status) { return new WP_Error('geocoder_http_request_failed', $status . ': ' . $admin_search_response['response']['message'], $admin_search_response); } $data = json_decode($admin_search_response['body']); if (empty($data) or 0 == $data->totalResultsCount) { return ''; } $admin_name = $data->geonames[0]->name; $admin_id = $data->geonames[0]->geonameId; if (!empty($admin_name)) { GeoMashupDB::cache_administrative_name($country_code, $admin_code, $this->language, $admin_name, $admin_id); } return $admin_name; } return ''; }