您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“如何實(shí)現(xiàn)免費(fèi)的貨幣匯率API”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“如何實(shí)現(xiàn)免費(fèi)的貨幣匯率API”吧!
JSON返回示例:
{ "resultcode":"200", "reason":"SUCCESSED!", "result":[ { "data1":{ "name":"英鎊", /*貨幣名稱*/ "fBuyPri":"1001.430", /*現(xiàn)匯買入價(jià)*/ "mBuyPri":"970.510", /*現(xiàn)鈔買入價(jià)*/ "fSellPri":"1009.480", /*現(xiàn)匯賣出價(jià)*/ "mSellPri":"1009.480", /*現(xiàn)鈔賣出價(jià)*/ "bankConversionPri":"1014.870", /*銀行折算價(jià)/中間價(jià)*/ "date":"2012-12-13", /*發(fā)布日期*/ "time":"16:25:49" /*發(fā)布時(shí)間*/ }, "data2":{ "name":"港幣", "fBuyPri":"80.210", "mBuyPri":"79.570", "fSellPri":"80.520", "mSellPri":"80.520", "bankConversionPri":"81.140", "date":"2012-12-13", "time":"16:25:49" }, "data3":{ "name":"美元", "fBuyPri":"621.650", "mBuyPri":"616.670", "fSellPri":"624.150", "mSellPri":"624.150", "bankConversionPri":"628.870", "date":"2012-12-13", "time":"16:25:49" }, "data4":{ "name":"瑞士法郎", "fBuyPri":"670.310", "mBuyPri":"649.620", "fSellPri":"675.700", "mSellPri":"675.700", "bankConversionPri":"674.350", "date":"2012-12-13", "time":"16:25:49" }, "data5":{ "name":"新加坡元", "fBuyPri":"508.300", "mBuyPri":"492.610", "fSellPri":"512.390", "mSellPri":"512.390", "bankConversionPri":"512.000", "date":"2012-12-13", "time":"16:25:49" }, "data6":{ "name":"瑞典克朗", "fBuyPri":"93.340", "mBuyPri":"90.450", "fSellPri":"94.090", "mSellPri":"94.090", "bankConversionPri":"93.770", "date":"2012-12-13", "time":"16:25:49" }, "data7":{ "name":"丹麥克朗", "fBuyPri":"108.920", "mBuyPri":"105.550", "fSellPri":"109.790", "mSellPri":"109.790", "bankConversionPri":"109.430", "date":"2012-12-13", "time":"16:25:49" }, "data8":{ "name":"挪威克朗", "fBuyPri":"110.490", "mBuyPri":"107.080", "fSellPri":"111.380", "mSellPri":"111.380", "bankConversionPri":"111.060", "date":"2012-12-13", "time":"16:25:49" }, "data9":{ "name":"日元", "fBuyPri":"7.431", "mBuyPri":"7.201", "fSellPri":"7.483", "mSellPri":"7.483", "bankConversionPri":"7.531", "date":"2012-12-13", "time":"16:25:49" }, "data10":{ "name":"加拿大元", "fBuyPri":"630.950", "mBuyPri":"611.470", "fSellPri":"636.010", "mSellPri":"636.010", "bankConversionPri":"638.800", "date":"2012-12-13", "time":"16:25:49" }, "data11":{ "name":"澳大利亞元", "fBuyPri":"654.900", "mBuyPri":"634.680", "fSellPri":"660.160", "mSellPri":"660.160", "bankConversionPri":"663.300", "date":"2012-12-13", "time":"16:25:49" }, "data12":{ "name":"歐元", "fBuyPri":"812.520", "mBuyPri":"787.430", "fSellPri":"819.040", "mSellPri":"819.040", "bankConversionPri":"821.960", "date":"2012-12-13", "time":"16:25:49" }, "data13":{ "name":"澳門元", "fBuyPri":"77.910", "mBuyPri":"75.300", "fSellPri":"78.210", "mSellPri":"80.720", "bankConversionPri":"78.320", "date":"2012-12-13", "time":"16:25:49" }, "data14":{ "name":"菲律賓比索", "fBuyPri":"15.100", "mBuyPri":"14.640", "fSellPri":"15.220", "mSellPri":"15.690", "bankConversionPri":"15.220", "date":"2012-12-13", "time":"16:25:49" }, "data15":{ "name":"泰國銖", "fBuyPri":"20.240", "mBuyPri":"19.620", "fSellPri":"20.400", "mSellPri":"21.030", "bankConversionPri":"20.390", "date":"2012-12-13", "time":"16:25:49" }, "data16":{ "name":"新西蘭元", "fBuyPri":"524.400", "mBuyPri":"0.000", "fSellPri":"528.610", "mSellPri":"0.000", "bankConversionPri":"526.770", "date":"2012-12-13", "time":"16:25:49" }, "data17":{ "name":"韓國元", "fBuyPri":"0.000", "mBuyPri":"0.560", "fSellPri":"0.000", "mSellPri":"0.608", "bankConversionPri":"0.586", "date":"2012-12-13", "time":"16:25:49" } } ] }
外匯匯率 接口地址: http://web.juhe.cn:8080/finance/exchange/frate 支持格式: json 請求方式: get 請求示例: http://web.juhe.cn:8080/finance/exchange/frate?key=您申請的APPKEY 請求參數(shù)說明:
JSON返回示例:
{ "error_code":0, "resultcode":"200", "reason":"SUCCESSED!", "result":[ { "data1":{ "code":"JPY", /*貨幣代碼*/ "currency":"美元日元", /*貨幣名稱*/ "closePri":"83.41", /*最新價(jià)*/ "diffPer":"0.19%", /*漲跌%*/ "diffAmo":"0.16", /*漲跌金額*/ "openPri":"83.25", /*開盤價(jià)*/ "highPic":"83.67", /*最高價(jià)*/ "lowPic":"83.1", /*最低價(jià)*/ "range":"0.68%", /*震幅%*/ "buyPic":"83.41", /*買入價(jià)*/ "sellPic":"83.45", /*賣出價(jià)*/ "color":"Red", /*漲跌顏色*/ "date":"2016-07-29", /*日期*/ "datatime":"08:58:39" /*數(shù)據(jù)時(shí)間*/ }, "data2":{ "code":"CHF", "currency":"美元瑞郎", "closePri":"0.9241", "diffPer":"0.06%", "diffAmo":"0.0006", "openPri":"0.9234", "highPic":"0.9243", "lowPic":"0.9231", "range":"0.13%", "buyPic":"0.9241", "sellPic":"0.9246", "color":"Red", "date":"2016-07-29", "datatime":"09:40:54" }, ...... } ] }
示例代碼 基于php的貨幣匯率接口調(diào)用代碼實(shí)例
<!--?php // +---------------------------------------------------------------------- // | JuhePHP [ NO ZUO NO DIE ] // +---------------------------------------------------------------------- // | Copyright (c) 2010-2015 http://juhe.cn All rights reserved. // +---------------------------------------------------------------------- // | Author: Juhedata <info@juhe.cn--> // +---------------------------------------------------------------------- //---------------------------------- // 貨幣匯率調(diào)用示例代碼 - 聚合數(shù)據(jù) // 在線接口文檔:http://www.juhe.cn/docs/23 //---------------------------------- header('Content-type:text/html;charset=utf-8'); //配置您申請的appkey $appkey = "*********************"; //************1.人民幣牌價(jià)************ $url = "http://web.juhe.cn:8080/finance/exchange/rmbquot"; $params = array( "key" => $appkey,//APP Key "type" => "",//兩種格式(0或者1,默認(rèn)為0) ); $paramstring = http_build_query($params); $content = juhecurl($url,$paramstring); $result = json_decode($content,true); if($result){ if($result['error_code']=='0'){ print_r($result); }else{ echo $result['error_code'].":".$result['reason']; } }else{ echo "請求失敗"; } //************************************************** //************2.外匯匯率************ $url = "http://web.juhe.cn:8080/finance/exchange/frate"; $params = array( "key" => $appkey,//APP Key "type" => "",//兩種格式(0或者1,默認(rèn)為0) ); $paramstring = http_build_query($params); $content = juhecurl($url,$paramstring); $result = json_decode($content,true); if($result){ if($result['error_code']=='0'){ print_r($result); }else{ echo $result['error_code'].":".$result['reason']; } }else{ echo "請求失敗"; } //************************************************** /** * 請求接口返回內(nèi)容 * @param string $url [請求的URL地址] * @param string $params [請求的參數(shù)] * @param int $ipost [是否采用POST形式] * @return string */ function juhecurl($url,$params=false,$ispost=0){ $httpInfo = array(); $ch = curl_init(); curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 ); curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' ); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 ); curl_setopt( $ch, CURLOPT_TIMEOUT , 60); curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true ); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); if( $ispost ) { curl_setopt( $ch , CURLOPT_POST , true ); curl_setopt( $ch , CURLOPT_POSTFIELDS , $params ); curl_setopt( $ch , CURLOPT_URL , $url ); } else { if($params){ curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params ); }else{ curl_setopt( $ch , CURLOPT_URL , $url); } } $response = curl_exec( $ch ); if ($response === FALSE) { //echo "cURL Error: " . curl_error($ch); return false; } $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE ); $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) ); curl_close( $ch ); return $response; }
基于Python的貨幣匯率接口調(diào)用代碼實(shí)例
#!/usr/bin/python # -*- coding: utf-8 -*- import json, urllib from urllib import urlencode #---------------------------------- # 貨幣匯率調(diào)用示例代碼 - 聚合數(shù)據(jù) # 在線接口文檔:http://www.juhe.cn/docs/23 #---------------------------------- def main(): #配置您申請的APPKey appkey = "*********************" #1.人民幣牌價(jià) request1(appkey,"GET") #2.外匯匯率 request2(appkey,"GET") #人民幣牌價(jià) def request1(appkey, m="GET"): url = "http://web.juhe.cn:8080/finance/exchange/rmbquot" params = { "key" : appkey, #APP Key "type" : "", #兩種格式(0或者1,默認(rèn)為0) } params = urlencode(params) if m =="GET": f = urllib.urlopen("%s?%s" % (url, params)) else: f = urllib.urlopen(url, params) content = f.read() res = json.loads(content) if res: error_code = res["error_code"] if error_code == 0: #成功請求 print res["result"] else: print "%s:%s" % (res["error_code"],res["reason"]) else: print "request api error" #外匯匯率 def request2(appkey, m="GET"): url = "http://web.juhe.cn:8080/finance/exchange/frate" params = { "key" : appkey, #APP Key "type" : "", #兩種格式(0或者1,默認(rèn)為0) } params = urlencode(params) if m =="GET": f = urllib.urlopen("%s?%s" % (url, params)) else: f = urllib.urlopen(url, params) content = f.read() res = json.loads(content) if res: error_code = res["error_code"] if error_code == 0: #成功請求 print res["result"] else: print "%s:%s" % (res["error_code"],res["reason"]) else: print "request api error" if __name__ == '__main__': main()
基于JAVA的貨幣匯率接口調(diào)用代碼實(shí)例
import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; import net.sf.json.JSONObject; /** *貨幣匯率調(diào)用示例代碼 - 聚合數(shù)據(jù) *在線接口文檔:http://www.juhe.cn/docs/23 **/ public class JuheDemo { public static final String DEF_CHATSET = "UTF-8"; public static final int DEF_CONN_TIMEOUT = 30000; public static final int DEF_READ_TIMEOUT = 30000; public static String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36"; //配置您申請的KEY public static final String APPKEY ="*************************"; //1.人民幣牌價(jià) public static void getRequest1(){ String result =null; String url ="http://web.juhe.cn:8080/finance/exchange/rmbquot";//請求接口地址 Map params = new HashMap();//請求參數(shù) params.put("key",APPKEY);//APP Key params.put("type","");//兩種格式(0或者1,默認(rèn)為0) try { result =net(url, params, "GET"); JSONObject object = JSONObject.fromObject(result); if(object.getInt("error_code")==0){ System.out.println(object.get("result")); }else{ System.out.println(object.get("error_code")+":"+object.get("reason")); } } catch (Exception e) { e.printStackTrace(); } } //2.外匯匯率 public static void getRequest2(){ String result =null; String url ="http://web.juhe.cn:8080/finance/exchange/frate";//請求接口地址 Map params = new HashMap();//請求參數(shù) params.put("key",APPKEY);//APP Key params.put("type","");//兩種格式(0或者1,默認(rèn)為0) try { result =net(url, params, "GET"); JSONObject object = JSONObject.fromObject(result); if(object.getInt("error_code")==0){ System.out.println(object.get("result")); }else{ System.out.println(object.get("error_code")+":"+object.get("reason")); } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { } /** * * @param strUrl 請求地址 * @param params 請求參數(shù) * @param method 請求方法 * @return 網(wǎng)絡(luò)請求字符串 * @throws Exception */ public static String net(String strUrl, Map params,String method) throws Exception { HttpURLConnection conn = null; BufferedReader reader = null; String rs = null; try { StringBuffer sb = new StringBuffer(); if(method==null || method.equals("GET")){ strUrl = strUrl+"?"+urlencode(params); } URL url = new URL(strUrl); conn = (HttpURLConnection) url.openConnection(); if(method==null || method.equals("GET")){ conn.setRequestMethod("GET"); }else{ conn.setRequestMethod("POST"); conn.setDoOutput(true); } conn.setRequestProperty("User-agent", userAgent); conn.setUseCaches(false); conn.setConnectTimeout(DEF_CONN_TIMEOUT); conn.setReadTimeout(DEF_READ_TIMEOUT); conn.setInstanceFollowRedirects(false); conn.connect(); if (params!= null && method.equals("POST")) { try (DataOutputStream out = new DataOutputStream(conn.getOutputStream())) { out.writeBytes(urlencode(params)); } } InputStream is = conn.getInputStream(); reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET)); String strRead = null; while ((strRead = reader.readLine()) != null) { sb.append(strRead); } rs = sb.toString(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { reader.close(); } if (conn != null) { conn.disconnect(); } } return rs; } //將map型轉(zhuǎn)為請求參數(shù)型 public static String urlencode(Map<String,Object>data) { StringBuilder sb = new StringBuilder(); for (Map.Entry i : data.entrySet()) { try { sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue()+"","UTF-8")).append("&"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return sb.toString(); } }
到此,相信大家對“如何實(shí)現(xiàn)免費(fèi)的貨幣匯率API”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。