Jacek Kowalski
2014-06-11 1128f2ee2379a8473a36631c51b7c2dfb39c0651
commit | author | age
2fb07e 1 <?php
JK 2 require_once(dirname(__FILE__).'/api_geonames_config.php');
3
4 class api_geonames extends api_geonames_config {
5     function search($name) {
6         $url = 'http://'.$this->host.'/search?name='.urlencode($name).'&lang=pl&maxrows=3'.($this->username !== NULL ? '&username='.urlencode($this->username) : '');
7         
8         try {
9             $download = new DownloadHelper($url);
10             $data = $download->exec();
11             
12             if(!$data) {
13                 $download->cacheFor(600);
14                 return FALSE;
15             }
16             
17             libxml_use_internal_errors();
48c64f 18             $data = simplexml_load_string($data);
2fb07e 19             libxml_clear_errors();
JK 20             
21             if(!$data) {
22                 $download->cacheFor(600);
23                 return FALSE;
24             }
25             
dd796a 26             // Trzymaj w cache przez około 116 dni
JK 27             $download->cacheFor(10000000);
2fb07e 28             
10ce8b 29             if((int)$data->totalResultsCount == 0 || $data->geoname->getName() != 'geoname' || $data->geoname[0]->getName() != 'geoname'
2fb07e 30                 || $data->geoname[0]->name->getName() != 'name'
JK 31                 || $data->geoname[0]->countryName->getName() != 'countryName'
32                 || $data->geoname[0]->lat->getName() != 'lat'
33                 || $data->geoname[0]->lng->getName() != 'lng') {
34                 return NULL;
35             }
36             
efd600 37             $data = (array)$data->geoname[0];
2fb07e 38             foreach($data as &$value) {
JK 39                 $value = trim($value);
40             }
41             unset($value);
42             
43             if(isset($data['countryName']) && $data['countryName'] == 'Rzeczpospolita Polska') {
44                 $data['countryName'] = 'Polska';
45             }
46             
47             return $data;
48         }
49         catch(Exception $e) {
50             return FALSE;
51         }
52     }
53 }
54 ?>