0

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;
    }

}
1
  • did you get any error at all on the server? the fact that it works on your local machine makes me think that's an issue with the environment rather than anything else. Commented Dec 22, 2014 at 11:58

2 Answers 2

1

Try something like this:

<?php

class IpInfo
{
    protected $apiUrl = 'http://ipinfo.io';

    private $ip;
    private $city;
    private $region;
    private $country;
    private $loc;
    private $hostname;
    private $org;
    private $postal;

    public function __construct($user_ip)
    {
        // Check if required function exists
        if (!function_exists('file_get_contents'))
            throw new Exception('file_get_contents php function does not exists');

        // Load ip data from remote server
        $ip_data = @file_get_contents($this->apiUrl . '/'. $user_ip);
        if (empty($ip_data))
            throw new Exception('failed to load ip info from remote server');

        // Parse ip data
        $ip_data = @json_decode($ip_data);
        if (!$ip_data)
            throw new Exception('failed to parse ip data from remote server response');
        else {
            $this->ip = $ip_data->ip;
            $this->city = $ip_data->city;
            $this->region = $ip_data->region;
            $this->country = $ip_data->country;
            $this->loc = $ip_data->loc;
            $this->hostname = $ip_data->hostname;
            $this->org = $ip_data->org;
        }
    }

    public function ip() { return $this->ip; }
    public function city() { return $this->city; }
    public function region() { return $this->region; }
    public function country() { return $this->country; }
    public function loc() { return $this->loc; }
    public function hostname() { return $this->hostname; }
    public function org() { return $this->org; }
}

?>

Usage

<?php

$user_ip = '74.125.71.138'; // or $_SERVER['REMOTE_ADDR'];

try
{
    $ip_info = new IpInfo($user_ip);

    echo $ip_info->city(); // Mountain View
}
catch (Exception $ex)
{
    echo $ex->getMessage();
}
Sign up to request clarification or add additional context in comments.

1 Comment

I tried your class but locally with Xampp (in Development) it works great. The problem is when it's online on my website: the answer I got is: failed to parse ip data from remote server response. I really don't know why. It seems the class can't retrieve the date from the JSON file using the function file_get_contents(). I should use the cURL library as I already do with another PHP class I found on www.geoplugin.com for a similar service. Do you have a suggestion to make my cURL class work?
0

I make my cURL class work but only locally!!!!

I noticed a mistake and now it's working ok, but not online, not in my website. Here it is:

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);

        $response = $this->fetch($host);

        // decode the JSON into an associative array, setting the option true
        $data = json_decode($response, true);

        //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;
    }

}

then I use it this way, as I said above:

require_once 'geo.class.php';

$geo = new Geolocalize('74.125.71.138'); // 74.125.71.138

echo var_dump($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 />';

As you can see I even try to catch the errors with var_dump() but I only see the writing NULL in Production, online.

Guess it's a problem with the host, which is not able to catch data from the JSON file. I'll try to contact the admins. I'll let you know if I get some news from them. It's really frustrating!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.