【问题标题】:How can get current country name and state in WordPress function如何在 WordPress 功能中获取当前国家名称和状态
【发布时间】:2018-05-18 07:28:15
【问题描述】:

我想使用geoplugin。但当前的地理插件在json 中显示数据。但是如何在WordPress function 中使用这个json 代码 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
}

【问题讨论】:

  • 是什么让您无法运行 json_decode 将 JSON 数组解析为 PHP 数组?
  • 我知道json_decode 但如何在 WordPress 中使用以及如何创建 WordPress 功能。请解释一下
  • 您到底在寻找什么? Wordpress 是用 PHP 编写的,你可以直接使用任何 PHP 方法
  • 试试这个链接geoplugin.com/webservices/php
  • 我想在我的网站上获取当前用户位置

标签: php json wordpress


【解决方案1】:

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

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;
}

【讨论】:

  • 是的,我需要这个代码。它根据我的需要工作。谢谢@asif。
【解决方案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();

【讨论】:

  • 感谢您的回复@George 请检查我的代码不起作用 ---------- function geo_location(){ $result = file_get_contents('geoplugin.net/json.gp'); $resultArr = json_decode($result);回声“国家=”。 $resultArr['geoplugin_countryName']; } geo_location();
【解决方案3】:

试试这个也

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

它会给你一个数组

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2016-06-11
    相关资源
    最近更新 更多