PHP query ip address location (province, city)

Source: Internet
Author: User

Today, I am sharing a PHP app that obtains the location of the app based on the client IP address. I have been using this app for a long time and I feel that this interface is still stable. Although there are many similar items on the Internet, I think this is quite good. The core code can be implemented in only three lines. Although the function is not very powerful, we usually use it. The following code is used:

 

The code is as follows: Copy code

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>

<Title> obtain the region through IP address </title>

</Head>

 

 

 


Figure 1: returned data

 

 


Figure 2: query successful

 


If you like it, you can try it and get a few lines of code. I think it is quite practical !!

This article from the generation of Ruibo customer original address: http://www.daixiaorui.com/read/3.html

<Body>

<Form action = "" 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, as shown in Figure 1;

$ 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;

 }

?>

</Body>

</Html>


Figure 1: returned data


Figure 2: query successful


If you like it, you can try it and get a few lines of code. I think it is quite practical !!

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.