суббота, 3 августа 2013 г.

PHP: Determination of the country by IP


PHP: Determination of the country by IP by IP using geoplugin. net OR ipinfodb. com - return the country code in "Alpha-2 ISO 3166-1" format (a two-char code)

  1. function get_country($ip_addr) {
  2. $curlopt_useragent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0';
  3. $url = 'http://www.geoplugin.net/php.gp?ip=' . $ip_addr;
  4. $ch1 = curl_init();
  5. curl_setopt($ch1, CURLOPT_URL, $url);
  6. curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
  7. $result = curl_exec($ch1);
  8. $geoplugin = unserialize($result);
  9.  
  10. if ($geoplugin[geoplugin_countryCode] != '') {
  11. return $geoplugin[geoplugin_countryCode];
  12. } else {
  13. $url = 'http://ipinfodb.com/ip_locator.php?ip=' . urlencode($ip_addr);
  14. $ch = curl_init();
  15.  
  16. $curl_opt = array(
  17. CURLOPT_FOLLOWLOCATION => 1,
  18. CURLOPT_HEADER => 0,
  19. CURLOPT_RETURNTRANSFER => 1,
  20. CURLOPT_USERAGENT => $curlopt_useragent,
  21. CURLOPT_URL => $url,
  22. CURLOPT_TIMEOUT => 1,
  23. CURLOPT_REFERER => 'http://' . $_SERVER['HTTP_HOST'],
  24. );
  25. curl_setopt_array($ch, $curl_opt);
  26. $content = curl_exec($ch);
  27.  
  28. if (!is_null($curl_info)) {
  29. $curl_info = curl_getinfo($ch);
  30. }
  31.  
  32. curl_close($ch);
  33.  
  34. if (preg_match('{<li>Country : ([^<]*)}i', $content, $regs))
  35. $country = $regs[1];
  36.  
  37. return $country;
  38. }
  39. }

Комментариев нет:

Отправить комментарий

Постоянные читатели

Популярные сообщения