[英]How to get country code and currency code by IP-address?
我是zend Framework的新手。 我想通過ip-address獲取貨幣代碼,國家代碼。
我可以有任何示例網址嗎?
請幫我...
提前致謝。
您可以使用我的服務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
}
很多 - 感謝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.geoplugin.net/json.gp?ip="ip地址獲取詳細的國家代碼,貨幣,貨幣轉換器,貨幣符號等“
基於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ł
放棄
我創建了這項服務。
您應該能夠使用MaxMind數據庫。
你將需要像geoip這樣的東西 - 我最近使用的另一個是基於訂閱的(不能在mo上記住它的名字)。
也許這個也應該幫助http://api.ip2.cc.nyud.net/?api=cname&ip=112.197.167.19
還有一個很好的問題好php API從IP提取國家代碼? 也許你可以創建一個插件來提取zend框架中的國家代碼和貨幣代碼。
(new Zend_Currency(null, 'GB'))->getShortName();
返回string 'GBP'
。
您可以輕松地使用https://ip-api.io完成此任務。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.