/**
* WordPress 显示评论者IP归属地PHP函数
* https://zhang.ge/5060.html
**/
function get_locate($ip) {
    if(empty($ip)) $ip = get_comment_author_IP();
    $ch = curl_init();  
    $timeout = 5;  
    curl_setopt ($ch, CURLOPT_URL, 'http://ip.taobao.com/service/getIpInfo.php?ip='.$ip);  
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  
    $file_contents = curl_exec($ch);  
    curl_close($ch);  
    $result = json_decode($file_contents,true);
    if ($result['data']['country'] != '中国') {
        return $result['data']['country'];
    } else {
        return $result['data']['country'].' · '.$result['data']['region'].' · '.$result['data']['city'].' · '.$result['data']['isp'];
    }
}


<?php get_locate(get_comment_author_ip());?>
Last modification:March 31st, 2019 at 11:57 am
If you think my article is useful to you, please feel free to appreciate