簡體   English   中英

地理位置-通過IP地址識別國家

[英]geolocation - Identifying country by IP address

我想實現日志瀏覽器,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;
    }

我更喜歡https://usercountry.com/v1.0/json/ 下面是一個示例:

$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

現在,您可以插入(或更新)數據:

$data = [
  'state' => $state,
  'country' => $country,
  'country_code' => $country_code,
  'ip_address' => $this->input->ip_address(),
  'timestamp' => time(),
  'user_id' => $your_user_id_variable
];
$this->db->insert('your_table_name', $data);

就這樣! 如果要查看https://usercountry.com/v1.0/json/的示例輸出,請添加您的IP地址(或其他IP地址)並將其粘貼到地址欄中。 (例如: https : //usercountry.com/v1.0/json/46.102.103.10

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM