我正在尝试一些代码以在我的网站上获取用户位置但我遇到了一些我无法理解该怎么做的问题,请任何人都可以解决我的问题
我在努力
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$uaa = $_SERVER['HTTP_USER_AGENT'];
curl_setopt($ch, CURLOPT_USERAGENT, "User-Agent: $uaa");
return curl_exec($ch);
}
$ip = $_SERVER['REMOTE_ADDR'];
$ipdat = @json_decode(curl("http://www.geoplugin.net/json.gp?ip=" . $ip));
//get ISO2 country code
if(property_exists($ipdat, 'geoplugin_countryCode')) {
$local_visit = $ipdat->geoplugin_countryCode;
}
echo $local_visit;
我遇到了错误
Warning: First parameter must either be an object or the name of an existing class public/visitor.php on line 17
line 17: if(property_exists($ipdat, 'geoplugin_countryCode')) {
我如何解决这个问题或任何其他来源以获取访问者国家代码以阻止某些国家
提前致谢
最佳答案
改用 isset()
:)
if (isset($ipdat->geoplugin_countryCode)) {
$local_visit = $ipdat->geoplugin_countryCode;
}
您还需要在 if
条件之前初始化 $local_visit
变量以避免 undefined variable 的问题。
关于php - 警告 : First parameter must either be an object or the name of an existing class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45073153/