수출입은행에서 api
본문
수출입은행에서 api 를 받아와서 김치프리미엄을 계산하려고 하는데요
김치프리미언:4.35% 이런식으로 출력 되게 하려고 하는데
출력값이 김치 프리미엄: 142298.6% 이런식으로 나오는데 모가 잘못된것지 못찾겠어요 ㅠㅠ 모가 잘못된걸까요 도와주세요
<?php
$apiKey = getenv('KOREAEXIM_API_KEY');
if (!$apiKey) {
$apiKey =
}
// 오늘 날짜를 YYYYMMDD 형식으로 가져옵니다.
$searchDate = date('Ymd');
function get_exchange_rates($apiKey, $searchDate) {
$url = "https://www.koreaexim.go.kr/site/program/financial/exchangeJSON";
$query = http_build_query(array(
"searchdate" => $searchDate,
"data" => "AP01"
));
$full_url = $url . "?authkey=" . $apiKey . "&" . $query;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $full_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
$data = json_decode($response, true);
foreach ($data as $item) {
if ($item['cur_unit'] == 'USD') {
return floatval($item['deal_bas_r']); // 미국 달러 환율 반환
}
}
return null; // 미국 달러 환율 정보가 없는 경우
}
function get_binance_price($symbol) {
$url = "https://api.binance.com/api/v3/ticker/price?symbol=" . $symbol;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
return null;
}
curl_close($ch);
$data = json_decode($response, true);
return isset($data['price']) ? floatval($data['price']) : null;
}
function get_upbit_price($symbol) {
$url = "https://api.upbit.com/v1/ticker?markets=" . $symbol;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
return null;
}
curl_close($ch);
$data = json_decode($response, true);
if (!empty($data) && is_array($data)) {
return isset($data[0]['trade_price']) ? floatval($data[0]['trade_price']) : null;
}
return null;
}
function calculate_kimchi_premium($apiKey, $searchDate) {
$usd_krw_rate = get_exchange_rates($apiKey, $searchDate);
if (!$usd_krw_rate) {
return "환율 정보를 가져오는데 실패했습니다.";
}
$binance_btc_price = get_binance_price("BTCUSDT");
if (!$binance_btc_price) {
return "바이낸스 가격 정보를 가져오는데 실패했습니다.";
}
$upbit_btc_price = get_upbit_price("KRW-BTC");
if (!$upbit_btc_price) {
return "업비트 가격 정보를 가져오는데 실패했습니다.";
}
// '1 USD당 몇 KRW' 형태로 환율이 제공된다면, KRW를 USD로 변환하는 계산은 아래와 같습니다.
$upbit_btc_price_usd = $upbit_btc_price / $usd_krw_rate;
// 김치 프리미엄 계산
if ($binance_btc_price == 0) {
return "분모가 0입니다.";
}
$kimchi_premium = (($upbit_btc_price_usd - $binance_btc_price) / $binance_btc_price) * 100;
return round($kimchi_premium, 1); // 소수점 한 자리까지 반올림하여 반환
}
$result = calculate_kimchi_premium($apiKey, $searchDate);
// 결과가 숫자인 경우와 아닌 경우를 구분하여 출력
if (is_numeric($result)) {
echo "김치 프리미엄: " . $result . "%\n";
} else {
echo $result;
}
?>
답변 1
34번 라인을 다음과 같이 수정 해주세요
foreach ($data as $item) {
if ($item['cur_unit'] == 'USD') {
$rate = str_replace(',', '', $item['deal_bas_r']); // 콤마 제거
return floatval($rate); // 수정된 미국 달러 환율 반환
}
}
!-->