Home  >  Article  >  Backend Development  >  How does php get its actual address based on ip (with example)

How does php get its actual address based on ip (with example)

不言
不言forward
2019-01-16 10:21:005054browse

The content of this article is about how PHP obtains its actual address based on IP (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

There are many tutorials on the Internet for obtaining the actual address based on the IP address

They provide many ready-made interfaces, for example:

Sina: 'http://int. dpool.sina.com.cn/...' . $ip

Taobao: "http://ip.taobao.com/service/getIpInfo.php?ip=".$ip

Wait for the interface to get the actual address based on the IP address, but these interfaces are very unstable. Here I recommend Baidu Map’s interface to get the actual address based on the IP. After testing, it is very fast and very stable

1: Log in to http://lbsyun.baidu.com/index... to get the secret key:

How does php get its actual address based on ip (with example)

# #Fill in the relevant information to obtain a string of secret keys

2: Obtain the actual address information based on the interface address provided by Baidu Map

Here is an example of ordinary IP positioning:

How does php get its actual address based on ip (with example)Write the code to get the actual address based on the ip according to the tutorial:

<?php function curl($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    //参数为1表示传输数据,为0表示直接输出显示。
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //参数为0表示不带头文件,为1表示带头文件
    curl_setopt($ch, CURLOPT_HEADER,0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}
$data_info=curl("http://api.map.baidu.com/location/ip?ip=218.94.147.138&ak=XXXXXXXXX");
//$data_info=file_get_contents("http://api.map.baidu.com/location/ip?ip=218.94.147.138&ak=XXXXXXXXX");
$data_info = json_decode($data_info,true); //数据转换
echo &#39;<pre class="brush:php;toolbar:false">';
print_r($data_info);
echo '
';The output result is:

How does php get its actual address based on ip (with example)

The above is the detailed content of How does php get its actual address based on ip (with example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete