php 如何通过IP地址获取国家代码和货币代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5741347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get country code and currency code by IP-address?
提问by Manoj
I am new in zend Framework. And i want to get currency code, country code by the ip-address.
我是 zend 框架的新手。我想通过 ip 地址获取货币代码、国家代码。
Can i have any example url?.
我可以有任何示例网址吗?
Please Help me...
请帮我...
Thanks in advance.
提前致谢。
采纳答案by Manoj
Many-many thanks to jmathai, ToonMariner, experimentXfor precious advice.
非常感谢jmathai、ToonMariner、experimentX提供宝贵的建议。
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;
}
回答by Ben Dowling
You can use my service, the http://ipinfo.ioAPI to get the country code:
您可以使用我的服务http://ipinfo.ioAPI 来获取国家/地区代码:
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
我8.8.8.8
在这些示例中使用了 IP ,但如果您想要用户 IP 的详细信息,只需传入即可$_SERVER['REMOTE_ADDR']
。更多详细信息请访问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:
您可以从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
}
回答by Md. Zubaer Ahammed
Get detailed country code, currency, currency converter, currency symbol etc from http://www.geoplugin.net/json.gp?ip="ip address here"
从http://www.geoplugin.net/json.gp?ip="ip address here"获取详细的国家代码、货币、货币转换器、货币符号等
回答by jmathai
You should be able to use the MaxMind database for this.
您应该能够为此使用 MaxMind 数据库。
回答by Jonathan
An example based on ipdata.co, which gives you the currency symbol and code directly from an IP address.
基于ipdata.co的示例,它直接从 IP 地址为您提供货币符号和代码。
This answer uses a 'test' API Key that is very limited and only meant for testing a few calls. Signupfor your own Free API Key and get up to 1500 requests daily for development.
这个答案使用了一个非常有限的“测试”API 密钥,仅用于测试几个调用。注册您自己的免费 API 密钥,每天最多可收到 1500 个开发请求。
The API also has 10 global endpoints each able to handle >10,000 calls per second!
该 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?
Disclaimer
免责声明
I created this service.
我创建了这个服务。
回答by Ian Wood
回答by Santosh Linkha
Perhaps this one should also help http://api.ip2.cc.nyud.net/?api=cname&ip=112.197.167.19
也许这个也应该有帮助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.
还有一个很好的问题Good php API for extracting country code from IP? 也许您可以创建一个插件,用于在 zend 框架中提取国家/地区代码和货币代码。
回答by Andrey E
You can use https://ip-api.iofor this task easily.
您可以轻松地使用https://ip-api.io完成此任务。
回答by umpirsky
(new Zend_Currency(null, 'GB'))->getShortName();
Returns string 'GBP'
.
返回string 'GBP'
。