今天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; }
上述方法总感觉差强人意,其他的一些基本上都要给钱或者次数限制;
以下为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 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦