Home  >  Article  >  Backend Development  >  PHP gets current geographical location interface based on IP address

PHP gets current geographical location interface based on IP address

WBOY
WBOYOriginal
2016-07-25 09:07:311260browse
  1. function address_baidu($ip) {
  2. $u = "";
  3. $address = file_get_contents(" http://open.baidu.com/ipsearch/s?wd={$ip }&tn=baiduip");
  4. preg_match('#From: (.+)#Ui', $address, $m);
  5. return strval($m[1]);
  6. }
  7. function GetRemoteIp($default='127.0.0.1')
  8. {
  9. $ip_string = $_SERVER['HTTP_CLIENT_IP'].','.$_SERVER['HTTP_X_FORWARDED_FOR'].','.$_SERVER['REMOTE_ADDR' ];
  10. if ( preg_match ("/d+.d+.d+.d+/", $ip_string, $matches) )
  11. {
  12. return $matches[0];
  13. }
  14. return $default;
  15. }
  16. $ip = GetRemoteIp ();
  17. $addr = address_baidu($ip);
Copy code

There are other methods: Tencent Sina's interface to obtain the current geographical location (province) through IP address

Tencent’s interface is and returns an array http://fw.qq.com/ipaddress

Return value var IPData = new Array("61.135.152.194","","Beijing City","");

Sina interface: http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js Multi-region testing method: http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=218.192.3.42

Easy to use:

Copy code

>>>



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn