forked from tk1024/Zaif4PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Zaif.php
149 lines (126 loc) · 3.25 KB
/
Zaif.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
use WebSocket\Client;
class Zaif {
const PUBLIC_BASE_URL = "https://api.zaif.jp/api/1";
const TRADE_BASE_URL = "https://api.zaif.jp/tapi";
const STREAMING_BASE_URL = "ws://api.zaif.jp:8888/stream";
private $key;
private $secret;
private $nonce;
public function __construct($key, $secret) {
$this->key = $key;
$this->secret = $secret;
$this->nonce = time();
}
public static function pub($endpoint, $prm) {
switch ($endpoint) {
case 'last_price':
case 'ticker':
case 'trades':
case 'depth':
break;
default:
throw new Exception('Argument has not been set.');
break;
}
switch ($prm) {
case 'btc_jpy':
case 'mona_jpy':
case 'mona_btc':
break;
default:
throw new Exception('Argument has not been set.');
break;
}
$url = self::PUBLIC_BASE_URL.'/'.$endpoint.'/'.$prm;
$data = self::get($url);
$data = json_decode( $data );
return $data;
}
public function trade($method, $prms=null) {
switch ($method) {
case 'get_info':
case 'get_info2':
case 'get_personal_info':
case 'trade_history':
case 'active_orders':
case 'trade' :
case 'cancel_order' :
case 'withdraw' :
case 'deposit_history' :
case 'withdraw_history' :
break;
default:
throw new Exception('Argument has not been set.');
break;
}
$postdata = array( "nonce" => $this->nonce++, "method" => $method );
if( !empty( $prms ) ) {
$postdata = array_merge( $postdata, $prms );
}
$postdata_query = http_build_query( $postdata );
$sign = hash_hmac( 'sha512', $postdata_query, $this->secret);
$header = array( "Sign: {$sign}", "Key: {$this->key}", );
$data = self::post( self::TRADE_BASE_URL, $header, $postdata_query );
$data = json_decode( $data );
return $data;
}
public static function streaming($prms, $callback) {
$file_path = dirname(__FILE__).'/vendor/autoload.php';
if (file_exists($file_path) && is_readable($file_path)) {
require_once $file_path ;
} else {
throw new Exception('You can not use Streaming API.You should check including libray.');
}
switch ($prms['currency_pair']) {
case 'btc_jpy':
case 'mona_jpy':
case 'mona_btc':
break;
default:
throw new Exception('Argument has not been set.');
return 0;
break;
}
$ws = self::STREAMING_BASE_URL.'?'.http_build_query($prms);
$client = new Client($ws);
while(true) {
try {
$json = $client->receive();
$data = json_decode($json);
$callback($data);
} catch (WebSocket\ConnectionException $e) {
$clinet = new Client($ws);
}
}
}
private static function get($url) {
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
);
curl_setopt_array($ch, $options);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
private static function post($url, $header, $postdata) {
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postdata,
CURLOPT_HTTPHEADER => $header,
);
curl_setopt_array($ch, $options);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
}