免费的汇率API api
程序员文章站
2024-03-14 16:08:40
...
来自我的博客:http://www.meedee.cn/2016/07/06/%e5%85%8d%e8%b4%b9%e7%9a%84%e6%b1%87%e7%8e%87api/
1 https://currencylayer.com/
documents:https://currencylayer.com/documentation
免费功能:1个月1000次的api记录,可以使用live,historical,可以传currencies获取指定的货币汇率。
不能使用convert,source,from,to,amout等参数
php实现代码
配置文件
'CURRENCY_API'=>"http://apilayer.net/api/historical?access_key=xxx&format=1"
public function testCurrencyApi()
{
$date = I("request.date") ? I("request.date") : "2016-07-02";
$price = I("request.price") ? I("request.price") : "100";
$from = I("request.from") ? I("request.from") : "CNY";
//$currencies = I("request.currencies") ? I("request.currencies") : "HKD,CNY,USD,AUD,MOP,MYR";
$to = "HKD";
$source = "USD";
// AUD:澳幣,MOP:澳門幣,MYR:馬來西亞
$endpoint = C("CURRENCY_API");
//$searchData['currencies'] = $currencies;
$searchData['date'] = $date;
//$searchData['price'] = $price;
$endpoint .= "&" . http_build_query($searchData);
$memcache_key = md5($endpoint);
$cache = S(array('type'=>'memcache','host'=>C("MEMCACHE_HOST"),'port'=>C("MEMCACHE_PORT"),'expire'=>0));
$memcache_data = S($memcache_key);
if ($memcache_data) {
$currency = $memcache_data;
echo "use cache!
";
} else {
$result = addCurl($endpoint);
$currency = json_decode($result,true);
S($memcache_key, $currency);
}
if (!$currency['success']) {
$this->response(false, "get api failed");
}
$quotes = $currency['quotes'];
//dump($quotes);
// to usd
$usdPrice = $price / $quotes[$source . $from];
echo "currency date:" . $currency['date'] . "
";
echo "usd price:{$usdPrice}
";
$hkdPrice = $usdPrice * $quotes[$source . $to];
echo "hk price:" . round($hkdPrice, 2);
}
1 https://currencylayer.com/
documents:https://currencylayer.com/documentation
免费功能:1个月1000次的api记录,可以使用live,historical,可以传currencies获取指定的货币汇率。
不能使用convert,source,from,to,amout等参数
php实现代码
配置文件
'CURRENCY_API'=>"http://apilayer.net/api/historical?access_key=xxx&format=1"
public function testCurrencyApi()
{
$date = I("request.date") ? I("request.date") : "2016-07-02";
$price = I("request.price") ? I("request.price") : "100";
$from = I("request.from") ? I("request.from") : "CNY";
//$currencies = I("request.currencies") ? I("request.currencies") : "HKD,CNY,USD,AUD,MOP,MYR";
$to = "HKD";
$source = "USD";
// AUD:澳幣,MOP:澳門幣,MYR:馬來西亞
$endpoint = C("CURRENCY_API");
//$searchData['currencies'] = $currencies;
$searchData['date'] = $date;
//$searchData['price'] = $price;
$endpoint .= "&" . http_build_query($searchData);
$memcache_key = md5($endpoint);
$cache = S(array('type'=>'memcache','host'=>C("MEMCACHE_HOST"),'port'=>C("MEMCACHE_PORT"),'expire'=>0));
$memcache_data = S($memcache_key);
if ($memcache_data) {
$currency = $memcache_data;
echo "use cache!
";
} else {
$result = addCurl($endpoint);
$currency = json_decode($result,true);
S($memcache_key, $currency);
}
if (!$currency['success']) {
$this->response(false, "get api failed");
}
$quotes = $currency['quotes'];
//dump($quotes);
// to usd
$usdPrice = $price / $quotes[$source . $from];
echo "currency date:" . $currency['date'] . "
";
echo "usd price:{$usdPrice}
";
$hkdPrice = $usdPrice * $quotes[$source . $to];
echo "hk price:" . round($hkdPrice, 2);
}
推荐阅读
-
免费的汇率API api
-
使用GroovyHelp高效掌握API的变化(以比较Java 7与Java 6为例) GroovyHelpJava 7APIjavadocchm
-
C# web api返回类型设置为json的两种方法 博客分类: web API api
-
在谷歌目标检测(Google object_detection) API 上训练自己的数据集
-
ubuntu下谷歌开源的TensorFlow Object Detection API的安装教程
-
浅谈PHP的反射API
-
Java中使用Preferences 的 API设置用户偏好
-
thinkPHP5.0框架API优化后的友好性分析
-
MongoDB最简单的入门教程之五-通过Restful API访问MongoDB mongoDBRestfulAPI微服务
-
用Java实现全国天气预报的api接口调用示例