Geo Tageting Using HostIP API and PHP

Discussion in 'PHP' started by megler, Jan 20, 2008.

  1. #1
    Hello,

    Has anyone used the HostIP API and PHP for Geo targeting? Their site stinks for showing any script examples for setup and I'd really like to try this.

    The API I'm trying to use is http://api.hostip.info/get_html.php?ip=12.215.42.19

    Does anyone have a second to show me the PHP code that would go with this to make that API pull and work? I know that once it works, it should display something like:

    Country: UNITED STATES (US)
    City: Sugar Grove, IL

    I THINK to call it, the code would be along the lines of:

    <?
    
    $country = '';
    $IP = $_SERVER['REMOTE_ADDR'];
    
    if (!empty($IP)) {
    $country = file_get_contents('http://api.hostip.info/country.php?ip='.$IP);
    }
    ?>
    PHP:
    But not sure how to finish it up. Any help would be greatly appreciated!!!!

    Marceia
     
    megler, Jan 20, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Have you tried MaxMind's one? There's a free version of the database, and as it all client side (database stored on server), it instantly looks up IPs to Citys/Countrys, with no delays (also no connection issues).

    Jay
     
    jayshah, Jan 20, 2008 IP
  3. megler

    megler Peon

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yeah, I'm having issues with that one, too. In the maxmind case, I'm generating errors:

    Warning: fopen(GeoIP.dat) [function.fopen]: failed to open stream: No such file or directory in http://completesources.com/geoip/geoip.inc on line 314

    I think the database is getting corrupted when I upload it (in binary). I have a ticket in with my host to see if they will unpack it on the server for me vs me unpacking it on my computer then uploading it. Just to see if that fixes it.

    In the meantime, I'm trying this hostip to see if I can get anything to work. It's been a very frustrating day. :(
     
    megler, Jan 20, 2008 IP
  4. mvl

    mvl Peon

    Messages:
    147
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I use the Maxmind city database webservice for this at http://getmyip.eu/ and a few other sites.
    It is a paid service but it has never let me down. Maxmind have code examples available for their customers.
     
    mvl, Jan 20, 2008 IP
  5. megler

    megler Peon

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I've heard great things about maxmind. I actually JUST found the fix to the maxmind error. The page to call the code apparantly has to be in the same directory as the .dat and the .inc files. That's the problem with free, sometimes, lack of documentation. I really should put out the 50 bucks so I can get better support. LOL.

    I still wouldn't mind knowing how to call that API just for personal knowledge, but yes, I will probably use the maxmind script.
     
    megler, Jan 20, 2008 IP