回答
多对多的感谢jmathai,ToonMariner,experimentX的宝贵意见。
但我已经得到了简单的解决方案
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;
}
此网址(http://www.google.com/ig/calculator)不再有效 – 2014-11-09 23:33:18
修改您的代码以使用此网址代替... 'https://www.google.com/finance/converter?a = 1000&from = USD&to = AUD' – 2016-07-13 18:40:15
你应该可以使用MaxMind数据库。
你需要像geoip - 还有另外一个我最近使用它是一个基于订阅(在莫不记得它的名字)。
也许这一个也应该帮助http://api.ip2.cc.nyud.net/?api=cname&ip=112.197.167.19
而且还有很好的问题Good php API for extracting country code from IP?也许你可以提取Zend Framework的国家代码和货币代码创建一个插件。
你可以用我的服务,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
}
(new Zend_Currency(null, 'GB'))->getShortName();
返回string 'GBP'
。
获得详细的国家代码,货币,汇率换算,货币符号等从http://www.geoplugin.net/json.gp?ip="ip address here"
基于https://ipdata.co的示例,该示例为您提供直接来自IP地址的货币符号和代码。
该API还具有10个全局端点,每个端点每天能够处理> 800M的呼叫!
$ip = '78.8.53.5';
$details = json_decode(file_get_contents("https://api.ipdata.co/{$ip}"));
echo $details->country_name;
//Poland
echo $details->city;
//Głogów
echo $details->currency;
// PLN
echo $details->currency_symbol;
// zł
免责声明
我创造了这个服务。
- 1. 获取国家的货币代码
- 2. 如何通过IP地址获取国家/地区名称和/或代码?
- 3. SQL IP地址到国家代码
- 4. 从国家代码获取货币信息在PHP
- 5. 获取国家和地区代码
- 6. 通过代码android模拟器获取pc的ip地址通过代码android
- 7. 如何将国家名称转换为货币代码?
- 8. 如何按国家获取货币?
- 9. 获取ISO国家代码
- 10. 如何从iphone中的货币代码获取货币符号?
- 11. 新的PayPal IPN模拟器缺少国家代码和货币
- 12. 从国家或国家代码获取电话代码 - Twilio
- 13. 如何从货币代码
- 14. 从国家代码中获取国家/地区名称
- 15. 获取IP地址的C代码
- 16. Perl代码获取ip地址
- 17. Swift 3:如何从国家代码获取语言代码?
- 18. 代码找到状态,国家,城市从IP地址在PHP
- 19. 从货币代码中获取CultureInfo?
- 20. 基于国家代码的货币符号
- 21. 格式NSDecimalNumber货币的国家代码,而无需精密
- 22. 从Objective-C中获取货币代码的货币符号
- 23. 选择国家和国家代码?
- 24. WCF REST服务:通过国家代码
- 25. 如何从Firefox扩展代码获取本地IP地址
- 26. 如何从任何号码获取国家名称/代码
- 27. Android获取ISO国家代码
- 28. Android获取设备国家代码
- 29. 获取用户的国家代码
- 30. Android获取国家代码的位置
ipdata.co直接为您提供货币符号和IP地址代码!请参阅我的回答以下地址https://stackoverflow.com/a/47142938/3176550 – Jonathan 2018-02-18 14:30:45