-
Notifications
You must be signed in to change notification settings - Fork 340
/
Copy pathOkTrade.php
126 lines (89 loc) · 3.52 KB
/
OkTrade.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<?php
$Ok=new OkAPI ;
//var_dump( $Ok->Fund());
//var_dump( $Ok->CancelOrder(23456));
var_dump( $Ok->Trade(1000,0.11,"buy"));
//var_dump($Ok->GetOrder("2345"));
class OkAPI {
var $partner=// "45678912";
var $secretKey ="";//"19535CF3D949D4EF56F8D3D4ED78C505";
protected function ok_query($parameters, $url){
$post_data =http_build_query($parameters, '', '&');
echo $post_data;
$sign=md5 ($post_data.$this->secretKey );
$sign=strtoupper($sign);
var_dump($sign);
$post="partner=".$this->partner."&sign=".$sign."&".$post_data;
var_dump($post);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$res = curl_exec($ch);
$res=json_decode ($res,true);
return $res;
}
function MarketDepth($N=5){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.okcoin.com/api/depth.do "); //国际站
//curl_setopt($ch, CURLOPT_URL, "https://www.okcoin.cn/api/depth.do "); //中国站
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res= curl_exec($ch);
$res=json_decode ($res,true);
$res_ask=array_reverse(array_slice($res["asks"] , -$N, $N));
$res_bid=array_slice($res["bids"] , 0, $N) ;
$ans=array("asks"=>$res_ask,"bids"=>$res_bid);
return $ans;
}
function Trade($Price,$Amount,$Direction){
$parameters=array("amount"=>$Amount,"partner"=>$this->partner,"rate"=>$Price,"symbol"=>'btc_cny',
"type"=>strtolower($Direction));
$url= 'https://www.okcoin.com/api/trade.do'; //国际站
// $url= 'https://www.okcoin.cn/api/trade.do'; //中国站
$res=$this->ok_query($parameters, $url);
return $res;
}
function CancelOrder($OrderID){
$parameters=array("order_id"=>$OrderID,"partner"=>$this->partner,"symbol"=>"btc_cny");
$url='https://www.okcoin.com/api/cancelorder.do'; //国际站
// $url='https://www.okcoin.cn/api/cancelorder.do'; //中国站
$res=$this->ok_query($parameters, $url);
return $res;
}
function Fund()
{
$parameters=array("partner"=>$this->partner);
$url='https://www.okcoin.com/api/userinfo.do'; //国际站
// $url='https://www.okcoin.cn/api/userinfo.do'; //中国站
$res=$this->ok_query($parameters, $url);
//var_dump($res);
if($res["result"] ){
$res=array("result"=>true, "Frozen"=>array("BTC"=>$res["info"]["funds"]["freezed"]["btc"], "CNY"=>$res["info"]["funds"][ "freezed"]["cny"]),
"Free"=>array("BTC"=>$res["info"]["funds"]["free"]["btc"], "CNY"=>$res["info"]["funds"]["free"]["cny"]) );
return $res;
}
$res=array("result"=>false);
return $res;
}
function GetOrder($OrderID){
$parameters=array("order_id"=>$OrderID,"partner"=>$this->partner,"symbol"=>"btc_cny");
$url= 'https://www.okcoin.com/api/getorder.do'; //国际站
//$url= 'https://www.okcoin.cn/api/getorder.do'; //中国站
$res=$this->ok_query($parameters, $url);
return $res;
}
}
?>
<body>
</body>
</html>