为了账号安全,请及时绑定邮箱和手机立即绑定

货币汇率计算

标签:
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
提交
取消