Call reference for country in GeoLite2 using PHP

余生长醉 提交于 2020-06-08 06:24:42

问题


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.


回答1:


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:

  1. Download the MaxMind-DB-Reader-php from https://github.com/maxmind/MaxMind-DB-Reader-php
  2. Copy the Db folder found in src/MaxMind to the directory with the file containing the above code.
  3. Download the GeoLite2 Country MaxMind DB from http://dev.maxmind.com/geoip/geoip2/geolite2/
  4. Unzip the downloaded gzip and copy the GeoLite2-Country.mmdb file to the same directory as the file containing the above code.
  5. You should now be able to run the above code! Just make sure you replace ##.###.##.### with a real IP.



回答2:


This is a simple way. First you have to insert the user IP in MySQL. Then you have to run fetch query, something like this

//database connect or includ database php file

//user_ip detect

$geo = json_decode(file_get_contents("http://extreme-ip-lookup.com/json/$user_ip")); 
$country = $geo->country; 
$city = $geo->city; 
$ipType = $geo->ipType; 
$businessName = $geo->businessName; 
$businessWebsite = $geo->businessWebsite; 
echo "Location of $user_ip: $city, $country\n"; 
echo $ip_address;


来源:https://stackoverflow.com/questions/26198262/call-reference-for-country-in-geolite2-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!