Programming/PHP
[PHP] 신종 코로나 바이러스 실시간 현황 확인 사이트 제작(Open-Source, 2020-02-05)
Talking1258
2020. 2. 5. 16:11
이 소스는 오래되었습니다. 최신버전으로 확인 바랍니다. https://blog.mcv.kr/26
요즘 유행하고 있는 신종 코로나 바이러스에 대한 감염자 수 등의 기본정보를 확인할 수 있습니다.
이것은 https://mcv.kr/corona 에서 확인이 가능합니다.
새로고침 없이 자동적으로 10분마다 데이터가 업데이트 됩니다.
index.php
<html>
<head>
<title> 신종 코로나 바이러스 실시간 정보</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<script>
window.onresize = function(event) {
winsize();
};
window.onload = function() {
winsize();
}
function winsize() {
if (window.innerWidth > 780) {
document.getElementById('board1').style.styleFloat = 'left';
document.getElementById('board1').style.cssFloat = 'left';
document.getElementById('board1').style.width = '50%';
document.getElementById('board2').style.styleFloat = 'right';
document.getElementById('board2').style.cssFloat = 'right';
document.getElementById('board2').style.width = '50%';
if (window.innerWidth > 1330) {
document.getElementById('board3').style.height = '220px';
} else {
document.getElementById('board3').style.height = '260px';
}
document.getElementById('board4').style.height = '300px';
document.getElementById('titlenewline').innerHTML = '';
document.getElementById('mainframe1').style.height = '300px';
} else {
document.getElementById('board1').style.styleFloat = 'none';
document.getElementById('board1').style.cssFloat = 'none';
document.getElementById('board1').style.width = '100%';
document.getElementById('board2').style.styleFloat = 'none';
document.getElementById('board2').style.cssFloat = 'none';
document.getElementById('board2').style.width = '100%';
document.getElementById('board3').style.height = '375px';
document.getElementById('board4').style.height = '400px';
document.getElementById('titlenewline').innerHTML = '<br>';
document.getElementById('mainframe1').style.height = '530px';
}
}
function getuuid() {
var request = new XMLHttpRequest();
request.onload = function () {
var apidata = JSON.parse(request.responseText);
document.getElementById("time").innerText = apidata['time'];
document.getElementById("korinfected").innerText = apidata['korinfected'];
document.getElementById("kordeathtotal").innerText = apidata['kordeathtotal'];
document.getElementById("kcdcyellow").innerText = apidata['kcdcyellow'];
// document.getElementById("kcdcgreen").innerText = apidata['kcdcgreen'];
// document.getElementById("kcdcanalysis").innerText = apidata['kcdcanalysis'];
document.getElementById("globalinfected").innerText = apidata['globalinfected'];
document.getElementById("globalcountries").innerText = apidata['globalcountries'];
document.getElementById("globaldeathtotal").innerText = apidata['globaldeathtotal'];
document.getElementById("deathrating").innerText = apidata['deathrating'];
setTimeout(function() {
getuuid();
}, 60000);
}
request.open('GET', "https://mcv.kr/corona/api.php" , true);
request.send();
}
getuuid();
</script>
<body style="margin: 0;">
<center>
<div style="background-color: lightgray;">
<strong><font size="5">신종 코로나 바이러스</font>
<process id="titlenewline"></process>
<font size="5">실시간 업데이트 정보</font></strong>
<br>
<font size="3"><info id="time">서버에서 데이터를 불러오는 중 입니다.</info></font>
</div>
</center>
<div id="mainframe1" style="width: 100%; height: 300px;">
<div id="board1" style="float: left; width: 50%;">
<center><br><strong><font size="5">국내 바이러스 감염 상황</font></strong><br></center>
<br>
<center>
<strong style="width: 130px; display:inline-block"><font size="4">정부 확진 집계: </font></strong>
<info id="kcdcyellow" style="width: 70px; display:inline-block;">정보 없음</info>
<!-- <br>
<strong style="width: 130px; display:inline-block"><font size="4">격리해제자 수: </font></strong>
<info id="kcdcgreen" style="width: 70px; display:inline-block;">정보 없음</info>
<br>
<strong style="width: 130px; display:inline-block"><font size="4">현재 분석 수: </font></strong>
<info id="kcdcanalysis" style="width: 70px; display:inline-block;">정보 없음</info> -->
<br>
<strong style="width: 130px; display:inline-block"><font size="4">확진자 수: </font></strong>
<info id="korinfected" style="width: 70px; display:inline-block;">정보 없음</info>
<br>
<strong style="width: 130px; display:inline-block"><font size="4">사망자 수: </font></strong>
<info id="kordeathtotal" style="width: 70px; display:inline-block;">정보 없음</info>
<br>
<p>자료 출처: Wikipedia US, KCDC 질병관리본부</p>
</center>
</div>
<div id="board2" style="float: right; width: 50%;">
<center><br><strong><font size="5">국제 바이러스 감염 상황</font></strong><br></center>
<br>
<center>
<strong style="width: 130px; display:inline-block"><font size="4">보고 국가 수: </font></strong>
<info id="globalcountries" style="width: 70px; display:inline-block;">정보 없음</info>
<br>
<strong style="width: 130px; display:inline-block"><font size="4">확진자 수: </font></strong>
<info id="globalinfected" style="width: 70px; display:inline-block;">정보 없음</info>
<br>
<strong style="width: 130px; display:inline-block"><font size="4">사망자 수: </font></strong>
<info id="globaldeathtotal" style="width: 70px; display:inline-block;">정보 없음</info>
<br>
<strong style="width: 130px; display:inline-block"><font size="4">치사율: </font></strong>
<info id="deathrating" style="width: 70px; display:inline-block;">정보 없음</info>
<br>
<p>자료 출처: Wikipedia US</p>
</center>
</div>
</div>
<div id="board3" style="width: 100%; height: 220px;">
<center><br><strong><font size="5">코로나 바이러스?</font></strong><br></center>
<br>
<center>
<div style="width: 60%;">2019년 12월 중국 우한에서 발생한 신종 코로나바이러스 감염증의 원인 바이러스(병원체)로, 인체 감염 7개 코로나바이러스 중 하나다. 신종 코로나바이러스는 감염자의 비말(침방울)이 호흡기나 눈·코·입의 점막으로 침투될 때 전염된다. 감염되면 약 2~14일(추정)의 잠복기를 거친 뒤 발열(37.5도) 및 기침이나 호흡곤란 등 호흡기 증상, 폐렴이 주 증상으로 나타나지만 무증상 감염 사례도 드물게 나오고 있다. </div>
<p>자료 출처: <a style="text-decoration: none;" href="https://terms.naver.com/entry.nhn?docId=5912275&cid=43667&categoryId=43667">네이버 지식백과</a></p>
</center>
</div>
<div id="board4" style="width: 100%; height: 300px;">
<center><br><strong><font size="5">예방 수칙</font></strong><br></center>
<br>
<center>
<div style="width: 60%;"><strong>신종 코로나바이러스감염증 예방 국민 행동수칙</strong>
<br>-흐르는 물에 30초 이상 손 씻기.
<br>-옷소매로 가리고 기침하기.
<br>-기침 등 호흡기 증상이 있을 경우 마스크 착용 (의료기관 방문 시 반드시)
<br>-후베이성 등 중국 방문 후 의심증상 발생 시 관할보건소 또는 1339 문의
<br>-중국 여행력을 의료진에게 알려주기</div>
<p>자료 출처: <a style="text-decoration: none;" href="https://www.cdc.go.kr/board/board.es?mid=a20501000000&bid=0015&list_no=365948&act=view">KCDC 질병관리본부</a>, <a style="text-decoration: none;" href="https://search.naver.com/search.naver?sm=tab_hty.top&where=nexearch&query=%EC%8B%A0%EC%A2%85+%EC%BD%94%EB%A1%9C%EB%82%98%EB%B0%94%EC%9D%B4%EB%9F%AC%EC%8A%A4+%EA%B0%90%EC%97%BC%EC%A6%9D">네이버 검색 안내</a></p>
</center>
</div>
<div id="board5" style="width: 100%; height: 300px;">
<center><br><strong><font size="5">의료 또는 관련기관 연락처</font></strong><br></center>
<br>
<center>
<div style="width: 60%;">
<br>지역 콜센터(☎지역번호+120)
<br>질병관리본부 상담센터(1339)
<br><a style="text-decoration: none;" href="http://www.mohw.go.kr/react/popup_200128.html">선별 진료소 찾기(여기를 누르세요)</a></div>
</center>
</div>
</body>
</html>
api.php
<?php
header("Access-Control-Allow-Origin: *");
// Cache
$data = file_get_contents('cache.txt');
$jsondata = file_get_contents('cache.json');
if((int)$data + 600 > time()) {
print($jsondata);
exit;
}
// Process
$content = file_get_contents('https://en.wikipedia.org/wiki/2019%E2%80%9320_Wuhan_coronavirus_outbreak');
$options = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10\r\n" // i.e. An iPad
)
);
$context = stream_context_create($options);
$kcdc = file_get_contents('http://www.cdc.go.kr/contents.es?mid=a21111050500', false, $context);
$info = array();
$info['time'] = date('Y.m.d H:i');
// Wikipedia US
$korInfected = explode('</span>South Korea', $content)[1];
$korInfected = explode('<td align="right">', $korInfected)[1];
$korInfected = explode('</td>', $korInfected)[0];
$korInfected = str_replace(PHP_EOL, '', $korInfected);
$korInfected = str_replace('</td>', '', $korInfected);
$info['korinfected'] = $korInfected;
$korDeathtotal = explode('</span>South Korea', $content)[1];
$korDeathtotal = explode('<td align="right">', $korDeathtotal)[2];
$korDeathtotal = explode('</td>', $korDeathtotal)[0];
$korDeathtotal = str_replace(PHP_EOL, '', $korDeathtotal);
$korDeathtotal = str_replace('</td>', '', $korDeathtotal);
$korDeathtotal = explode('</tr>', $korDeathtotal)[0];
$info['kordeathtotal'] = $korDeathtotal;
$globalcountries = explode('<tr class="sortbottom">'.PHP_EOL.'<th>', $content)[1];
$globalcountries = explode(' territories', $globalcountries)[0];
$info['globalcountries'] = $globalcountries;
$globalInfected = explode('<tr class="sortbottom">', $content)[1];
$globalInfected = explode('<th align="right">', $content)[1];
$globalInfected = str_replace(PHP_EOL, '', $globalInfected);
$globalInfected = str_replace('</th>', '', $globalInfected);
$info['globalinfected'] = $globalInfected;
$globalDeathtotal = explode('<tr class="sortbottom">', $content)[1];
$globalDeathtotal = explode('<th align="right">', $globalDeathtotal)[2];
$globalDeathtotal = explode('</th>', $globalDeathtotal)[0];
$globalDeathtotal = str_replace(PHP_EOL, '', $globalDeathtotal);
$info['globaldeathtotal'] = $globalDeathtotal;
// KCDC meterial
$kcdcyellow = explode('<strong>(확진환자)</strong> ', $kcdc)[1];
$kcdcyellow = explode('명', $kcdcyellow)[0];
$info['kcdcyellow'] = $kcdcyellow;
$kcdcgreen = explode(' (격리해제 ', $kcdc)[1];
$kcdcgreen = explode('명', $kcdcgreen)[0];
$info['kcdcgreen'] = $kcdcgreen;
$kcdcanalysis = explode('명, 검사 중 ', $kcdc)[1];
$kcdcanalysis = explode('명', $kcdcanalysis)[0];
$info['kcdcanalysis'] = $kcdcanalysis;
// rating
$deathrating = (int)$globalDeathtotal / (int)str_replace(',', '', $globalInfected);
$info['deathrating'] = round($deathrating, 5) * 100 . '%';
print(json_encode($info));
file_put_contents('cache.txt', time());
file_put_contents('cache.json', json_encode($info));
웹 서버 사본 다운로드