function getIpAddress($ip = ''){ // ip属地(来自太平洋电脑网)
if(empty($ip)){
$ip = $_REQUEST['ip'];
if(empty($ip)) die('请传输ip地址'); // 根据实际调用方式去返回数据
}
$ch = curl_init();
$url = 'https://whois.pconline.com.cn/ipJson.jsp?ip=' . $ip;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($ch);
curl_close($ch);
$data = mb_convert_encoding($data, 'utf-8', 'GB2312'); // 转换编码
// 截取{}中的字符串
$data = substr($data, strlen('({') + strpos($data, '({'), (strlen($data) - strpos($data, '})')) * (-1));
// 将截取的字符串$data中的‘,’替换成‘&’ 将字符串中的‘:‘替换成‘=’
$data = str_replace('"', "", str_replace(":", "=", str_replace(",", "&", $data)));
parse_str($data, $addressInfo); // 将字符串转换成数组格式
//return $addressInfo['addr']; // 返回ip归属地,如:四川省成都市 电信ADS
return $addressInfo['pro']; // 返回ip归属地,如:四川省
//return $addressInfo['city']; // 返回ip归属地,如:成都市
}
The above code comes from the 20220705 network. We need to add the code to the DESTOON custom function php file. The file location is api/extend.func.php
Front-end retrieval method
No matter where you are, first you need to get the label of the native IP and just add it outside the label, such as:
{getIpAddress($ip)}
Notice: in different places$ipThere are different ways of writing, such as: loop is used$t[ip], the company store shows that the registered IP is$COM[regip], the member login IP is$COM[loginip]
Display the results graph (we only display the province here, you can display the city or province + city), pay attention to the annotations of the php function.
