테스트 사이트 - 개발 중인 베타 버전입니다

php json으로 api 받아오는것 질문드립니다. 채택완료

마린1 8년 전 조회 3,374

빗썸이라는 가상화폐 거래소에서 public api의 정보를 받아 그누보드에 삽입하고 싶습니다.


아래는 api 주소입니다.


https://api.bithumb.com/public/ticker/ALL">https://api.bithumb.com/public/ticker/ALL 


json으로 받아와야 하는것 같은데 json이 처음이라 어떻게 코드를 넣어도 가져오질 않네요.


어떻게해야 값을 받을수있을까요?


json은 따로 플로그인이 있어야 작동을 하나요?

댓글을 작성하려면 로그인이 필요합니다.

답변 1개

채택된 답변
+20 포인트
8년 전

json_decode() 를 이용하면 됩니다. http://php.net/manual/en/function.json-decode.php">http://php.net/manual/en/function.json-decode.php


텍스트를 가져온 후라면, (ex. $contents)

$array = json_decode($contents, true); 

과 같은 형태로 배열에 저장할 수 있습니다.


예제 코드입니다.

</p><p><?php </p><p>
</p><p>$url = '<a href="<a href="https://api.bithumb.com/public/ticker/ALL" target="_blank" rel="noopener noreferrer">https://api.bithumb.com/public/ticker/ALL</a>"><a href="https://api.bithumb.com/public/ticker/ALL" target="_blank" rel="noopener noreferrer">https://api.bithumb.com/public/ticker/ALL</a></a>';</p><p>
</p><p>$ch = curl_init();</p><p>curl_setopt ($ch, CURLOPT_URL, $url);</p><p>curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);</p><p>curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);</p><p>$contents = curl_exec($ch);</p><p>curl_close($ch);</p><p>
</p><p>$array = json_decode($contents, true);</p><p>
</p><p>print_r($array);</p><p>

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인