Alish
Alish

Reputation: 240

Warning: First parameter must either be an object or the name of an existing class

I'm Trying Some Codes for Getting User Location on My site But i have facing some issues i can't under stand what to do please anyone can solve my problem

i'm trying

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;

And I'm facing error

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')) {

how can i solve this issue or anyother source to get visitor country code for blocking some countries

Thanks in advance

Upvotes: 3

Views: 15276

Answers (3)

Professor Abronsius
Professor Abronsius

Reputation: 33813

I'm not able to replicate the stated problem with any of the IP addresses I have tried using the following:

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

The response to the above call is:

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
)

Upvotes: 1

mayank
mayank

Reputation: 462

try this :

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

Upvotes: 3

Tajgeer
Tajgeer

Reputation: 408

Use isset() instead :)

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

You need to init $local_visit variable too before if condition in order to avoid issues with undefined variable.

Upvotes: 4

Related Questions