4th Street Bar Hive-Bar

Hive-Bar Powered by Hive beta-fdb5b5b

Community post

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

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(accesstoken,access_token,api_secret) {
			this->accesstoken=this->access_token =access_token;
			this->apisecret=this->api_secret =api_secret;
		}
			
		private function query(array req=array(),req = array(),trading_url_extra=null) {
			// API settings
			key=key =this->access_token;
			secret=secret =this->api_secret;
		 
			// generate a nonce to avoid problems with 32bit systems
			$mt = explode(' ', microtime());
			nonce=nonce =mt[1].substr($mt[0], 2, 6);

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

			// generate the POST data string
			postdata=httpbuildquery(post_data = http_build_query(req, '', '&');
			signature=hashhmac('sha512',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->tradingurl=this->trading_url =this->trading_url .'/'.$trading_url_extra;
			
			curl_setopt(ch,CURLOPTURL,ch, CURLOPT_URL,this->trading_url);
			curl_setopt(ch,CURLOPTPOSTFIELDS,ch, CURLOPT_POSTFIELDS,post_data);
			curl_setopt(ch,CURLOPTHTTPHEADER,ch, CURLOPT_HTTPHEADER,headers);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);


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

			if (res===false)thrownewException('Curlerror:'.curlerror(res === false) throw new Exception('Curl error: '.curl_error(ch));
			//echo $res;
			dec=jsondecode(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=streamcontextcreate(context = stream_context_create(opts);
			feed=filegetcontents(feed = file_get_contents(URL, false, $context);
			json=jsondecode(json = json_decode(feed, true);
			return $json;
		}
		
		public function get_balances() {
			return $this->query( 
				array(
					'command' => 'returnBalances'
				)
				,
					'account/balance/'
			);
		}
		

	}
?>
20 upvotes $1.52

Replies (6)

Review before signing

Posting as . Signing with . Keychain permission: Posting. Hive Keychain will ask you to approve this action next.


  
Technical details

Operation fingerprint: