通过curl获取用户位置

Im having dificult having users location by Curl.

i have this code :

$ip ='123.125.114.144';
$result ='Unkown' ;
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://www.iptolatlng.com/?ip='.$ip);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$ip_data = curl_exec($ch);
curl_close($ch);

if($ip_data && $ip_data->countryFullName != null)  //---the error is in this line
{
    $result = $ip_data->countryFullName;
}

echo $result;

However im getting this error:

Notice: Trying to get property of non-object in /mywebsite/index.php on line 29 Unknown

for testing purpose this is their link with random ip http://www.iptolatlng.com/?ip=123.125.114.144

any help will be much apreciated on whats going wrong.

curl_exec returns the result of the request in type of string, while you are trying access the result value in type of object. Because your request returns json string, you should use the function json_decode.

Example:

$ip_data = curl_exec($ch);
$ip_obj = json_decode($ip_data);
echo $ip_obj->countryFullName;