由奇它论坛@老白转载自网络的“WordPress网站显示评论者ip教程-以7B2主题为例”,效果以及实现代码如下,有三种方案供选择,支持IPV6。B2主题美化教程请点我阅读!
1.演示效果
2.实现教程
由奇它论坛@老白转载自权戈网络,好文值得备份,原文:https://www.teelm.com/2022/05/19/24584.html,感谢站长分享
2.1 在线方案一
(免费次数太少,不建议商用)
张戈博客原在线方案的在线 API:淘宝、百度和新浪的 API 接口都相继关停或失效了,总之已经没法用了。。。
在此介绍一个新的接口:IP查询API接口_免费数据接口 - 极速数据 (jisuapi.com)
该接口提供全国数百万IP的归属地、运营商类型查询,定期更新。免费
- 免费会员:500次/天
- 白银会员:1000次/天
- 钻石会员:15万次/日
只需要将代码添加到 WordPress 主题函数模板文件 functions.php 中并保存
function curlOpen($url, $config = array()) { $arr = array('post' => false,'referer' => $url,'cookie' => '', 'useragent' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; customie8)', 'timeout' => 20, 'return' => true, 'proxy' => '', 'userpwd' => '', 'nobody' => false,'header'=>array(),'gzip'=>true,'ssl'=>false,'isupfile'=>false); $arr = array_merge($arr, $config); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, $arr['return']); curl_setopt($ch, CURLOPT_NOBODY, $arr['nobody']); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_USERAGENT, $arr['useragent']); curl_setopt($ch, CURLOPT_REFERER, $arr['referer']); curl_setopt($ch, CURLOPT_TIMEOUT, $arr['timeout']); //curl_setopt($ch, CURLOPT_HEADER, true);//获取header if($arr['gzip']) curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); if($arr['ssl']) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } if(!empty($arr['cookie'])) { curl_setopt($ch, CURLOPT_COOKIEJAR, $arr['cookie']); curl_setopt($ch, CURLOPT_COOKIEFILE, $arr['cookie']); } if(!empty($arr['proxy'])) { //curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); curl_setopt ($ch, CURLOPT_PROXY, $arr['proxy']); if(!empty($arr['userpwd'])) { curl_setopt($ch,CURLOPT_PROXYUSERPWD,$arr['userpwd']); } } //ip比较特殊,用键值表示 if(!empty($arr['header']['ip'])) { array_push($arr['header'],'X-FORWARDED-FOR:'.$arr['header']['ip'],'CLIENT-IP:'.$arr['header']['ip']); unset($arr['header']['ip']); } $arr['header'] = array_filter($arr['header']); if(!empty($arr['header'])) { curl_setopt($ch, CURLOPT_HTTPHEADER, $arr['header']); } if ($arr['post'] != false) { curl_setopt($ch, CURLOPT_POST, true); if(is_array($arr['post']) && $arr['isupfile'] === false) { $post = http_build_query($arr['post']); } else { $post = $arr['post']; } curl_setopt($ch, CURLOPT_POSTFIELDS, $post); } $result = curl_exec($ch); //var_dump(curl_getinfo($ch)); curl_close($ch); return $result; } class ip { public function ipQuery($appkey,$ip) { $url = "https://api.jisuapi.com/ip/location?appkey=$appkey&ip=$ip"; $result = curlOpen($url, ['ssl'=>true]); $jsonarr = json_decode($result, true); //exit(var_dump($jsonarr)); if($jsonarr['status'] != 0) { return $jsonarr['msg']; } $result = $jsonarr['result']; return $result['area'].' '.$result['type']; } } function get_locate($ip) { if(empty($ip)) $ip = get_comment_author_IP(); $appkey = '你的appkey';//你的appkey $query = new ip; $result = $query->ipQuery($appkey,$ip); return $result; }
然后,在极速数据 (jisuapi.com)注册一个账号,申请一下对应接口(免费申请的)。最后在 WordPress 评论模板函数中合适的位置插入如下代码即可:
<?php echo get_locate(get_comment_author_ip()); ?>
B2主题可添加在wp-content\themes\b2\Modules\Common\Comment.php:268行后面
2.2 在线方案二(支持IPV4、IPV6)
利用 ip-api 免费IP地址查询API接口,在function.php添加代码
function get_locate($ip) { if(empty($ip)) $ip = get_comment_author_IP(); $ch = curl_init(); $timeout = 5; curl_setopt ($ch, CURLOPT_URL, 'http://ip-api.com/json/'.$ip.'?lang=zh-CN'); 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['country'] != '中国') { return $result['country']; } else { return $result['country'].' · '.$result['regionName'].' · '.$result['city'].' · '.$result['isp']; } }
2.3 本地方案(QQ纯真数据库)
本地方案则是借助 qq 纯真 ip 数据库来查询 IP 的归属地信息,无需在线获取,从而效率更高。当然,本地数据是不会自己更新的,实时准确性肯定比在线的稍微弱一点,不过我们自己手动更新本地 IP 数据库文件就好了。
文件下载:https://pan.baidu.com/s/1v2Dzg
上传到主题目录之后,请编辑 WordPress 主题目录下的 functions.php 文件,添加如下代码:
require_once get_stylesheet_directory() . '/ip2c/ip2c.php'; //IP 归属地和运营商查询功能
接着参考上文在线方案,找到主题自定义的评论样式回调函数,然后在合适的位置加入如下代码:
<?php echo convertip(get_comment_author_ip()); ?>
IP 数据库文件更新,前往out0fmemory/qqwry.dat: 自动更新的纯真ip库,每天自动更新 (github.com)下载qqwry_lastest.dat文件覆盖目录内的.dat文件即可:https://github.com/out0fmemory/qqwry.dat
就能在前台评论列表对应位置展示评论者的 IP 归属地和运营商信息了
感谢分享