Skip to content

Instantly share code, notes, and snippets.

@iftekharbhuiyan
Last active July 23, 2017 05:01
Show Gist options
  • Save iftekharbhuiyan/f672a2fea277f75fc3da3bf317994af8 to your computer and use it in GitHub Desktop.
Save iftekharbhuiyan/f672a2fea277f75fc3da3bf317994af8 to your computer and use it in GitHub Desktop.
Reading JSON API Data using PHP
<?php
// Desired IP address
$ip = '172.217.10.78';
// With Error Reporting: file_get_content();
// Without Error Reporting: @file_get_content();
$content = @file_get_contents('http://freegeoip.net/json/'.$ip);
// Result as an Array: json_decode($var, true);
// Result as an Object: json_decode($var);
$result = json_decode($content, true);
// For Array: $result['key'];
// For Object: $result->city;
echo $result['city'];
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment