如何通过IP地址获取国家代码和货币代码?

13

我刚接触Zend框架,想要通过IP地址获取货币代码和国家代码。

请问有示例URL吗?

请帮助我......

提前致谢。

9个回答

16

您可以使用我的服务,即http://ipinfo.io API,以获取国家代码:

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

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

如果您对其他细节感兴趣,您可以编写一个更通用的函数:

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

我在这些示例中使用了IP 8.8.8.8,但如果您想获取用户的IP详细信息,只需传入 $_SERVER['REMOTE_ADDR']。有关更多详细信息,请访问http://ipinfo.io/developers

您可以从 http://country.io/data/ 获取国家代码到货币代码的映射,并将其添加到您的代码中。以下是一个简单的示例:

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
}

6
非常感谢 jmathaiToonMarinerexperimentX 提供的宝贵建议。但我已经找到了简单的解决方案。
 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;
 }

修改您的代码,使用此URL而不是... https://www.google.com/finance/converter?a=1000&from=USD&to=AUD - Stephen McFarland

5

这不是一个好的解决方案,API使用HTTP而不是HTTPS公开。 - Alexandre V.

3
基于ipdata.co的一个示例,它可以直接从IP地址获取货币符号和代码。

这个答案使用了一个非常有限的“测试”API密钥,仅用于测试几个调用。注册您自己的免费API密钥,并获得每天高达1500个请求进行开发。

API还有10个全球终端点,每个终端点都能处理每秒超过10,000次呼叫!
$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ł

免责声明:
我创建了这项服务。

2

1
你需要类似 geoip 这样的东西 - 最近我使用了另一个基于订阅的工具(目前无法记住它的名称)。

1

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

返回字符串 'GBP'。

-1

提供一个调用此 API 的示例将非常有用。 - Gonzalo Matheu
1
它不是免费的。他们没有任何免费使用的规定。 - Arif

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接