php - 警告 : First parameter must either be an object or the name of an existing class

标签 php

我正在尝试一些代码以在我的网站上获取用户位置但我遇到了一些我无法理解该怎么做的问题,请任何人都可以解决我的问题

我在努力

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/

相关文章:

php - 从复选框插入许多值,只有最后一个值插入到数据库中

php - PHP 中的双引号和单引号有什么区别?

php - 透明背景颜色不适用于移动设备

javascript - 使用 JQuery、AJAX 和 PHP 提交表单

php - mysql中将行转换为列

php - 我可以在 php 中编写一个带有多个可选参数但至少必须使用一个参数的函数吗?

php - 如何从 .phpcs.xml 中的 PHP Code Sniffer 检查中排除某些目录以外的所有内容?

php - 代码点火器不显示我的数据(但没有显示错误)

php - XMLHttpRequest 将变量传递给 php 脚本

php - 将图片/视频发布到 Twitter