/** * onIpProcess * @return boolean | Geocoder\Model\AddressCollection */ protected function onIpProcess() { $ip = $this->getIp(); try { // Maxmind GeoIP2 Provider: e.g. the database reader $reader = new \GeoIp2\Database\Reader(ROOT . DS . APP_DIR . DS . 'Plugin' . DS . 'GeoLite2' . DS . 'GeoLite2-City.mmdb'); $adapter = new \Geocoder\Adapter\GeoIP2Adapter($reader); $geocoder = new \Geocoder\Provider\GeoIP2($adapter); $result = $geocoder->geocode($ip); if ($result) { return $result; } } catch (Exception $e) { return false; } return false; }
function geoIP($ip, $object = false) { $reader = new \GeoIp2\Database\Reader(APPLICATION_PATH . DS . 'geoip' . DS . 'GeoLite2-City.mmdb'); $adapter = new \Geocoder\Adapter\GeoIP2Adapter($reader); $geocoder = new \Geocoder\Provider\GeoIP2($adapter); $address = $geocoder->geocode($ip); $obj = Arrays::first($address); $cont = []; $country = $obj->getCountry(); $cont['country']['name'] = $country->getName(); $cont['country']['code'] = $country->getCode(); $region = $obj->getRegion(); $cont['region']['name'] = $region->getName(); $cont['region']['code'] = $region->getCode(); $coordinates = $obj->getCoordinates(); $cont['coordinates']['latitude'] = $coordinates->getLatitude(); $cont['coordinates']['longitude'] = $coordinates->getLongitude(); $cont['streetNumber'] = $obj->getStreetNumber(); $cont['streetName'] = $obj->getStreetName(); $cont['zip'] = $obj->getPostalCode(); $cont['city'] = $obj->getLocality(); if ($object) { return with(new Container())->populate($cont); } return $cont; }