ip-location-db icon indicating copy to clipboard operation
ip-location-db copied to clipboard

Any php library to use these DBs ?

Open kirkcc opened this issue 2 years ago • 5 comments

Hi,

Is there any php library can use on these IP db ?

Thanks

kirkcc avatar May 29 '22 11:05 kirkcc

Sorry, I don't know any PHP library using our databases. If someone knows it, please let us know.

sapics avatar May 30 '22 13:05 sapics

I realize I am rather late to this conversation, but for PHP I would probably suggest first building these databases into .mmdb archives and then using either MaxMind-DB-Reader-php or GeoIP2-php to read from it.

.mmdb is a binary format that MaxMind created for their databases and is more efficient than using something like a CSV as it's purpose-built (25MB of CSVs from this repo compiles down to 7.3MB).

If compiling them is an issue for you, I've put together a repo which will once-per-day pull in the CC0 and PDDL databases from this project, compile various DBs, and then publish them. (It's brand new, so let me know if you see any issues).

https://github.com/HostByBelle/IP-Geolocation-DB

Also using the .mmdb format and the related packages means that if you ever want to upgrade to a paid database, you'd hardly have to do any work since AFAIK most if not all providers will include their databases in that format due to it's popularity.

BelleNottelling avatar Dec 05 '23 08:12 BelleNottelling

I'm sorry, but that's just not the result I get. Not using your code at the very least. My testing shows that the Maximid solution is significantly faster and much more efficient than the function you shared in your earlier comment.

MaxMind-DB-Reader-php (Configured poorly)

This was tested with it configured poorly by making a new instance of the reader for each lookup:

Benchmark results:
- Number of lookups: 10
- Total elapsed time: 4.1215 milliseconds
- Average lookup time: 0.41171 milliseconds per lookup
- Peak memory usage: 599440
- Peak memory usage (real): 2097152

MaxMind-DB-Reader-php (Configured correctly)

This was tested with it configured correctly by making an once before performing all lookups:

Benchmark results:
- Number of lookups: 10
- Total elapsed time: 0.2716 milliseconds
- Average lookup time: 0.02688 milliseconds per lookup
- Peak memory usage: 599344
- Peak memory usage (real): 2097152

ipinfo.php (your script)

I actually removed an excess var_dump function that was leftover in the function before this test:

Benchmark results:
- Number of lookups: 10
- Total elapsed time: 214.187 milliseconds
- Average lookup time: 21.41728 milliseconds per lookup
- Peak memory usage: 33214712
- Peak memory usage (real): 41943040

So used correctly the maxmind package took 0.13% of the time and used 5% of the RAM yours did. When the maxmind package was used in a less efficient way, it still only took 1.92% of the time while still only using 5% of RAM (measuring RAM though PHP is pretty imperfect though, so take that number with a grain of salt).

Trying to lookup 100 addresses with the maxmind package took 2.514 milliseconds and your script took 2090.8009 milliseconds.

These were all tested using the same database, too. That being geo-whois-asn-country-ipv4.csv where the only difference is the maxmind package was tested with that database compiled to their format.

You are more than welcome to use whatever method you prefer, but personally.. I'm sticking to the purpose-built tools and packages that are designed, maintained, and built for an enterprise product 🤷‍♀️

BelleNottelling avatar Dec 05 '23 11:12 BelleNottelling

@2551654928 @BelleNottelling

Thank you for your creation!

It is a very smart idea to make a well used library available by using .mmdb file which developed recently as safing/mmdbmeld! I guess that multiple programing languages are supported by maxmind in .mmdb format. I would like to write down your idea in README and make a link to your repository.

sapics avatar Dec 07 '23 03:12 sapics

I guess that multiple programing languages are supported by maxmind in .mmdb format.

Hi, yes! MaxMind themselves maintain readers for tons of different languages (PHP, JS, NodeJS, Python, Java, and a few others). All of these readers are open source as well and the the database format is publicly documented as well, so I'm sure there's other readers made unofficially.

I would like to write down your idea in README and make a link to your repository.

Absolutely, feel free to do so. I've also spent some time automating accuracy checks on the databases which shows the CC0 DBs are about half as accurate as the GeoLite2 databases from MaxMind with the PDDL DBs being another 10% less accurate on top of that.

So I can confirm you appear to be correctly recommending the geo-whois-asn-country over iptoasn-country-ipv4 in the readme of this repository when looking for attribute-free databases. I will try to expand the testing over time and potentially try to see if there's a more accurate method / order for merging the 3 CC0 databases to create the geo-whois-asn-country database.

Thanks!

BelleNottelling avatar Dec 07 '23 05:12 BelleNottelling