#author("2017-11-18T14:53:51+09:00","","")
#author("2017-12-07T00:36:05+09:00","","")
#navi(../)
* zaifのdepth・板情報を取得APIのサンプルコード(PHP) [#xf98d09b]
&color(red){当サイトに記載されている会社名、製品名などは一般に各社または団体の商標または登録商標です。&br;当サイトの資料により直接的および間接的障害が生じても一切責任を負いません。&br;あらかじめご了承ください。};
----

PHPを使って&htmlinsert(zaif.html);の[[depth・板情報を取得API>http://http://techbureau-api-document.readthedocs.io/ja/latest/public/2_individual/6_depth.html]]の使用例を以下に記します。

#contents
----
以下のバナーは&htmlinsert(zaif.html);へのリンクです。~
#htmlinsert(zaif_wide_1.html)

* 関連記事 [#q1212d00]
-[[API動作環境構築>API/環境構築]]
-[[currencies・通貨情報の取得(PHP)>API/zaif/currencies(PHP)]]
-[[currency_pairs・通貨ペア情報を取得(PHP)>API/zaif/currency_pairs(PHP)]]
-[[last_price・現在の終値を取得(PHP)>API/zaif/last_price(PHP)]]
-[[ticker・ティッカーを取得(PHP)>API/zaif/ticker(PHP)]]
-[[depth・板情報を取得(PHP)>API/zaif/depth(PHP)]]
-[[trades・全ての取引履歴を取得(PHP)>API/zaif/trades(PHP)]]
-[[取引通貨一覧の取得(PHP)>API/zaif/取引通貨一覧の取得(PHP)]]

-[[ZaifのAPIキー生成手順>API/zaif/APIキー生成手順]]
-[[アカウントの残高確認(PHP)>API/zaif/残高確認・get_info, get_info2(PHP)]]

* ブラウザで板情報を取得APIを呼び出してみる [#l758315a]
公開APIなので&htmlinsert(zaif.html);に取引口座を開設していなくても使用することができます。

「depth・板情報を取得」API仕様(Zaif APIページから抜粋) ~
http://http://techbureau-api-document.readthedocs.io/ja/latest/public/2_individual/6_depth.html

 depth
 
 板情報を取得します。
 リクエスト
 /depth/{currency_pair}
 例. https://api.zaif.jp/api/1/depth/btc_jpy
 
 currency_pairに指定できる値は currency_pairs を参照してください。
 パラメータ
 なし
 
 戻り値
 
 キー 	詳細 	型
 asks 	売り板情報 	list
 bids 	買い板情報 	list
 補足
 取引種別
 配列の最初が価格、最後が量


上記の指定するcurrency_pairsですが、以下のAPIサンプルコードを実行して取得したcurrency_pairsを使用しています。
-[[currency_pairs・通貨ペア情報を取得(PHP)>API/zaif/currency_pairs(PHP)]]


* ブラウザで板情報を取得 [#j521e35e]
GETなのでブラウザで板情報を取得することができます。~
以下は{currency_pair}にmona_btcを指定しています。~
https://api.zaif.jp/api/1/depth/mona_btc

* ブラウザで板情報を取得APIにアクセスした結果 [#t9a9ed0c]
以下ブラウザから上記URLにアクセスしたときの出力です。

-Firefox ver56.0~
JSONが整形され表示されています。~
見やすいですね。~
(生データをクリックすると上記同様にJSON形式の返却された文字列が表示されます。)
#ref(01.png)

----
以下のバナーは&htmlinsert(zaif.html);へのリンクです。~
#htmlinsert(zaif_wide_2.html)

* PHPで「depth・板情報を取得」APIを呼び出してみる(サンプルコード) [#rd85eeb8]
PHPのサンプルコードは以下のようになります。~
(btc_jpy通貨ペアで試すと返却されるasks, bidsの数が150(計300)だったので上位15件表示するようになっています。~
また、asksですが、上位15件を取得後、逆順にしています。(売気配と買気配を見やすくするためです。)~
最後にdepth・板情報取得APIで取得したasksとbidsの件数を表示して終了します。
#ref(depth.php.zip)
 <?php
 // API doc : http://techbureau-api-document.readthedocs.io/ja/latest/public/2_individual/6_depth.html
 // API url : https://api.zaif.jp/api/1/depth/{currency_pair}
 
 // depth api url
 $zaif_api_url  = "https://api.zaif.jp/api/1/depth/";
 
 // 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 depth 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;
 printf("currency_pair: %s" . PHP_EOL, $currency);
 // asks
 $i = 0;
 print ("----- asks -----" . PHP_EOL);
 $reverse_asks = array();
 foreach($json_decode["asks"] as $ask) {
     $i++;
     if ($i > $show_limit) { break; }
     array_unshift($reverse_asks, $ask);
 }
 foreach($reverse_asks as $ask) {
     printf("%f : %f" . PHP_EOL, $ask[0], $ask[1]);
 }
 // bids
 $i = 0;
 print ("----- bids -----" . PHP_EOL);
 foreach($json_decode["bids"] as $bid) {
     $i++;
     if ($i > $show_limit) { break; }
     printf("%f : %f" . PHP_EOL, $bid[0], $bid[1]);
 }
 print ("----------------" . PHP_EOL);
 printf("ask count : %d" . PHP_EOL . "bid count : %d" . PHP_EOL,
     count($json_decode["asks"]),
     count($json_decode["bids"]));
 
 
 
 
 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 depth.php OPTION" . PHP_EOL .
         "OPTION:" . PHP_EOL .
         "   list : show all currency pair code" . PHP_EOL .
         "   currency pair code : show specified currency pair depth" . PHP_EOL .
         "       e.g. php depth.php btc_jpy" . PHP_EOL .
         PHP_EOL);
 }



* 実行例 [#s805056c]
上記のPHPサンプルソースを実行してみます。~
** オプション [#z3a2af7b]
本サンプルソースのオプション(引数)について以下に説明します。
-- list 通貨ペアコード一覧を表示します。
-- 通貨ペアコード(e.g. btc_jpy) 指定した通貨ペアコードの板情報を表示します。

** listで実行 [#k4571b54]
指定できる通貨ペア一覧表示されます。
 $ php depth.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


** 通貨ペアコードを指定して実行 [#w1c976a5]
以下、BTC/JPY, XEM/JPYの板情報を取得した例になります。
- BTC/JPY
 $ php depth.php btc_jpy
 currency_pair: btc_jpy
 ----- asks -----
 849190.000000 : 0.043700
 849110.000000 : 0.010400
 849000.000000 : 0.033300
 848245.000000 : 0.010300
 848070.000000 : 0.010400
 848005.000000 : 0.788300
 848000.000000 : 0.082900
 847990.000000 : 0.001300
 847965.000000 : 0.182300
 847950.000000 : 0.381000
 847880.000000 : 0.025500
 847815.000000 : 0.110000
 847810.000000 : 0.122300
 847370.000000 : 0.155100
 847130.000000 : 0.100000
 ----- bids -----
 846055.000000 : 1.425300
 846050.000000 : 0.184100
 846015.000000 : 2.000000
 846010.000000 : 0.107000
 846005.000000 : 1.000000
 846000.000000 : 3.032900
 845870.000000 : 0.001000
 845790.000000 : 0.016200
 845730.000000 : 0.015400
 845700.000000 : 0.070000
 845590.000000 : 0.040000
 845505.000000 : 0.200000
 845460.000000 : 1.079000
 845450.000000 : 0.103800
 845360.000000 : 0.040000
 ----------------
 ask count : 150
 bid count : 150

- XEM/JPY
 $ php depth.php xem_jpy
 currency_pair: xem_jpy
 ----- asks -----
 20.720000 : 1143.200000
 20.710000 : 853.800000
 20.709900 : 319881.000000
 20.709800 : 2200.000000
 20.700000 : 1122.400000
 20.699800 : 1387.000000
 20.692000 : 998.000000
 20.691200 : 186.000000
 20.690000 : 26.100000
 20.680000 : 26.100000
 20.649500 : 2800.000000
 20.649300 : 3065.300000
 20.649000 : 9952.000000
 20.648900 : 6125.000000
 20.599800 : 20000.000000
 ----- bids -----
 20.557700 : 3646.800000
 20.550100 : 451.000000
 20.550000 : 130737.000000
 20.540100 : 115.000000
 20.540000 : 26800.100000
 20.530100 : 31.000000
 20.521100 : 9875.000000
 20.521000 : 5000.000000
 20.520100 : 15.000000
 20.510100 : 12066.000000
 20.510000 : 3161.000000
 20.500100 : 15.000000
 20.500000 : 12749.800000
 20.490100 : 15.000000
 20.490000 : 26.100000
 ----------------
 ask count : 150
 bid count : 150


以上、&htmlinsert(zaif.html);の公開APIである板情報を取得APIのブラウザによるアクセスとPHPによる板情報を取得呼び出しのサンプルコードの記事でした。


----
以下のバナーは&htmlinsert(zaif.html);へのリンクです。~
#htmlinsert(zaif_wide_3.html)



トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS