2

I'm trying to process an array ip from data posted from a input post. To convert this to latitude longitude I am using the following code,

this my ci library:

<?php defined('BASEPATH') OR exit('No direct script access allowed');
  include APPPATH . 'libraries/GeoIP/src/geoipcity.inc';

    class GeoIp{

        public function info($ip){
            $gi = geoip_open(APPPATH . 'libraries/GeoIp/data/GeoLiteCity.dat', GEOIP_STANDARD);
            $record = geoip_record_by_addr($gi, $ip);
            geoip_close($gi);
            return $record;
        }

        public function get($ip){
            $gi = geoip_open(APPPATH . 'libraries/GeoIp/data/GeoLiteCity.dat', GEOIP_STANDARD);
            $records = array();
            $ips = is_array($ip);
            foreach ($ips as $key=>$value) {
                $records[] = geoip_record_by_addr($gi, $value);
            }

            foreach ($records as $record) {
            $record_string = $record->longitude . "," . $record->latitude;
            geoip_close($gi);
            return $record_string;
            }
        }

    }

and this my controller:

public function iplat(){
    $this->load->library('geoip');
    $record = $this->geoip->get($this->input->post('ip'));

    echo json_encode($record);


}

This code always returns null

AAT
  • 411
  • 7
  • 16
gofur triad
  • 83
  • 2
  • 6
  • 2 problem seems to me :- 1. `$gi = geoip_open(APPPATH . 'libraries/GeoIp/data/GeoLiteCity.dat', GEOIP_STANDARD);` i think path issue is there.2. ` $this->load->library('geoip');` you need to cehck that it is working fine or not. check your error log files for that.(I hope codeignitor produces error log files somewhere) – Alive to die - Anant Aug 08 '17 at 04:38

1 Answers1

1

One problem I see right away is your code

        $ips = is_array($ip);
        foreach ($ips as $key=>$value) {
            $records[] = geoip_record_by_addr($gi, $value);
        }

Beacuse you have assigned is_array($ip) to $ips, $ips is now a boolean and not and array. So you cannot run a loop on it. Perhaps you meant to run the loop of $ip like this:

if(is_array($id))
{
    foreach ($ip as $key=>$value)
    {
        $records[] = geoip_record_by_addr($gi, $value);
    }
}
Amit Joshi
  • 1,334
  • 1
  • 8
  • 10