亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

貨幣匯率計算

標簽:
Java

今天steam游戏开发时,游戏内置商店商品价格定价只有人民币(CNY);而没有其他币种价格的设置,于是做了一个简单的货币兑换。
依照:百度汇率计算,修改了url参数

function rateAmount($currency, $price){        //$baseCurrency:基本币种    $currency:转换币种
        //币种可识别中文和ISO 4217 currency code
        $query = urlencode('1' . $baseCurrency . '=' . $currency);
        $url = 'https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=' .
            $query .            '&co=&resource_id=6017&t=' .
            time() * 1000 .            '&cardId=6017&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=baidu&' .            'cb=jQuery110209644633764682506_1534919455172&_=' .
            time() * 1000;
        $response = file_get_contents($url);        //返回值是gbk格式的,转一下
        $response = iconv('GBK', 'UTF-8', $response);
        $json = substr($response, strpos($response, '(') + 1);
        $data = json_decode(substr($json, 0, -2), true);

        $result = $data['data'][0]['content1'];
        $rate = substr($result, strpos($result, '=') + 1, 5);        //steam要求价格必须为整元且单位为分,比如1000而没有1050。
        $gameAmount = ceil($price / 100 * $rate) * 100;        return (int)$gameAmount;
}
  1. 上述方法总感觉差强人意,其他的一些基本上都要给钱或者次数限制;
    以下为steam官方api方法:
    文档地址:http://steam.steamlytics.xyz/api

function rateAmount($currency, $amount){
        $baseUrl = 'http://api.steam.steamlytics.xyz/v1/currencies/';
        $apiKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

        $responseCurrencies = Cache::remember('currencies', 10, function () use ($baseUrl, $apiKey) {
            $url = $baseUrl . '?key=' . $apiKey;
            $response = $this->client->request('GET', $url);
            $json = json_decode($response->getBody(), true);            return $json['currencies'];
        });

        $gameCurrencyId = 0;
        $currencyId = 0;        foreach ($responseCurrencies as $id => $responseCurrency) {            if ($responseCurrency['code'] == 'CNY') {
                $gameCurrencyId = $id;
            }            if ($responseCurrency['code'] == $currency) {
                $currencyId = $id;
            }
        }

        $rateUrl = $baseUrl . 'convert/' . $amount . '/' . $gameCurrencyId . '/' .
            $currencyId . '?key=' . $apiKey;
        $rateResponse = $this->client->request('GET', $rateUrl);
        $data = json_decode($rateResponse->getBody(), true);

        $payAmount = $data['amount'];
}



作者:愛餘生sb
链接:https://www.jianshu.com/p/e0f4028a8485


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消