Php 如何在WordPress函数中获取当前国家名称和状态
我想用geoplugin。但是当前的地理插件用json显示数据。但是如何在WordPress函数中使用这个json代码呢 你可以这样用Php 如何在WordPress函数中获取当前国家名称和状态,php,json,wordpress,Php,Json,Wordpress,我想用geoplugin。但是当前的地理插件用json显示数据。但是如何在WordPress函数中使用这个json代码呢 你可以这样用 function geo_location(){ $result = file_get_contents('http://www.geoplugin.net/json.gp'); $resultArr = json_decode($result); echo 'Country ='. $resultArr->geoplugin_coun
function geo_location(){
$result = file_get_contents('http://www.geoplugin.net/json.gp');
$resultArr = json_decode($result);
echo 'Country ='. $resultArr->geoplugin_countryName;
}
geo_location();
你可以这样用
function geo_location(){
$result = file_get_contents('http://www.geoplugin.net/json.gp');
$resultArr = json_decode($result);
echo 'Country ='. $resultArr->geoplugin_countryName;
}
geo_location();
也试试这个
echo var_export(unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR'])));
它会给你一个数组试试这个
echo var_export(unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR'])));
它会给你一个数组试试这个。希望这对你有帮助
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;
}
试试这个。希望这对你有帮助
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;
}
是什么阻止你将JSON数组解析成PHP数组?我知道JSON_解码,但如何在WordPress中使用,如何创建WordPress函数。请告诉我你到底在找什么?Wordpress是用PHP编写的,您可以直接使用任何PHP方法尝试此链接我想获取我网站上的当前用户位置是什么阻止您运行将JSON数组解析为PHP数组?我知道JSON_解码,但如何在Wordpress中使用以及如何创建Wordpress函数。请告诉我你到底在找什么?Wordpress是用PHP编写的,您可以直接使用任何PHP方法尝试此链接我想在我的网站上获取当前用户位置感谢您的回复@George请检查我的代码不工作------函数geo_location{$result=file_get_contents';$resultar=json_decode$result;echo'Country=.$resultar['geoplugin\u countryName']}地理位置;感谢您回复@George请检查我的代码不起作用------函数geo_location{$result=file_get_contents';$resultar=json_decode$result;echo'Country=。$resultar['geoplugin_countryName'];}geo_location;谢谢@Techno designer谢谢@Techno designers,我需要这个代码。它正按我的需要工作。谢谢@asif。是的,我需要这个代码。它正按我的需要工作。谢谢@asif。