Error "trying to get Property of non-object" when using ipinfo.io

Asked

Viewed 642 times

1

I’m relatively new with PHP and I started going through a site. And I found the following code that was going wrong:

<?php        
 function ip_details() {
  $json = file_get_contents("http://ipinfo.io/");
  $details = json_decode($json);
  return $details;
 }
 $details = ip_details();
 $ip = $details->ip;//34    
 $local = $details->city;  //35
?>

This is the mistake that comes to me:

PHP error was encountered Severilty: Notice Message: trying to get Property of non-object Filename: views/login_view.php Line Number: 34

I know something about PHP but I didn’t understand what was here and I’ve been searching but I couldn’t find anything to make me understand what that code was.

Does anyone understand this code that can explain to me?

  • I removed the "Good" from your question, avoid using it.

  • I did the test here and ran smoothly, see if the server you are running on has file_get_contents support for external addresses;

  • You can also var_dump($Details); to see what content is in the variable

  • 1

    Tried to put it like this: $json = file_get_contents("http://ipinfo.io/json");?

  • 1

    Instead of posting questions like "what this does", try to focus on the problem. In this case the problem would be with the data brought by file_get_contents that were not decoded by json_decode. And do not use images to illustrate an error that is a text message "Trying to get Property of non-object"

  • Another thing, it seems to me that the software is using MVC, I disagree to do executions and treatments in the View layer. I recommend studying MVC first and using PHP documentation.

Show 1 more comment

1 answer

2


@Bruno Gibellino,

this script takes details of ip,city,region,country, location that can be used to find you on google maps for example.

Take a test, put a echo before the ip and also in the $details->city, will show your ip and where you live.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.