2011-04-21 25 views
8

Soy nuevo en Zend Framework. Y quiero obtener el código de moneda, el código de país por la dirección IP.¿Cómo obtener el código de país y el código de moneda por dirección IP?

¿Puedo tener algún ejemplo de url ?.

Por favor, ayúdame ...

Gracias de antemano.

+0

¡ipdata.co le proporciona el código de moneda y el código de una dirección IP directamente! Ver mi respuesta a continuación https://stackoverflow.com/a/47142938/3176550 – Jonathan

Respuesta

4

Muchos-muchas gracias a jmathai, ToonMariner, experimentX de valiosos consejos.

pero tengo la solución simple

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

Esta url (http://www.google.com/ig/calculator) ya no funciona –

+0

Modifique su código para utilizar esta url en su lugar ... 'https: //www.google.com/finance/converter? a = 1000 & from = USD & to = AUD' –

1

Tendrá algo así como geoip - hay otra Recientemente he usado, que es basado en la suscripción (no recordaba su nombre en el mo).

9

Puede usar mi servicio, la API http://ipinfo.io para obtener el código del país:

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

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

Si usted está interesado en otros detalles que podrían hacer una función más genérica:

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 

he utilizado el IP 8.8.8.8 en estos ejemplos, pero si desea detalles para la IP del usuario, simplemente pase el $_SERVER['REMOTE_ADDR']. Más detalles están disponibles en http://ipinfo.io/developers

Puede obtener un mapeo de los códigos de país a los códigos de moneda desde http://country.io/data/ y agregar eso a su código. Aquí hay un ejemplo simple:

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(); 

devoluciones string 'GBP'.

0

Puede utilizar https://ip-api.io para esta tarea fácilmente.

+0

Sería útil tener un ejemplo de una llamada a esta API. –

2

Un ejemplo basado en https://ipdata.co, que le proporciona el código y el símbolo de moneda directamente desde una dirección IP.

¡La API también tiene 10 puntos finales globales, cada uno capaz de manejar llamadas> 800M diariamente!

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

Negación

he creado este servicio.

Cuestiones relacionadas