I'm trying to use the free MaxMind GeoLite2 code to be able to determine the country of a specific IP address.
I am using the Composer-free method that was posted here: Get a localized name of the users city via Maxmind GeoLite2 Free
I'm sure its incredibly simple, but I can't figure out how to actually pass an IP address and have it return the country.
After the $reader = new Reader...
line I have $place = $reader->country('##.###.##.###');
(where the #'s are actual IP address numbers) and it's not working. I tried replacing 'country' with 'city' and that didn't work either. I'm sure its something simple, I'm just not sure what parameters I need to be using to get the country returned.
The error that is shown in the error log is 'PHP Fatal error: Call to undefined method MaxMind\Db\Reader::city() in <<< path to benchmark.php >>>)'
Any ideas/suggestions would be greatly appreciated.
There's no city()
or country()
functions defined in the files you're including (based on the answer you linked to.) Instead you're supposed to use get()
to get the IP geographic information, like so:
require_once __DIR__ . '/' . 'Db/Reader.php';
require_once __DIR__ . '/' . 'Db/Reader/Decoder.php';
require_once __DIR__ . '/' . 'Db/Reader/InvalidDatabaseException.php';
require_once __DIR__ . '/' . 'Db/Reader/Metadata.php';
require_once __DIR__ . '/' . 'Db/Reader/Util.php'; // new 2014/09
use MaxMind\Db\Reader;
$mmdb= 'GeoLite2-Country.mmdb';
$reader = new Reader( __DIR__ . '/' . $mmdb );
$ipData = $reader->get('##.###.##.###');
echo $ipData['country']['names']['en'];
Where you replace ##.###.##.###
with the IP you want to get info for. Obviously this requires you have all the required code files and GeoLite2-Country.mmdb
So the complete steps would be:
Db
folder found in src/MaxMind
to the directory with the file containing the above code.GeoLite2-Country.mmdb
file to the same directory as the file containing the above code.##.###.##.###
with a real IP.