回答
多對多的感謝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