개발/PHP
스누피(snoopy) 를 활용 하여 코인 거래소 API(json) DB(mysql)에 담기
핫펍co
2018. 6. 12. 16:19
스누피(snoopy) 다운 및 이용 방법은 아래 페이지에서 먼저 확인 부탁 드립니다.
http://hatpub.tistory.com/39?category=988325
<? include_once 'Snoopy.class.php'; $snoopy = new snoopy; $snoopy->fetch("https://bittrex.com/api/v1.1/public/getmarketsummaries"); $t = $snoopy->results; preg_match_all("|MarketName\":\"(.*)\",\"High\"|U", $t, $name, PREG_SET_ORDER); preg_match_all("|\"BaseVolume\":(.*),\"TimeStamp\":|U", $t, $volume, PREG_SET_ORDER); preg_match_all("|Last\":(.*),\"BaseVolume\"|U", $t, $price, PREG_SET_ORDER); ?>
업로드된 Snopy.class.php 파일을 먼저 인클루드 한뒤,
Json 형태의 API 사이트를 fetch 에 담아 $t 변수에 넣습니다.
preg_match_all 함수를 이용 하여 MarketName(코인명) 은 $name 변수에,
Last(최근거래가격)은 $price 변수에, BaseVolume(24시간비트볼륨) 은 $volume 변수에 항목의 모든 값을 2차 배열로 담아 옵니다.
↓ https://bittrex.com/api/v1.1/public/getmarketsummaries 접속 시 Json 형태의 값으로 아래 이미지와 같이 표시 됩니다.
<? $int = 0; $var = "insert into bittrex(name, price, volume) values"; WHILE($int < count($name)){ if($int == 0){ $var = $var."(".$name[$int][1]."',".$price[$int][1].",".$volume[$int][1].")"; }else{ $var = $var.",(".$name[$int][1]."',".$price[$int][1].",".$volume[$int][1].")"; } $int++; } mysqli_query($conn, $var); ?>
$name, $price, $volume 에 담아온 값을 WHILE 을 사용하여 쿼리문을 작성 합니다.
마지막으로 mysql insert 문을 실행 시키면 값이 DB에 저장 되게 됩니다.