美好的明天

分享改变,记录生活
美好的明天
当前位置: 首页 > WordPress > 正文

前言

阿里云 - 《全球IP归属地查询IP地址查询IPv6地址查询IP地理位置库查询IP离线库查询私有化部署离线查询》

同时支持 IPv4 和 IPv6 地址归属地查询,价钱还可以(30块钱查询100万次);

目前本博客评论区归属地显示基于此API实现。

要是有更便宜好用的可以偷偷告诉我哦 😄

代码片段

请求示例


<?php
    $host = "https://c2ba.api.huachen.cn";
    $path = "/ip";
    $method = "GET";
    $appcode = "你自己的AppCode";
    $headers = array();
    array_push($headers, "Authorization:APPCODE " . $appcode);
    $querys = "ip=58.242.2.2";
    $bodys = "";
    $url = $host . $path . "?" . $querys;

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_FAILONERROR, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, true);
    if (1 == strpos("$".$host, "https://"))
    {
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    }
    var_dump(curl_exec($curl));
?>

调用结果


{
  "ret": 200,
  "msg": "success",
  "data": {
    "ip": "58.242.2.2",
    "long_ip": "988938754",
    "isp": "中国联通",
    "area": "华东",
    "region_id": "340000",
    "region": "安徽",
    "city_id": "340100",
    "city": "合肥",
    "district": "蜀山区",
    "district_id": "340104",
    "country_id": "CN",
    "country": "中国",
    "lat": "31.851158",
    "lng": "117.260536"
  },
  "log_id": "3f4a10b492a94526929d121065a6ceff"
}

WordPress 后台评论批量更新归属地


/*-----------------------------------------------------------------------------------*/
// 后台评论列表自定义列(归属地显示)
/*-----------------------------------------------------------------------------------*/
function custom_comment_column($columns) {
    $columns['location_info'] = '归属地';
    return $columns;
}
add_filter('manage_edit-comments_columns', 'custom_comment_column');

function custom_comment_column_data($column_name, $comment_id) {
    if ($column_name === 'location_info') {
        $country = get_comment_meta($comment_id, 'country', true) ?: '';
        $region = get_comment_meta($comment_id, 'region', true) ?: '';
        $city = get_comment_meta($comment_id, 'city', true) ?: '';
        
        echo esc_html("$country $region $city $isp");
    }
}
add_action('manage_comments_custom_column', 'custom_comment_column_data', 10, 2);

/*-----------------------------------------------------------------------------------*/
// 后台批量更新归属地功能
/*-----------------------------------------------------------------------------------*/
function add_bulk_ip_update_action($actions) {
    $actions['update_ip_location'] = '更新归属地';
    return $actions;
}
add_filter('bulk_actions-edit-comments', 'add_bulk_ip_update_action');

function process_bulk_ip_update($redirect_to, $doaction, $comment_ids) {
    if ($doaction === 'update_ip_location') {
        $updated_count = 0;
        foreach ($comment_ids as $comment_id) {
            $comment = get_comment($comment_id);
            if ($comment && $comment->comment_author_IP) {
                $result = get_ip_province($comment->comment_author_IP, $comment_id);
                if ($result) $updated_count++;
            }
        }
        $redirect_to = add_query_arg(array(
            'ip_updated' => $updated_count,
            'updated' => true
        ), $redirect_to);
    }
    return $redirect_to;
}
add_filter('handle_bulk_actions-edit-comments', 'process_bulk_ip_update', 10, 3);

// 后台通知提示
function display_ip_update_notice() {
    if (isset($_GET['ip_updated'])) {
        $count = intval($_GET['ip_updated']);
        echo '<div class="updated notice is-dismissible"><p>' . sprintf(__('成功更新 %d 条评论的归属地信息'), $count) . '</p></div>';
    }
}
add_action('admin_notices', 'display_ip_update_notice');

思路

country数据是中国则显示region省份;
country数据是中国以外的则显示country国家;
有的时候开代理,评论后没有获取到IP,则不显示归属地;

本文评论区可查看效果。

THE END

目前有 24 条评论

  1. 棕耳兔
    23楼
    棕耳兔

    评论归属地测试23

  2. 棕耳兔
    22楼
    棕耳兔

    评论归属地测试22

  3. 棕耳兔
    21楼
    棕耳兔

    评论归属地测试21

    • CF673X
      CF673X

      无法登录了吗

发表评论

gravatar

OK aixin aoman baoquan bizui cahan caidao ciya dabing doge fadai ganga guzhang haixiu hanxiao huaixiao jie jingkong keai koubi ku leiben lenghan liuhan nanguo penxue piezui qiang qinqin quantou se shengli shuai tiaopi touxiao tuosai weiqu woshou wozuimei wunai xiaojiujie xiaoku xieyanxiao xigua yinxian yiwen youling yun