php 根据ip获取城市

180it 2021-02-28 PM 1060℃ 0条
/**
 * 根据ip获取城市
 * @param type $ip
 * @return type
 */
function get_ip_city($ip=""){
    if(!$ip) $ip = util::get_client_ip(); 
    $rs = get_json_data(send("http://api.114la.com/ip/","ip={$ip}&format=json","GET"));   
    if($rs && $rs['error_code']===0){
        return $rs['data']['city'];
    }
    return;
}

//解析json
function get_json_data($strJson) {

$strJson = trim($strJson);
$pos = strpos($strJson, '{');
if ($pos !== false) {
    $strJson = json_decode($strJson);
    $strJson = get_object_vars_final($strJson);
    return $strJson;
} else {
    return '';
}

}

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

php 根据ip获取城市