The article introduces the use of relevant api interfaces to obtain a php instance in the city and region based on the client IP address. Let's take a look.
Today I will share a small PHP program that obtains the location based on the client IP address. I wrote it before and have been using it for a long time. I feel that this interface is still stable. Although there are many similar ones on the Internet, I think mine is still very good. The core code can be implemented in only 3 lines. Although the function is not very powerful, it is enough for our daily use. Here is the code:
The code is as follows
代码如下 |
复制代码 |
通过IP获取归属地
图1 : 返回数据
图2 : 查询成功
如果喜欢的话,可以试试,几行代码就能搞定,我觉得还是挺实用的!!
本文章来源于 代潇瑞博客 原文地址:http://www.daixiaorui.com/read/3.html
//获取ip地址 //$ip = $_SERVER['REMOTE_ADDR']; //自动获取客户端的IP //ip对应的地区 if(!empty($_POST['ip'])){ $ip = $_POST['ip']; //接口地址,这样返回的是一个xml结果集,如图1; $str = file_get_contents("http://www.yodao.com/smartresult-xml/search.s?type=ip&q=".$ip); //这里要得到里面的地址信息,提取xml方法有很多,我用的是正则。 preg_match_all( "/
(.*?)
/",$str,$addr1); //最终结果,如图2; $addr = $ip."=>".$addr1[1][0]; echo $addr; } ?> |
|
Copy code
|
Get ownership via IP
|
Figure 1: Return data
Figure 2: Query successful
If you like it, you can try it. It can be done with a few lines of code. I think it is quite practical! ! This article comes from Dai Xiaorui’s blog. Original address: http://www.daixiaorui.com/read/3.html
<> //Get IP address <> <> //$ip = $_SERVER['REMOTE_ADDR']; //Automatically obtain the client’s IP<> <> //Region corresponding to IP<> <> if(!empty($_POST['ip'])){<> <> $ip = $_POST['ip'];<> <> //Interface address, this 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);<> <> //To get the address information inside, there are many ways to extract xml. I use regular expressions. <> <> preg_match_all( "/
(.*?)
/",$str,$addr1); //Final result, as shown in Figure 2; $addr = $ip."=>".$addr1[1][0]; echo $addr; } ?>Figure 1: Return data Figure 2: Query successful If you like it, you can try it. It can be done with a few lines of code. I think it is quite practical! ! http://www.bkjia.com/PHPjc/632723.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632723.htmlTechArticleThe article introduces the use of relevant api interfaces to obtain a php instance in the city and region based on the client IP address. Let's do it together. have a look. Today I will share a PHP method to obtain...
Statement:
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