zaifのdepth・板情報を取得APIのサンプルコード(PHP)

当サイトに記載されている会社名、製品名などは一般に各社または団体の商標または登録商標です。
当サイトの資料により直接的および間接的障害が生じても一切責任を負いません。
あらかじめご了承ください。


PHPを使ってZaifdepth・板情報を取得APIの使用例を以下に記します。


以下のバナーはZaif へのリンクです。

関連記事

ブラウザで板情報を取得APIを呼び出してみる

公開APIなのでZaif に取引口座を開設していなくても使用することができます。

「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を使用しています。

ブラウザで板情報を取得

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

ブラウザで板情報を取得APIにアクセスした結果

以下ブラウザから上記URLにアクセスしたときの出力です。


以下のバナーはZaif へのリンクです。

PHPで「depth・板情報を取得」APIを呼び出してみる(サンプルコード)

PHPのサンプルコードは以下のようになります。
(btc_jpy通貨ペアで試すと返却されるasks, bidsの数が150(計300)だったので上位15件表示するようになっています。
また、asksですが、上位15件を取得後、逆順にしています。(売気配と買気配を見やすくするためです。)
最後にdepth・板情報取得APIで取得したasksとbidsの件数を表示して終了します。

filedepth.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);
}

実行例

上記のPHPサンプルソースを実行してみます。

オプション

本サンプルソースのオプション(引数)について以下に説明します。

listで実行

指定できる通貨ペア一覧表示されます。

$ 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

通貨ペアコードを指定して実行

以下、BTC/JPY, XEM/JPYの板情報を取得した例になります。

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


以下のバナーはZaif へのリンクです。


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