当サイトに記載されている会社名、製品名などは一般に各社または団体の商標または登録商標です。
当サイトの資料により直接的および間接的障害が生じても一切責任を負いません。
あらかじめご了承ください。
PHPを使ってZaif のtrades・全ての取引履歴を取得APIの使用例を以下に記します。
以下のバナーはZaif
へのリンクです。
公開APIなのでZaif に取引口座を開設していなくても使用することができます。
「trades・全ての取引履歴を取得」API仕様(Zaif APIページから抜粋)
http://techbureau-api-document.readthedocs.io/ja/latest/public/2_individual/5_trades.html
trades 全ての取引履歴を取得します。 リクエスト /trades/{currency_pair} 例. https://api.zaif.jp/api/1/trades/btc_jpy currency_pairに指定できる値は currency_pairs を参照してください。 パラメータ なし 戻り値 キー 詳細 型 date 取引日時 UNIX_TIMESTAMP price 取引価格 float amount 取引量 float tid 取引ID int currency_pair 通貨ペア str trade_type 取引種別
上記の指定するcurrency_pairsですが、以下のAPIサンプルコードを実行して取得したcurrency_pairsを使用しています。
GETなのでブラウザで全ての取引履歴を取得することができます。
以下は{currency_pair}にmona_btcを指定しています。
https://api.zaif.jp/api/1/trades/mona_btc
以下ブラウザから上記URLにアクセスしたときの出力です。
以下のバナーはZaif
へのリンクです。
PHPのサンプルコードは以下のようになります。
(btc_jpy通貨ペアで試すと返却される件数が150だったので上位15件表示するようになっています。)
<?php // API doc : http://techbureau-api-document.readthedocs.io/ja/latest/public/2_individual/5_trades.html // API url : https://api.zaif.jp/api/1/trades/{currency_pair} // trades api urr $zaif_api_url = "https://api.zaif.jp/api/1/trades/"; // proxy settings $proxy = ""; $proxy_port = ""; $currency_pairs = array( "bch_btc" ,"bch_jpy" ,"bitcrystals_btc" ,"bitcrystals_jpy" ,"btc_jpy" ,"cicc_btc" ,"cicc_jpy" ,"eth_btc" ,"eth_jpy" ,"fscc_btc" ,"fscc_jpy" ,"jpyz_jpy" ,"mona_btc" ,"mona_jpy" ,"ncxc_btc" ,"ncxc_jpy" ,"pepecash_btc" ,"pepecash_jpy" ,"sjcx_btc" ,"sjcx_jpy" ,"xcp_btc" ,"xcp_jpy" ,"xem_btc" ,"xem_jpy" ,"zaif_btc" ,"zaif_jpy" ); if (!check_arguments($argc, $argv, $currency_pairs)) { usage(); die(1); } // show currency pairs if ($argv[1] == "list") { print("- currency_pairs -" . PHP_EOL); foreach ($currency_pairs as $value) { printf("%s" . PHP_EOL, $value); } exit(0); } $currency = $argv[1]; $curl = curl_init(); if ($curl == FALSE) { fputs(STDERR, "[ERR] curl_init(): " . curl_error($curl) . PHP_EOL); die(1); } // curl set options curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); // set proxy server settings if (!empty($proxy) && !empty($proxy_port)) { curl_setopt($curl, CURLOPT_HTTPPROXYTUNNEL, 1); curl_setopt($curl, CURLOPT_PROXY, $proxy . ":" . $proxy_port); curl_setopt($curl, CURLOPT_PROXYPORT, $proxy_port); } // call trades api curl_setopt($curl, CURLOPT_URL, $zaif_api_url . $currency); $response = curl_exec($curl); if ($response == FALSE) { fputs(STDERR, "[ERR] curl_exec(): " . curl_error($curl) . PHP_EOL); die(1); } // json decode $json_decode = json_decode($response, true); if ($json_decode == NULL) { fputs(STDERR, "[ERR] json_decode(): " . json_last_error_msg() . PHP_EOL); die(1); } curl_close($curl); //print_r($json_decode); $show_limit = 15; $i = 0; print("--- " . $currency . " ---" . PHP_EOL); foreach($json_decode as $trade) { $i++; printf("%s: %f\t%f\t%s" . PHP_EOL, date("Y-m-d H:i:s", $trade["date"]), $trade["price"], $trade["amount"], $trade["trade_type"]); if ($i > $show_limit) { break; } } exit(0); //------------------------------ // function //------------------------------ function check_arguments($argc, $argv, $currency_pair) { if ($argc != 2) { return FALSE; } $opts = $currency_pair; array_push($opts, "list"); if (!in_array($argv[1], $opts)) { return FALSE; } return TRUE; } function usage() { fputs(STDERR, "Usage: php trades.php OPTION" . PHP_EOL . "OPTION:" . PHP_EOL . " list : show all currency pair code" . PHP_EOL . " currency pair code : show specified currency pair trades" . PHP_EOL . " e.g. php trades.php btc_jpy" . PHP_EOL . PHP_EOL); }
上記のPHPサンプルソースを実行してみます。
trade API取得できたdate 取引日時, price 取引価格, amount 取引量, trade_type 取引種別を表示します。
本サンプルソースのオプション(引数)について以下に説明します。
指定できる通貨ペア一覧表示されます。
$ php trades.php list - currency_pairs - bch_btc bch_jpy bitcrystals_btc bitcrystals_jpy btc_jpy cicc_btc cicc_jpy eth_btc eth_jpy fscc_btc fscc_jpy jpyz_jpy mona_btc mona_jpy ncxc_btc ncxc_jpy pepecash_btc pepecash_jpy sjcx_btc sjcx_jpy xcp_btc xcp_jpy xem_btc xem_jpy zaif_btc zaif_jpy
以下、BTC/JPY, XEM/JPYの全ての取引履歴を取得した例になります。
$ php trades.php btc_jpy --- btc_jpy --- 2017-11-09 23:15:17: 828440.000000 0.002600 ask 2017-11-09 23:15:17: 828440.000000 0.002600 ask 2017-11-09 23:15:16: 828440.000000 0.002600 ask 2017-11-09 23:15:15: 828220.000000 0.023600 ask 2017-11-09 23:15:15: 828435.000000 0.017400 ask 2017-11-09 23:15:14: 828480.000000 0.000800 bid 2017-11-09 23:15:14: 828450.000000 0.001000 bid 2017-11-09 23:15:14: 828440.000000 0.017400 bid 2017-11-09 23:15:12: 828220.000000 0.038400 ask 2017-11-09 23:15:12: 828435.000000 0.002600 ask 2017-11-09 23:15:12: 828440.000000 0.002600 bid 2017-11-09 23:15:12: 828410.000000 0.029400 bid 2017-11-09 23:15:12: 828410.000000 0.020600 bid 2017-11-09 23:15:12: 828405.000000 0.001100 bid 2017-11-09 23:15:12: 828360.000000 0.000200 bid 2017-11-09 23:15:11: 828360.000000 0.001800 bid
$ php trades.php xem_jpy --- xem_jpy --- 2017-11-09 23:15:48: 23.599800 4300.000000 bid 2017-11-09 23:15:13: 23.550000 1748.900000 ask 2017-11-09 23:15:13: 23.550000 686.000000 ask 2017-11-09 23:15:13: 23.550000 1282.000000 ask 2017-11-09 23:14:49: 23.599900 12.000000 bid 2017-11-09 23:14:12: 23.550000 1718.000000 bid 2017-11-09 23:14:10: 23.530000 830.000000 ask 2017-11-09 23:13:56: 23.550000 1649.000000 ask 2017-11-09 23:13:45: 23.600000 989.000000 bid 2017-11-09 23:13:28: 23.550000 100.000000 ask 2017-11-09 23:13:21: 23.600000 63.000000 bid 2017-11-09 23:13:12: 23.600000 8.000000 bid 2017-11-09 23:12:52: 23.550000 455.000000 bid 2017-11-09 23:12:31: 23.540000 228.000000 ask 2017-11-09 23:12:12: 23.525000 631.800000 ask 2017-11-09 23:12:12: 23.550000 156.200000 ask
以上、Zaif の公開APIである全ての取引履歴を取得APIのブラウザによるアクセスとPHPによる全ての取引履歴を取得呼び出しのサンプルコードの記事でした。
以下のバナーはZaif
へのリンクです。