coinone.co.kr 에서 제공하는 v2 api 를 이용해 거래를 하기 위한 php class를 공유하고자 합니다.

closetoya(47)
Published in
#kr
Words
303
Reading
2 min
Listen
Play
9y

coinone.co.kr 에서 제공하는 api를 이용해 거래를 하기 위한 php class를 공유하고자 합니다.

기초 뼈대는 폴로닉스에서 제공하는 php용 api를 이용해서 coinone 사이트에서 이용 가능하도록 수정했습니다.
출처 https://pastebin.com/iuezwGRZ

coinone에서 access_token , api_secret 을 발급받으시고 ( https://coinone.co.kr/developer/app/ )
제일 아래 프로그램을 api.php 저장한뒤

require 'api.php';
$c = new coinone('access_token' , 'api_secret');
print_R($c->get_balances());

이런식으로 사용하시면 됩니다. 지금은 단순히 계정 잔고를 불러오는 부분만 있습니다.
기타 명령어들은 http://doc.coinone.co.kr/#api-_ 를 참조하셔서 사용하시면 됩니다.

코인원에서 제공하는 예제가 js, python 밖에 없어서 php 로 만들어 보다가 공유차 글을 올립니다.

이상입니다.

이하 api class

<?php

    class coinone {
        protected $access_token;
        protected $api_secret;
        protected $trading_url = "https://api.coinone.co.kr/v2";
        protected $public_url = "https://api.coinone.co.kr/";
        
        public function __construct($access_token, $api_secret) {
            $this->access_token = $access_token;
            $this->api_secret = $api_secret;
        }
            
        private function query(array $req = array(), $trading_url_extra=null) {
            // API settings
            $key = $this->access_token;
            $secret = $this->api_secret;
         
            // generate a nonce to avoid problems with 32bit systems
            $mt = explode(' ', microtime());
            $nonce = $mt[1].substr($mt[0], 2, 6);

            // generate the extra headers
            $req['access_token'] = $key;
            $req['nonce'] = $nonce;
            
            $payload = json_encode($req);
            $payload  = base64_encode($payload);

            // generate the POST data string
            $post_data = http_build_query($req, '', '&');
            $signature = hash_hmac('sha512', $payload, $secret);
            
            $headers = array(
                'content-type: application/json',
                "X-COINONE-PAYLOAD: $payload",
                "X-COINONE-SIGNATURE: $signature",
            );

            // curl handle (initialize if required)
            static $ch = null;
            if (is_null($ch)) {
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_USERAGENT, 
                    'Mozilla/4.0 (compatible; coinone PHP bot; '.php_uname('a').'; PHP/'.phpversion().')'
                );
            }
            if (!is_null($trading_url_extra) )
                $this->trading_url = $this->trading_url .'/'.$trading_url_extra;
            
            curl_setopt($ch, CURLOPT_URL, $this->trading_url);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);


            // run the query
            $res = curl_exec($ch);

            if ($res === false) throw new Exception('Curl error: '.curl_error($ch));
            //echo $res;
            $dec = json_decode($res, true);
            if (!$dec){
                //throw new Exception('Invalid data: '.$res);
                return false;
            }else{
                return $dec;
            }
        }
        
        protected function retrieveJSON($URL) {
            $opts = array('http' =>
                array(
                    'method'  => 'GET',
                    'timeout' => 10 
                )
            );
            $context = stream_context_create($opts);
            $feed = file_get_contents($URL, false, $context);
            $json = json_decode($feed, true);
            return $json;
        }
        
        public function get_balances() {
            return $this->query( 
                array(
                    'command' => 'returnBalances'
                )
                ,
                    'account/balance/'
            );
        }
        

    }
?>