maxmind / GeoIP2-php

PHP API for GeoIP2 webservice client and database reader
https://maxmind.github.io/GeoIP2-php/
Apache License 2.0
2.3k stars 274 forks source link

Is it possible to get an array of all countries/cities from a country? #222

Closed cawa-93 closed 4 months ago

cawa-93 commented 4 months ago

I am wondering if it is possible to get an array of all countries, all cities in a country that are contained in the provided database using this library?

I've looked through the documentation, but didn't find any relevant examples.

oschwald commented 4 months ago

No, the data is not organized in a way to make this reasonably possible. I'd suggest either using the CSVs for this or using GeoNames directly.

cawa-93 commented 4 months ago

It sounds reasonable. My case is to create a UI component for the site administrator that requires data on cities that GeoIP2 can detect. For the same reason, I can't just take the data from GeoName directly, because it seems that GeoName has more entities than your set. I don't need to read them in the runtime. I can easily read the database and generate some JSON at the build step. So performance is not an issue at all.

Of course, I can get the data I need from CSV, but I would really like to keep a single source of truth. Maybe I can somehow getAllIterator() to foreach all entities in database and collect all needed data from it?

oschwald commented 4 months ago

There is no way to iterate over the database with the PHP API. The CSVs are built from the MMDB files using the Go reader and you could iterate over the database in a similar way. It isn't particularly fast. The readers that currently support iteration are Python, .NET, Java, and the unofficial Go reader. There are currently no plans to add this feature to the PHP API as most users of the PHP API use it while processing an HTTP request and such iteration is far too slow for that.