/**
* @name: 获取ip归属地
* @param {str} $ip IP地址
* @author: Turbo
* @Date: 2022-05-20 09:15:44
*/
public function getIpAddress($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; // 返回ip归属地
}