前言
阿里云 - 《全球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
版权声明
- 本文标题:美好的明天 - 主题评论归属地测试
- 本文地址:https://www.wmviv.com/archives/1088.html
- 转载请保留本文标题、本文地址及链接
- 本站遵循 知识共享《署名—非商业性使用—相同方式共享 4.0 协议国际版》(CC BY-NC-SA 4.0)公共许可协议
- 部分文章来源于网络,仅作为学习展示之用,版权归原作者所有
- 若因文章多次网络流转无法追溯原作者,导致侵犯您的权益,请您 来信告知。
2025-04-22 00:55
评论归属地测试23
2025-04-22 00:03
•来自:南非
评论归属地测试22
2025-04-22 00:03
•来自:阿联酋
评论归属地测试21
2025-04-23 21:05
•来自:云南
无法登录了吗