Methods for querying ip address locations in PHP

Source: Internet
Author: User
Find the location of our ip address based on the ip address. The easiest way is to directly search the ip address in Baidu to know the location of your ip address, but we cannot use this method for some websites, but it can be implemented through APIs. example: use a third-party interface instance to replace... find the location of our ip address based on the ip address. The easiest way is to directly search the ip address in Baidu to know the location of your ip address, but we cannot use this method for some websites, but it can be implemented through APIs.

For example, the code for using a third-party interface instance is as follows:

 
 (.*?)/", $ Str, $ addr1); // final result, 2; $ addr = $ ip. "=> ". $ addr1 [1] [0]; echo $ addr ;}?>

For example, QQWry currently only supports PHP5.

Installation:

1. sudo pecl install qqwry-beta or directly download and compile phpize &./configure & make & sudo make install

2. edit php. ini and add extension = qqwry. so.

3. restart the http server

It is easy to use. The first step is to instantiate the file path of the pure database, and then call the q method. this method returns an array. The first value is Address 1, for example, Nanning, the second value is Address 2, for example, Suining telecom ADSL. you can see it clearly ., The iconv below is to convert data from GB2312 to UTF-8, if your site is using GB2312, then this step is not needed, the code is as follows:

 Q ('2017. 0.0.1 '); $ addr1 = iconv ('gb2312', 'utf-8', $ addr1); $ addr2 = iconv ('gb2312', 'utf-8 ', $ addr2); echo $ addr1, '|', $ addr2, ""; $ arr = $ qqwry-> q ('2017. 222. 216.47.4 '); $ arr [0] = iconv ('gb2312', 'utf-8', $ arr [0]); $ arr [1] = iconv ('gb2312', 'utf-8', $ arr [1]); echo $ arr [0], '| ', $ arr [1], ""; $ arr = $ qqwry-> q ('64. 233.187.99 '); $ arr [0] = iconv ('gb2312', 'utf-8', $ arr [0]); $ arr [1] = iconv ('gb2312', 'utf-8', $ arr [1 ]); Echo $ arr [0], '|', $ arr [1], ""; // output: // address of the local machine | // Nanning, Guangxi | (Qingxiu district) china Telecom ADSL // US | Google, California?>

For example, if you only need to check the IP address, it does not need to be as complicated as that. for example, if I enter 110.110.110.110 in Baidu

Display address: IP address: 110.110.110.110 Harbin Tietong, Heilongjiang province

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.