使用php类从JSON检索数据

时间:2014-12-22 11:55:54

标签: php json curl

我需要使用从此site

的API生成的JSON

我创建了一个PHP类来提取数据作为这里建立的最简单的解决方案,就是这个:

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

根本不能在我的服务器上运行。它只在本地工作。我想说清楚我的主机使用函数fsockopen()(尽管仅限于80和443端口)和cURL库都可以正常工作。

事实上,我已经使用了一个非常适合cURL的PHP​​类,从本网站的JSON文件中提取我需要的所有数据:www.geoplugin.com

现在我尝试自己创建一个PHP类,但是我犯了一些我找不到的错误。我尝试了很多......我已经使用了2天但仍然没有解决方案!

我有一个文件,我打电话给geo.php我回调并使用(尝试使用,我应该说)我的班级,这样:

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

这是我的课。提前谢谢大家。

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

}

2 个答案:

答案 0 :(得分:1)

尝试这样的事情:

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

?>

用法

<?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();
}

答案 1 :(得分:0)

我让我的cURL课程工作但只在本地!!!!

我发现了一个错误,现在它正常工作,但不在线,不在我的网站上。 这是:

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

}

然后我就这样使用它,正如我上面所说:

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

正如你所看到的,我甚至尝试用var_dump()来捕获错误,但我只看到生产中的写入NULL,在线。

猜测主机是一个问题,它无法从JSON文件中捕获数据。 我会尝试联系管理员。如果我从他们那里得到一些消息,我会通知你。 真的很令人沮丧!