如何通过IP地址获取国家代码和货币代码? [英] How to get country code and currency code by IP-address?
本文介绍了如何通过IP地址获取国家代码和货币代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是zend Framework中的新成员。我想通过ip地址获取货币代码,国家代码。
我可以有任何示例网址吗?
请帮助我...
在此先感谢您。
解决方案
许多感谢 jmathai , ToonMariner , experimentX 提供宝贵建议。
但我有简单的解决方案
public function getCountryIp()
{
$ currency = new Zend_Currency();
$ countryCode = $ this-> getCountryFromIP();
$ currencyCode = $ currency-> getCurrencyList($ countryCode);
$ localCurrency = $ this->货币('USD',$ currencyCode [0],50);
$ var ['currencyCode'] = $ currencyCode [0];
$ var ['currency'] = $ localCurrency;
返回$ var;
}
//用于转换货币
公共功能货币($ 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=zh-CN&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']); //删除特殊字符
return round($ stripped,3);
// $ var = $ data ['0'];
// return $ var;
// return round($ var,8);
}
/ / get ip-address并显示国家代码
public function getCountryFromIP()
{
$ ip = $ _SERVER ['REMOTE_ADDR'];
$ country = exec(whois $ ip | grep -i country); //运行一个本地whois并得到结果
// $ country = strtolower($ country); // Make所有的文本小写,所以我们可以愉快地使用str_replace
//清理结果,因为一些whois结果返回奇数结果,这应该适合大多数问题
$ country = str_replace(country:, ,$ country);
$ country = str_replace(Country:,,$ country);
$ country = str_re地点(国家:,,$ 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);
返回$ country;
}
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.
解决方案
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;
}
这篇关于如何通过IP地址获取国家代码和货币代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文