2011-04-21 8 views

risposta

4

molti-molti grazie alla jmathai, ToonMariner, experimentX per un consiglio prezioso.

Ma ho avuto la soluzione semplice

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; 
} 
+0

Questo URL (http://www.google.com/ig/calculator) non funziona più –

+0

Modificare il codice per utilizzare questo URL invece ... 'https: //www.google.com/finance/converter? a = 1000 & from = USD & a = AUD' –

1

Avrete bisogno di qualcosa di simile a geoip - ce n'è un altro che ho usato di recente, che è basato su abbonamento (non può bassino il suo nome al mo).

9

È possibile utilizzare il mio servizio, l'API http://ipinfo.io per ottenere il codice di paese:

function get_country($ip) { 
    return file_get_contents("http://ipinfo.io/{$ip}/country"); 
} 

echo get_country("8.8.8.8"); // => US 

Se siete interessati a altri dettagli si potrebbe fare una funzione più generica:

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 

Ho usato l'IP 8.8.8.8 in questi esempi, ma se desideri i dettagli per l'IP dell'utente, passa semplicemente in $_SERVER['REMOTE_ADDR']. Ulteriori dettagli sono disponibili al http://ipinfo.io/developers

È possibile ottenere una mappatura dei codici paese in codici valuta da http://country.io/data/ e aggiungerla al proprio codice. Ecco un semplice esempio:

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 
} 
0
(new Zend_Currency(null, 'GB'))->getShortName(); 

Returns string 'GBP'.

0

È possibile utilizzare https://ip-api.io per questo compito con facilità.

+0

Sarebbe utile avere un esempio di chiamata a questa API. –

2

Un esempio basato su https://ipdata.co, che fornisce il simbolo di valuta e il codice direttamente da un indirizzo IP.

L'API dispone inoltre di 10 endpoint globali, ciascuno in grado di gestire> 800 milioni di chiamate al giorno!

$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ł 

responsabilità

ho creato questo servizio.

Problemi correlati