警告:第一个参数必须是对象或现有类的名称

时间:2017-07-13 06:41:23

标签: php

我正在尝试使用一些代码在我的网站上获取用户位置但我面临一些问题,我无法忍受该怎样做,请任何人都可以解决我的问题

我正在尝试

CSV_DATA : process.argv[4].split(":").join(" "),

我正面临错误

  

警告:第一个参数必须是第17行的对象或现有类 public / visitor.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;

如何解决此问题或任何其他来源以获取阻止某些国家/地区的访问者国家/地区代码

提前致谢

3 个答案:

答案 0 :(得分:2)

试试这个:

$ip = $_SERVER['REMOTE_ADDR'];
$url="http://www.geoplugin.net/json.gp?ip=" . $ip;
$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");
$ipdat=json_decode(curl_exec($ch));

if(property_exists($ipdat, 'geoplugin_countryCode')) 
{
    $local_visit = $ipdat->geoplugin_countryCode;
}

答案 1 :(得分:1)

我无法使用以下任何IP地址复制所述问题:

function curl($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "User-Agent: {$_SERVER['HTTP_USER_AGENT']}");
    return curl_exec($ch);
}
$ip   = '61.135.218.30'; /* youdao.com ~ chinese searchengine */
$url  = 'http://www.geoplugin.net/json.gp?ip='.$ip;
$data = curl( $url );

if( !empty( $data ) ){
    $json=json_decode( $data );
    echo ( is_object( $json ) && property_exists( $json,'geoplugin_countryCode') ) ? $json->geoplugin_countryCode : 'Unable to locate property in Object';
    echo '<pre>',print_r( $json,true ),'</pre>';
}

对上述电话的回复是:

CN
stdClass Object
(
    [geoplugin_request] => 61.135.218.30
    [geoplugin_status] => 200
    [geoplugin_credit] => Some of the returned data includes GeoLite data created by MaxMind, available from http://www.maxmind.com.
    [geoplugin_city] => Beijing
    [geoplugin_region] => Beijing
    [geoplugin_areaCode] => 0
    [geoplugin_dmaCode] => 0
    [geoplugin_countryCode] => CN
    [geoplugin_countryName] => China
    [geoplugin_continentCode] => AS
    [geoplugin_latitude] => 39.9289
    [geoplugin_longitude] => 116.3883
    [geoplugin_regionCode] => 22
    [geoplugin_regionName] => Beijing
    [geoplugin_currencyCode] => CNY
    [geoplugin_currencySymbol] => 元
    [geoplugin_currencySymbol_UTF8] => 元
    [geoplugin_currencyConverter] => 6.9112
)

答案 2 :(得分:0)

使用isset()代替:)

if (isset($ipdat->geoplugin_countryCode)) {
    $local_visit = $ipdat->geoplugin_countryCode;
}

您需要在$local_visit条件之前初始化if变量,以避免未定义变量出现问题。