stevebauman / location

Detect a users location by their IP Address.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Some IPs have no timezone data, this causes Location::get() to return false

kajgiesbersflavour opened this issue · comments

Hi Everyone,

I was having an issue where Location::get() was returning false. I am using a local maxmind-city database.
I added a log in the MaxMind driver to see what is going on:
image

Now it was logging the following error:
local.DEBUG: RuntimeException: Unknown attribute: timezone in {project_root}\vendor\geoip2\geoip2\src\Record\AbstractRecord.php:43

I double checked the csv for the city data and found out that indeed for my ip & more the timezone data was missing.

I managed to fix the error by adding an isset to this line:

 protected function process($ip)
    {
        try {
            $record = $this->fetchLocation($ip);

            return new Fluent([
                'country' => $record->country->name,
                'country_code' => $record->country->isoCode,
                'city' => $record->city->name,
                'regionCode' => $record->mostSpecificSubdivision->isoCode,
                'regionName' => $record->mostSpecificSubdivision->name,
                'postal' => $record->postal->code,
                'timezone' => isset($record->location->timezone) ? $record->location->timezone : '',// - Changed this line 
                'latitude' => (string) $record->location->latitude,
                'longitude' => (string) $record->location->longitude,
                'metro_code' => (string) $record->location->metroCode,
            ]);
        } catch (Exception $e) {
            Log::debug($e);
            return false;
        }
    }

Shall I make a pull request for this or do you think there is a better way to solve this issue?

Hi there @kajgiesbersflavour, thanks for the report.

I've just patched this, can update to the latest release and confirm if you still receive the same exception?

Haha, so the issue was a typo there, not the lack of data.
Thank you for solving the issue so quickly! It works properly now 😄