I need to use the JSON generated from the API of this site
I created a PHP class to extract the data as the simplest solution founded here, that's this one:
function ip_details($ip) {
$json = file_get_contents("http://ipinfo.io/{$ip}");
$details = json_decode($json);
return $details;
}
$details = ip_details('8.8.8.8');
echo $details->ip . '<br />';
echo $details->city . '<br />';
echo $details->region . '<br />';
echo $details->country . '<br />';
echo $details->loc . '<br />';
echo $details->hostname . '<br />';
echo $details->org . '<br />';
echo $details->postal . '<br />';
it doesn't work on my server at all. It works only locally. I want to make it clear my host works fine both with the function fsockopen() (even though limited to the 80 and 443 ports) and the cURL library.
In fact, I already use a PHP class that works great with cURL, to extract all the data I need from the JSON file of this website: www.geoplugin.com
Now I tried to create a PHP class on my own but I make some mistakes I can't find. I tried so much......I've been on it for 2 days but still I got no solution at all!.
I've got a file, I called geo.php where I call back and use (try to use, I should say) my class, this way:
require_once 'geo.class.php';
$geo = new Geolocalize('8.8.8.8');
echo $geo->ip . '<br />';
echo $geo->city . '<br />';
echo $geo->region . '<br />';
echo $geo->country . '<br />';
echo $geo->loc . '<br />';
echo $geo->hostname . '<br />';
echo $geo->org . '<br />';
echo $geo->postal . '<br />';
Here's my class. Thank you all in advance.
class Geolocalize {
public $host = 'http://ipinfo.io/{IP}';
public $ip;
public $city;
public $region;
public $country;
public $loc;
public $hostname;
public $org;
public $postal;
public function __construct($ip) {
$host = str_replace('{IP}', $ip, $this->host);
$data = array();
$response = $this->fetch($host);
$data = $response;
//set the vars
$this->ip = $data['ip'];
$this->city = $data['city'];
$this->region = $data['region'];
$this->country= $data['country'];
$this->loc = $data['loc'];
$this->hostname = $data['hostname'];
$this->org = $data['org'];
$this->postal = $data['postal'];
}
public function fetch($host) {
if ( function_exists('curl_init') ) {
// use cURL to fetch data
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
// if deactivated (with 0) you see the json from the API
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close ($ch);
} else if ( ini_get('allow_url_fopen') ) {
//fall back to fopen()
$response = file_get_contents($host, 'r');
}
return $response;
}
}