How to do a geoip lookup in php?

后端 未结 2 1932
青春惊慌失措
青春惊慌失措 2021-01-14 12:16

All tutorials I have seen have pointed towards functions like geoip_record_by_name. I always get this error: Fatal error: Call to undefined function geoip

2条回答
  •  春和景丽
    2021-01-14 13:11

    If you have MySQL server, there are IP databases that you can use and install for free, then do a $_SERVER['REMOTE_ADDR'] and run it against the database data.

    i.e. (based on some arbitrary db)

    
    

    Some databases(Or just google it): http://www.ipinfodb.com/ip_database.php

    EDIT

    You can also do JSON/XML requests from other APIs and parse the data:

    i.e. (Using ipinfodb.com again)

    $doc->loadXML(file_get_contents("http://api.ipinfodb.com/v2/ip_query.php?key=your_key&ip=" . $_SERVER['REMOTE_ADDR'] . "&timezone=false")); 
    
    $country = $doc->getElementsByTagName('CountryName')->item(0)->nodeValue;
    

提交回复
热议问题