2017-10-01 2 views
0

로그 브라우저, IP, 사용자, 타임 스탬프를 구현하고 싶지만 국가를 추가하고 상태를 원합니다. http://api.ipinfodb.com/에서 일부 매개 변수를 추가하려고했으나 API에서 기본 기능인 json 국가 코드, 국가 및 상태를 추가하기 만하면되므로 매개 변수를 추가하지 않아도됩니다. 600 개 이상의 행이있는 json이 IP 국가, 주 및 지역에 따라 저장해야합니다. 컨트롤러geolocation - 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)

관련 문제