地理位置 - 按IP地址识别国家/地区

时间:2017-10-01 23:03:20

标签: php json api codeigniter

我想实现日志浏览器,ip,用户,时间戳,但我想添加国家/地区和州。我试图添加一些参数 http://api.ipinfodb.com/,但它没有,因为我只需要从api添加到我的默认函数json国家代码,国家和州,但我无法添加它。我有一个需要存储600多行的json取决于ip国家,州和地区。 控制器

public function datatable(){
        $array = $this->sessions->datatable();
        $this->json($array);
        $data = array();
        foreach ($array as $rows){
            $user_agent = $this->getBrowser($rows['user_agent']);
            array_push($data, array(
                $user_agent['name'].' on '.$user_agent['platform'],
                $rows['ip_address'],
                date('m/d/Y H:i:s',$rows['timestamp']),
            ));
        }
        $this->json(array('data' => $data));
    }

    public function tests()
    {
        $ip = '200.92.39.106';
        $this->load->config('geolocation', true);
        $config = $this->config->config['geolocation'];
        $this->geolocation->initialize($config);
        $this->geolocation->set_ip_address($ip);

        $city = $this->geolocation->get_city();
        if($city === FALSE)
            echo $this->geolocation->get_error();
        else
            echo $city;
    }

1 个答案:

答案 0 :(得分:0)

我更喜欢https://usercountry.com/v1.0/json/。以下是一个例子:

$nb_cell = ...; // retrieve the number of cell to merge horizontaly
$TBS->MergeField('my_field', $nb_cell, false, array(
  'att'=>'table:table-cell#table:number-columns-spanned'
 ));

现在,您可以插入(或更新)数据:

$chs = curl_init('https://usercountry.com/v1.0/json/'.$this->input->ip_address());
curl_setopt($chs, CURLOPT_FAILONERROR, TRUE);
curl_setopt($chs, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($chs, CURLOPT_SSL_VERIFYPEER, 0);

$geolocation = curl_exec($chs);
$geolocation = json_decode($geolocation, true);

$state = $geolocation['region']['state'];
$country = $geolocation['country']['name'];
$country_code = $geolocation['country']['alpha-2']; //if you want 3 letters like USA, try alpha-3

这就是全部!如果您想查看https://usercountry.com/v1.0/json/的示例输出,请添加您的(或其他)IP地址并将其粘贴到您的地址栏。 (例如:https://usercountry.com/v1.0/json/46.102.103.10