2014-02-18 20:16
浏览 32


Im having dificult having users location by Curl.

i have this code :

$ip ='';
$result ='Unkown' ;
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, ''.$ip);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$ip_data = curl_exec($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

any help will be much apreciated on whats going wrong.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongmeng4742 2014-02-18 20:26

    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.


    $ip_data = curl_exec($ch);
    $ip_obj = json_decode($ip_data);
    echo $ip_obj->countryFullName;
    打赏 评论

相关推荐 更多相似问题