今天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'];
}
网友评论