Obtener los codigos de paises y almacenarlos en una tabla


/ Published in: PHP
Save to your folder(s)

Esto es para obtener los codigos de paises desde geonames.org y almacenarlos en una tabla. Se iso bajo CodeIgniter.


Copy this code and paste it in your HTML
  1. class Welcome {
  2. function obtener_codigos_de_paises() {
  3.  
  4. $this->db->where('iso', '');
  5. $query = $this->db->get('country');
  6.  
  7. foreach ($query->result() as $row) {
  8. if ($row->name == 'N/A' || $row->name == 'Otro') continue;
  9.  
  10. $country = urlencode(str_replace(array('á', 'é', 'í', 'ó', 'ú', 'ñ'), array('a', 'e', 'i', 'o', 'u', 'n'), $row->name));
  11.  
  12. $ws_url = "http://api.geonames.org/searchJSON?maxRows=10&q=$country&username=demo&lang=es";
  13. $data = json_decode(file_get_contents($ws_url));
  14.  
  15. if (isset($data->geonames[0]->countryCode)) {
  16. $this->db->set('iso', $data->geonames[0]->countryCode);
  17. $this->db->where('COUNTRY_ID', $row->COUNTRY_ID);
  18. $this->db->update('country');
  19. }
  20.  
  21. }
  22. }
  23.  
  24. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.