使用PHP货币转换 [英] Currency Conversion using PHP

查看:203
本文介绍了使用PHP货币转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方式来任何金额从一种货币转换到另一个网站上。用户可以输入类似100,然后选择美元作为货币,然后选择澳大利亚或加元作为货币转换为。当他点击转换按钮,我想这个数额自动转换,通过一些API,并展示了他的货币,他选择将其转换为量。

I'm looking for a way to convert any amount from one currency to another on a website. The user would enter something like '100' and select USD as the currency, and then chooses Australian or Canadian dollars as the currency to convert to. When he clicks the 'Convert' button, I'd like to convert that amount automatically, through some API, and show him the amount in the currency he chose to convert to.

任何想法?

推荐答案

片段

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']);
    $var = $data['0'];
    return round($var,2);
}

您不必然需要,虽然卷曲。 的file_get_contents 应该做的太多,如果在 allow_furl_open 已启用:

You dont necessarily need cURL for that though. file_get_contents should do too if the allow_furl_open is enabled:

$result = file_get_contents(
    'http://www.google.com/ig/calculator?hl=en&q=100EUR=?USD'
);

这将返回类似

{lhs: "100 Euros",rhs: "129.18 U.S. dollars",error: "",icc: true}

注意的iGoogle将于2013年11月1日退役。

这篇关于使用PHP货币转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆