如何在WordPress函数中获取当前的国家名称和状态

时间:2018-05-18 07:28:16

标签: php json wordpress

我想使用geoplugin。但是当前的Geo插件显示json中的数据。但是如何在json中使用此WordPress function代码 hear example json

{
  "geoplugin_request": "122.180.85.165",
  "geoplugin_status": 200,
  "geoplugin_delay": "2ms",
  "geoplugin_credit": "Some of the returned data includes GeoLite data created by MaxMind, available from <a href='http://www.maxmind.com'>http://www.maxmind.com</a>.",
  "geoplugin_city": "Ludhiana",
  "geoplugin_region": "Punjab",
  "geoplugin_regionCode": "PB",
  "geoplugin_regionName": "Punjab",
  "geoplugin_areaCode": "",
  "geoplugin_dmaCode": "",
  "geoplugin_countryCode": "IN",
  "geoplugin_countryName": "India",
  "geoplugin_inEU": 0,
  "geoplugin_continentCode": "AS",
  "geoplugin_continentName": "Asia",
  "geoplugin_latitude": "30.9",
  "geoplugin_longitude": "75.85",
  "geoplugin_locationAccuracyRadius": "100",
  "geoplugin_timezone": "Asia/Kolkata",
  "geoplugin_currencyCode": "INR",
  "geoplugin_currencySymbol": "&#8360;",
  "geoplugin_currencySymbol_UTF8": "₨",
  "geoplugin_currencyConverter": 67.9875
}

3 个答案:

答案 0 :(得分:2)

你可以像这样使用

function geo_location(){ 
  $result = file_get_contents('http://www.geoplugin.net/json.gp'); 
  $resultArr = json_decode($result); 
  echo 'Country ='. $resultArr->geoplugin_countryName; 
} 

geo_location();

答案 1 :(得分:2)

试试这个。希望这会对你有所帮助!!

function getusercountrycode()
{
$ip = $_SERVER['REMOTE_ADDR'];
$ch = curl_init();
$curlConfig = array(
    CURLOPT_URL            => "http://www.geoplugin.net/json.gp? 
ip=".$ip,
    CURLOPT_POST           => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS     => false
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
$json_a=json_decode($result,true);
$countrycode = $json_a['geoplugin_countryCode'];
return $countrycode;
}

答案 2 :(得分:1)

尝试这也是

 echo var_export(unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR'])));

它会给你一个数组