2017-10-01 13 views
0

ログブラウザ、IP、ユーザー、タイムスタンプを実装したいが、国を追加したいと思う。私は http://api.ipinfodb.com/からいくつかのパラメータを追加しようとしていましたが、それは私のデフォルト機能のjson国コード、国、およびAPIの状態を追加する必要があるためではありません。私は保存する必要がある600以上の行を持つjsonをip国、州および地域に依存する。 コントローラジオロケーション - 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; 
    } 

答えて

0

私は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アドレスを追加してアドレスバーに貼り付けてください。 (例:https://usercountry.com/v1.0/json/46.102.103.10

関連する問題