Ver Mensaje Individual
  #3 (permalink)  
Antiguo 11/10/2013, 10:38
Avatar de andresdzphp
andresdzphp
Colaborador
 
Fecha de Ingreso: julio-2011
Ubicación: $this->Colombia;
Mensajes: 2.749
Antigüedad: 12 años, 10 meses
Puntos: 793
Respuesta: convertir monto de soles a dolares

Prueba así:

Código PHP:
Ver original
  1. function  currency_paypal($from_Currency,$to_Currency,$amount) {
  2.     $amount = urlencode($amount);
  3.     $from_Currency = urlencode($from_Currency);
  4.     $to_Currency = urlencode($to_Currency);
  5.     $url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency";
  6.     $ch = curl_init();
  7.     $timeout = 0;
  8.     curl_setopt ($ch, CURLOPT_URL, $url);
  9.     curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  10.     curl_setopt($ch,  CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
  11.     curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  12.     $rawdata = curl_exec($ch);
  13.     curl_close($ch);
  14.     $data = explode('"', utf8_decode($rawdata));
  15.     $data = explode(' ', $data['3']);
  16.     $var = preg_replace('@\s+@', '', $data['0']);
  17.     return $var;
  18. }
  19.  
  20. echo currency_paypal('PEN', 'USD', 8000);

No necesitas aplicar number_format ya que google lo da formateado (pero si quieres hacerlo ya no te dará problemas), pero si te salen caracteres raros aveces y se corta el resto del valor, por eso usé utf8_decode y con preg_replace le quité unos espacios que sobran.
__________________
Si sabemos como leer e interpretar el manual será mucho más fácil aprender PHP. En lugar de confiar en ejemplos o copiar y pegar - PHP

Última edición por andresdzphp; 11/10/2013 a las 10:50