Get country location of an IP with native PHP

前端 未结 8 1056
野性不改
野性不改 2020-12-23 16:46

Read on before you say this is a duplicate, it\'s not. (as far as I could see)

I want to get the county code in php from the client.

Yes I know you can do th

相关标签:
8条回答
  • 2020-12-23 17:01

    MaxMind provide a free PHP GeoIP country lookup class (there is also a free country+city lookup one).

    The bit you want is what is mentioned under "Pure PHP module". This doesn't require you to install anything, or be dependent on them, nor does it need any special PHP modules installed. Just save the GeoIP data file somewhere, then use their provided class to interact with it.

    0 讨论(0)
  • 2020-12-23 17:02

    Here is a site with a script i just used. The only problem is that you would probably every now and then need to regenerate IPs by yourself... which might be pain and tahts why everyone is telling you to use external API. But for me that wasnt solution as i was pulling like 50 IPs at once, which means i would probably get banned. So solution was to use my own script or to do saves to DB, but i was again pulling images from external sites. Anyway here is the site i found script on:

    http://coding-talk.com/f29/country-flag-script-8882/

    0 讨论(0)
  • 2020-12-23 17:05

    Can you just install a copy of whois into your home directory and pass the full path into shell_exec? That way you're not bound to their upgrade schedule.

    0 讨论(0)
  • 2020-12-23 17:05

    An alternative, somewhat extreme solution to your problem would be to:

    1. Download the CSV format version of MaxMind's country database
    2. Strip out the information you don't need from the CSV with a script and ...
    3. ... generate a standard PHP file which contains a data structure containing the IP address as the key and the country code as the value.
    4. Include the resulting file in your usual project files and you now have a completely internal IP => country code lookup table.

    The disadvantage is that, regularly, you would need to regenerate the PHP file from the latest version of the database. Also, it's a pretty nasty way of doing it in general and performance might not be the best :)

    0 讨论(0)
  • 2020-12-23 17:07

    here is also one of them. just change the IP to the variable:
    http://api.codehelper.io/ips/?callback=codehelper_ip_callback&ip=143.3.87.193

    0 讨论(0)
  • 2020-12-23 17:11

    Whois is just a client for the whois service, so technically you are still relying on an outside site. For the queries that fail, you could try falling back to another site for the query, such as hostip.info, who happen to have a decent API and seem friendly:

    http://api.hostip.info/country.php?ip=4.2.2.2
    

    returns

    US
    

    Good luck,

    --jed

    EDIT: @Mint Here is the link to the API on hostip.info: http://www.hostip.info/use.html

    0 讨论(0)
提交回复
热议问题