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

in #kr7 years ago

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/'
            );
        }
        

    }
?>
Sort:  

이런 코드 공유는 참 감사한 것 같습니다!
약간 쓸데없는 참견을 조금 하자면.. github의 gist같은 서비스에 코드를 올리 신 후,
해당 내용을 steemit에 포스팅하시면, 내용과 코드파일을 분리하여 접근성과 가독성을 높일 수 있을 것 같습니다!

-- 상단에 pastebin으로 링크를 걸어두셨군요! 제가 헛소리를 했습니다! :D

좋은 정보 감사합니다. github쪽은 생각못했네요.
steemit 입력기가 익숙치가 않아서 편집하기가 어렵네요 :)

능력자분들은 추천입니다.

Congratulations @closetoya! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

와~ 좋은정보네요~ ^^ 감사해요~^

대단하시네요!! 앞으로도 좋은 정보 부탁드립니다!

Coin Marketplace

STEEM 0.27
TRX 0.13
JST 0.032
BTC 61451.22
ETH 2929.56
USDT 1.00
SBD 3.65