Several methods for querying ip address locations in PHP (api instance)

Source: Internet
Author: User

Example,Use third-party interface instances

The code is as follows: Copy code


<Form action = www.111cn.net method = "post">

<P> enter an ip address: <input type = "text" name = "ip"/> </p>

<P> <input type = "submit" value = "query"/> </p>

</Form>

<? Php

// Obtain the IP address

// $ Ip = $ _ SERVER ['remote _ ADDR ']; // automatically obtains the IP address of the client.

// Region corresponding to the ip address

If (! Empty ($ _ POST ['IP']) {

$ Ip = $ _ POST ['IP'];

// Interface address, which returns an xml result set,

$ Str = file_get_contents ("http://www.yodao.com/smartresult-xml/search.s? Type = ip & q = ". $ ip );

// Here we need to get the address information. There are many xml extraction methods, and I use regular expressions.

Preg_match_all ("/<location> (.*?) </Location>/", $ str, $ addr1 );

// The final result, as shown in figure 2;

$ Addr = $ ip. "=>". $ addr1 [1] [0];

Echo $ addr;

 }

?>

Example,Use QQWry


Currently, only PHP5 is supported.

Install

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 import 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, and the second value is Address 2, for example, China Telecom ADSL in Suining district. Let's take a look at the example. The iconv below is to transfer data from GB2312 to UTF-8, if your site is using GB2312, then this step is not needed.

The code is as follows: Copy code


$ Qqwry = new qqwry ('qqwry. Dat ');

List ($ addr1, $ addr2) = $ qqwry-> q ('2017. 0.0.1 ');
$ Addr1 = iconv ('gb2312', 'utf-8', $ addr1 );
$ Addr2 = iconv ('gb2312', 'utf-8', $ addr2 );
Echo $ addr1, '|', $ addr2 ,"";

$ Arr = $ qqwry-> q ('2017. 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:

Local Address |
Nanning, Guangxi | (Qingxiu district) China Telecom ADSL
US | Google Inc., California

Example,If you only need to check the IP address, it does not need to be so complex. For example, if I enter 110.110.110.110 in Baidu

The display address is

IP address: 110.110.110.110 Harbin Tietong, Heilongjiang province

As shown in the following figure.

 

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.