Home php教程 php手册 PHP通过其他网站获取IP地域信息实现信息的地区性

PHP通过其他网站获取IP地域信息实现信息的地区性

Jun 06, 2016 pm 07:55 PM
php information accomplish website Obtain pass

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 最近项目要求地域分布信息显示,就对IP及省市信息进行了下了解. 使用了如下2个方案来实现,用户周边信息的获取实现 1.使用了IP库,纯真IP库,可是数据库更新比较慢,文件庞大,相对所需要的资源占用较多. 2

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

   最近项目要求地域分布信息显示,就对IP及省市信息进行了下了解.

   使用了如下2个方案来实现,用户周边信息的获取实现

   1.使用了IP库,纯真IP库,可是数据库更新比较慢,文件庞大,相对所需要的资源占用较多.

   2.调用open.baidu.com的IPSEARCH服务,使用了IP138的数据库,来获取IP所在省市信息,然后在搜索本地数据库(ecshop的region表)得到相对应的regionid ,通过ID关联相关数据

   今天测试了下感觉比较不错贴出实现代码.

   /**
   * 获取IP所在城市的信息
   * TODO:需要构造IP地址缓存
   * @param string $ip
   */
   function get_city($ip=null) {
   import('ORG.Util.Utility');
   $array = array();
   $d = M('region');
   //TODO:这里可以构造缓存以提高加载速度,这里搜索 市 列表 $cities = $d->where('`regiontype`=2')->select(); $ip = ($ip) ? $ip : get_client_ip();
   $url = "http://open.baidu.com/ipsearch/s?wd={$ip}&tn=baiduip";
   $res = mb_convert_encoding(Utility::HttpRequest($url), 'UTF-8', 'GBK');
   if ( preg_match('#来自:(.+)#Ui', $res, $m) ) {
   foreach( $cities AS $value) {
   if ( FALSE !== strpos($m[1], $value['regionname']) ) {
   //返回所在城市的IP信息
   $array['c']=$value;
   $array['p']=$d->where('`regionid`='.$value['parentid'])->find();
   return $array;
   }
   }
   }
   return array();
   }
   调用返回的结果如下array
   'c' =>
   array
   'regionid' => string '386' (length=3)
   'parentid' => string '31' (length=2)
   'regionname' => string '金华' (length=6)
   'regiontype' => string '2' (length=1)
   'agencyid' => string '0' (length=1)
   'ename' => string 'jinhua' (length=6)
   'p' =>
   array
   'regionid' => string '31' (length=2)
   'parentid' => string '1' (length=1)
   'regionname' => string '浙江' (length=6)
   'regiontype' => string '1' (length=1)
   'agencyid' => string '0' (length=1)
   'ename' => string 'zhejiang' (length=8)

   用了IP测试了下,信息还是比较准确的,IP138的IP数据库和123CHA的数据库都是国内时间库里面相对比较全面的一个.通过这种方式,性能上还可以。

PHP通过其他网站获取IP地域信息实现信息的地区性

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles