PHP calls external services to obtain IP region information to achieve regional association of information

Source: Internet
Author: User

Recently, the project requires that the region distribution information be displayed, so you can understand the IP address and province and city information.

The following two solutions are used to obtain the user's peripheral information:

 

1. The IP library is used, and the pure IP library is used. However, the database update is slow, the file is large, and the resource usage is relatively high.

2. call the IPSEARCH service of open.baidu.com and use the IP138 database to obtain information about the province and city where the IP is located. Then, search the local database (the region table of ecshop) to obtain the corresponding regionid, associate related data by ID

I tested it today and found it quite good to post the implementation code.

 

/*** Get the information of the city where the IP is located * TODO: the ip address cache needs to be constructed * @ param string $ ip */function get_city ($ IP = null) {import ('org. util. utility '); $ array = array (); $ d = M ('District'); // TODO: You can construct a cache to increase the loading speed. Here, you can search for the market list.
// The district Table of discuzx1 is replaced here.
$ Cities = $ d-> getDistrict (); // memcached cache is set here.
$ 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 ('# From: <B> (. +) </B> # Ui ', $ res, $ m) {foreach ($ cities AS $ value) {if (FALSE! = Strpos ($ m [1], $ value ['regionname']) {// returns the IP address information of the city in which it belongs. $ array ['C'] = $ value; $ array ['P'] = $ d-> where (''regionid' = '. $ value ['parentid'])-> find (); return $ array ;}} return array ();}
The result returned by the call is as follows:
Array 'c' => array 'regionid' => string '000000' (length = 3) 'parentid' => string '31' (length = 2) 'regionname' => string 'jinhua '(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 'zhejiang '(length = 6) 'regiontype' => string '1' (length = 1) 'agencyid' => string '0' (length = 1) 'ename' => string 'zhejiang '(length = 8)
After testing with IP addresses, the information is still relatively accurate. The IP138 IP database and 123CHA database are both relatively comprehensive in the domestic time database. in this way, the performance is acceptable.
Notes after use
1. After the address uses memcached, it is found that the cache cannot be cached when all the information is loaded. Here I use the memcached cache.
Memcached limits the maximum acceptable item to be 1 MB. Data larger than 1 MB is ignored.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.