Article

How to access the API

There is source codes written in PHP and Python(for Django). Other languages and codes coming soon.

PHP and fsockopen

// Your domain name here
$site_name = "www.your-site-name.com";

function getUserCountry() {
    $fp = fsockopen("api.wipmania.com", 80, $errno, $errstr, 5);
    if (!$fp) {
        // API is currently down, return as "Unknown" :(
        return "XX";
    } else {
        $out = "GET /".$_SERVER['REMOTE_ADDR']."?".$site_name." HTTP/1.1\r\n";
        $out .= "Host: api.wipmania.com\r\n";
        $out .= "Typ: php\r\n";
        $out .= "Ver: 1.0\r\n";
        $out .= "Connection: Close\r\n\r\n";
        fwrite($fp, $out);
        while (!feof($fp)) {
            $country = fgets($fp, 3);
        }
        fclose($fp);
        return $country;
    }
}

PHP and curl

Other alternative (Thanks to www.anieto2k.com)
// Your domain name here
$site_name = "www.your-site-name.com";

function getUserCountry() {
    $url = 'http://api.wipmania.com/'.$_SERVER['REMOTE_ADDR'].'?'.$site_name;
    $ch = curl_init();
    $headers = "Typ: phpcurl\r\n";
    $headers .= "Ver: 1.0\r\n";
    $headers .= "Connection: Close\r\n\r\n";
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array($headers));
    $content = curl_exec($ch);
    curl_close($ch);
    return $content;
}

For Django framework (python)

In your views:
from worldip import getUserCountry
remote_addr = request.META.get('REMOTE_ADDR',"127.0.0.1")
country = getUserCountry(remote_addr)
Add in settings.py your URL as SITE_URL, like
SITE_URL = "http://www.google.com"
Small function getUserCountry in worldip.py, you can download it here
#!/usr/bin/env python

from urllib2 import urlopen, Request
import re, socket
from django.conf import settings

domain_re = re.compile('^(http|https):\/\/?([^\/]+)')
domain = domain_re.match(settings.SITE_URL).group(2)

def getUserCountry(ip):
    url = "http://api.wipmania.com/" + ip + "?" + domain
    socket.setdefaulttimeout(5)
    headers = {'Typ':'django','Ver':'1.0','Connection':'Close'}
    try:
        req = Request(url, None, headers)
        urlfile = urlopen(req)
        land = urlfile.read()
        urlfile.close()
        return land[:2]
    except Exception:
        return "XX"
If you use our database or API, we will be very happy, if you include a link back to WIPmania on your website.
Anonymous Ukraine09 Sep 2008 12:09#18Русский   
wipmania logo Alrond 09 Sep 2008 14:09#19Русский   
Anonymous Russia11 Sep 2008 05:09#21Русский   
wipmania logo Alrond 11 Sep 2008 06:09#22Русский   
Anonymous Russia11 Sep 2009 01:09#1650Русский   
wipmania logo Alrond 18 Sep 2009 06:09#1666Русский   
Henk-Sjoerd Duijst Netherlands01 Nov 2009 19:11#1764English   
Regarding to the PHP functions getUserCountry() using fsock and curl, a simple file_get_contents() seems to work fine as well.

$country = file_get_contents('http://api.wipmania.com/' .

$_SERVER['REMOTE_ADDR'] . '?' . $_SERVER['SERVER_NAME']);


Whereas:
$_SERVER['REMOTE_ADDR'] = visitor's IP
$_SERVER['SERVER_NAME'] = domain name of sender

Cheers,
Henk-Sjoerd

(By the way: how to highlight PHP code?)
wipmania logo Alrond 01 Nov 2009 20:11#1767English   
Hello Henk-Sjoerd, thanks for your suggestion.
Highlight can you find here
Anonymous Germany21 Jan 2010 04:01#2094Русский   
United Kingdom08 Dec 2010 12:12#3060English   
$_SERVER['SERVER_NAME'] = domain name of sender

Anonymous/Name OpenID OpenID Yahoo! OpenID AOL WordPress LiveJournal TypePad Vox Myvidoop Orange Yandex YandexBlog Mixi Myspace Flickr Verisign MyOpenID ClaimID

All fields are not required

Leave blank for main post.
Comment - 5000 characters maximum. BBcode is allowed.

Subscribe to answers: to all comments:RSS comments (en)

Please have a look at our
Facebook Page and leave a comment

Creative Commons License
Creative Commons Attribution-Noncommercial 3.0 License