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

不言
Release: 2023-04-04 19:06:02
forward
5066 people have browsed it

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 '
Copy after login
';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!

Related labels:
php
source:segmentfault.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template