How to get country code and currency code by IP-ad

2019-01-17 12:39发布

问题:

I am new in zend Framework. And i want to get currency code, country code by the ip-address.

Can i have any example url?.

Please Help me...

Thanks in advance.

回答1:

You can use my service, the http://ipinfo.io API to get the country code:

function get_country($ip) {
    return file_get_contents("http://ipinfo.io/{$ip}/country");
}

echo get_country("8.8.8.8"); // => US

If you're interested in other details you could make a more generic function:

function ip_details($ip) {
    $json = file_get_contents("http://ipinfo.io/{$ip}");
    $details = json_decode($json);
    return $details;
}

$details = ip_details("8.8.8.8");

echo $details->city;     // => Mountain View
echo $details->country;  // => US
echo $details->org;      // => AS15169 Google Inc.
echo $details->hostname; // => google-public-dns-a.google.com

I've used the IP 8.8.8.8 in these examples, but if you want details for the user's IP just pass in $_SERVER['REMOTE_ADDR'] instead. More details are available at http://ipinfo.io/developers

You can get a mapping of country codes to currency codes from http://country.io/data/ and add that to your code. Here's a simple example:

function getCurrenyCode($country_code) {
    $currency_codes = array(
        'GB' => 'GBP',
        'FR' => 'EUR',
        'DE' => 'EUR',
        'IT' => 'EUR',
    );

    if(isset($currency_codes[$country_code])) {
        return $curreny_codes[$country_code];
    }

    return 'USD'; // Default to USD
}


回答2:

Many-many thanks to jmathai , ToonMariner , experimentX for precious advice.

But i have got the simple solution

 public function getCountryIp()
{
    $currency = new Zend_Currency();
    $countryCode = $this->getCountryFromIP();
    $currencyCode = $currency->getCurrencyList($countryCode);
    $localCurrency = $this->currency('USD',$currencyCode[0],50);
    $var['currencyCode'] = $currencyCode[0];
    $var['currency'] = $localCurrency;
    return $var;
}



//use to convert currency



public function currency($from_Currency, $to_Currency, $amount)
 {
        $amount = urlencode($amount);
        $from_Currency = urlencode($from_Currency);
        $to_Currency = urlencode($to_Currency);
        $url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency";
        $ch = curl_init();
        $timeout = 0;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $rawdata = curl_exec($ch);
        curl_close($ch);
        $data = explode('"', $rawdata);
        $data = explode(' ', $data['3']);
        $stripped = ereg_replace("[^A-Za-z0-9.\+]", "", $data['0']);//remove special char
        return round($stripped,3);
//        $var = $data['0'];
//        return $var;
//        return round($var, 8);
    }

 //get ip-address and show country code


 public function getCountryFromIP()
 {
     $ip = $_SERVER['REMOTE_ADDR'];

    $country = exec("whois $ip  | grep -i country"); // Run a local whois and get the result back
    //$country = strtolower($country); // Make all text lower case so we can use str_replace happily
    // Clean up the results as some whois results come back with odd results, this should cater for most issues
    $country = str_replace("country:", "", "$country");
    $country = str_replace("Country:", "", "$country");
    $country = str_replace("Country :", "", "$country");
    $country = str_replace("country :", "", "$country");
    $country = str_replace("network:country-code:", "", "$country");
    $country = str_replace("network:Country-Code:", "", "$country");
    $country = str_replace("Network:Country-Code:", "", "$country");
    $country = str_replace("network:organization-", "", "$country");
    $country = str_replace("network:organization-usa", "us", "$country");
    $country = str_replace("network:country-code;i:us", "us", "$country");
    $country = str_replace("eu#countryisreallysomewhereinafricanregion", "af", "$country");
    $country = str_replace("", "", "$country");
    $country = str_replace("countryunderunadministration", "", "$country");
    $country = str_replace(" ", "", "$country");

    return $country;
 }


回答3:

You should be able to use the MaxMind database for this.

http://www.maxmind.com/app/country



回答4:

Get detailed country code, currency, currency converter, currency symbol etc from http://www.geoplugin.net/json.gp?ip="ip address here"



回答5:

An example based on ipdata.co, which gives you the currency symbol and code directly from an IP address.

This answer uses a 'test' API Key that is very limited and only meant for testing a few calls. Signup for your own Free API Key and get up to 1500 requests daily for development.

The API also has 10 global endpoints each able to handle >10,000 calls per second!

$ip = '78.8.53.5';
$details = json_decode(file_get_contents("https://api.ipdata.co/{$ip}?api-key=test"));
echo $details->country_name;
//Poland
echo $details->city;
//Głogów
echo $details->currency;
// PLN
echo $details->currency_symbol;
// zł

Disclaimer

I created this service.



回答6:

You will need something like geoip - there is another one I used recently which is subscription based (can't remeber its name at the mo).



回答7:

Perhaps this one should also help http://api.ip2.cc.nyud.net/?api=cname&ip=112.197.167.19

Also there's excellent question Good php API for extracting country code from IP? perhaps you can create a plugin for extracting the country code and currency code in zend framework.



回答8:

(new Zend_Currency(null, 'GB'))->getShortName();

Returns string 'GBP'.



回答9:

You can use https://ip-api.io for this task easily.