使用wordpress插件GeoIP检测从IP获取国家


Fetching country from IP using wordpress plugin GeoIP detection?

我已经安装了这个插件:https://tah.wordpress.org/plugins/geoip-detect/

当我在插件内测试"查找"时,插件似乎工作得很好,它返回我的地理信息。然而,当我尝试在我的一个wordpress页面内实现代码时,它不起作用。

$ip = $_SERVER['REMOTE_ADDR'];
$userInfo = geoip_detect2_get_info_from_current_ip($ip);
echo $userInfo->country->name;

我从本地woocommerce页面调用该函数,其中显示单个产品。但是这个函数什么也没有返回。我是否必须包含更多的东西来调用函数geoip_detect2_get_info_from_current_ip()?

我也试过:

<?php echo do_shortcode("[geoip_detect2 property='country']"); ?>

它也不返回任何东西。我在godaddy的代码编辑工具中进行编辑,所以我可能会错过错误。

我尝试在我的wordpress网站http://iradiofind.com/stations/通过IP实现GEO。我使用http://www.geoplugin.net来获取国家信息。为了获得ip地址,我使用这个函数

function get_client_ip() {
     $ipaddress = '';
     if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
     else if(getenv('HTTP_X_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
     else if(getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
     else if(getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
     else if(getenv('HTTP_FORWARDED'))
        $ipaddress = getenv('HTTP_FORWARDED');
     else if(getenv('REMOTE_ADDR'))
        $ipaddress = getenv('REMOTE_ADDR');
     else
        $ipaddress = 'UNKNOWN';
     return $ipaddress;
}
function ip_details($url) {
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
   $data = curl_exec($ch);
   curl_close($ch);
   return $data;
}

这是在我的页面模板。

<?php 
    $myipd = get_client_ip(); 
    $url = 'http://www.geoplugin.net/json.gp?ip='.$myipd; 
    $details    =   ip_details($url); 
    $v = json_decode($details);
    $mycountry = $v->geoplugin_countryName;
?>

我最近使用了相同的插件自定义解决方案-我注意到插件有一个内置的IP检测功能- geoip_detect2_get_client_ip() -尝试使用它代替?

从你的代码编辑:

$ip = geoip_detect2_get_client_ip();
$userInfo = geoip_detect2_get_info_from_current_ip($ip);
echo $userInfo->country->name;

这个函数也内置了缓存功能,在我的测试中,它看起来非常快。

可能geoip-detect函数超出了您的作用域(这将是非常奇怪的)。将此添加到函数中。php:

require_once ABSPATH . '/wp-content/plugins/geoip-detect/geoip-detect.php';