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

PHPを使って&htmlinsert(zaif.html);の[[ticker・ティッカーを取得API>http://techbureau-api-document.readthedocs.io/ja/latest/public/2_individual/4_ticker.html]]の使用例を以下に記します。

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

* 関連記事 [#t7119983]
-[[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を呼び出してみる [#mf901ffd]
公開APIなので&htmlinsert(zaif.html);に取引口座を開設していなくても使用することができます。

「ticker・ティッカーを取得」API仕様(Zaif APIページから抜粋) ~
http://techbureau-api-document.readthedocs.io/ja/latest/public/2_individual/4_ticker.html

 ticker
 ティッカーを取得します。
 
 リクエスト
 /ticker/{currency_pair}
 例. https://api.zaif.jp/api/1/ticker/btc_jpy
 currency_pairに指定できる値は currency_pairs を参照してください。
 
 パラメータ
 なし

 戻り値
 
 キー	詳細	型
 last	終値	float
 high	過去24時間の高値	float
 low	過去24時間の安値	float
 vwap	過去24時間の加重平均	float
 volume	過去24時間の出来高	float
 bid	買気配値	float
 ask	売気配値	float

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


* ブラウザでティッカーを取得 [#z6596ace]
GETなのでブラウザでティッカーを取得することができます。~
以下は{currency_pair}にmona_btcを指定しています。~
https://api.zaif.jp/api/1/ticker/mona_btc

* ブラウザでティッカーを取得APIにアクセスした結果 [#k9b2b9f7]
以下ブラウザから上記URLにアクセスしたときの出力です。

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

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

* PHPで「ticker・ティッカーを取得」APIを呼び出してみる(サンプルコード) [#zc96bcc6]
PHPのサンプルコードは以下のようになります。
#ref(ticker.php.zip)
 <?php
 // API doc : http://techbureau-api-document.readthedocs.io/ja/latest/public/2_individual/4_ticker.html
 // API url : https://api.zaif.jp/api/1/ticker/{currency_pair}
 
 // ticker api url
 $zaif_api_url  = "https://api.zaif.jp/api/1/ticker/";
 
 // 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);
 }
 
 if ($argv[1] != "all") {
     $currency_pairs = array($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 ticker api
 foreach ($currency_pairs as $currency) {
     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);
     }
 //    print_r($json_decode);
     printf("* %s *" . PHP_EOL, $currency);
     printf("last  : %f" . PHP_EOL, $json_decode["last"]);
     printf("high  : %f" . PHP_EOL, $json_decode["high"]);
     printf("low   : %f" . PHP_EOL, $json_decode["low"]);
     printf("vwap  : %f" . PHP_EOL, $json_decode["vwap"]);
     printf("volume: %f" . PHP_EOL, $json_decode["volume"]);
     printf("bid   : %f" . PHP_EOL, $json_decode["bid"]);
     printf("ask   : %f" . PHP_EOL, $json_decode["ask"]);
     print(PHP_EOL);
 }
 curl_close($curl);
 
 exit(0);
 
 //------------------------------
 // function
 //------------------------------
 function check_arguments($argc, $argv, $currency_pair) {
     if ($argc != 2) {
         return FALSE;
     }
     $opts = $currency_pair;
     array_push($opts, "all", "list");
     if (!in_array($argv[1], $opts)) {
         return FALSE;
     }
     return TRUE;
 }
 
 function usage() {
     fputs(STDERR,
         "Usage: php ticker.php OPTION" . PHP_EOL .
         "OPTION:" . PHP_EOL .
         "   all  : show all currency pair ticker" . PHP_EOL .
         "   list : show all currency pair code" . PHP_EOL .
         "   currency pair code : show specified currency pair ticker" . PHP_EOL .
         "       e.g. php ticker.php btc_jpy" . PHP_EOL .
         PHP_EOL);
 }

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

** listで実行 [#k4571b54]
指定できる通貨ペア一覧表示されます。
 $ php ticker.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, ETH/JPYのティッカーを取得した例になります。
 $ php ticker.php btc_jpy
 * btc_jpy *
 last  : 840095.000000
 high  : 842000.000000
 low   : 802560.000000
 vwap  : 824376.559800
 volume: 15313.315700
 bid   : 840045.000000
 ask   : 840095.000000

 $ php ticker.php eth_jpy
 * eth_jpy *
 last  : 33895.000000
 high  : 35100.000000
 low   : 33300.000000
 vwap  : 33925.708100
 volume: 2288.611300
 bid   : 33895.000000
 ask   : 34045.000000


** allで実行 [#j9a60476]
通貨ペアすべてのティッカーを表示します。
 $ php ticker.php all
 * bch_btc *
 last  : 0.087000
 high  : 0.088500
 low   : 0.084200
 vwap  : 0.086200
 volume: 267.816900
 bid   : 0.085200
 ask   : 0.087000
 
 * bch_jpy *
 last  : 72200.000000
 high  : 72200.000000
 low   : 69000.000000
 vwap  : 70653.531400
 volume: 4570.843300
 bid   : 72195.000000
 ask   : 72200.000000
 
 * bitcrystals_btc *
 last  : 0.000050
 high  : 0.000000
 low   : 0.000000
 vwap  : 0.000000
 volume: 0.000000
 bid   : 0.000033
 ask   : 0.000050
 
 * bitcrystals_jpy *
 last  : 35.500300
 high  : 39.500000
 low   : 35.000200
 vwap  : 37.500000
 volume: 12855.500000
 bid   : 35.500700
 ask   : 36.899700
 
 * btc_jpy *
 last  : 840620.000000
 high  : 843000.000000
 low   : 802560.000000
 vwap  : 824412.860700
 volume: 15337.622200
 bid   : 840620.000000
 ask   : 841125.000000
 
 <省略> 
 * pepecash_btc *
 last  : 0.000003
 high  : 0.000003
 low   : 0.000003
 vwap  : 0.000000
 volume: 149361.000000
 bid   : 0.000003
 ask   : 0.000003
 
 * pepecash_jpy *
 last  : 2.303000
 high  : 2.600000
 low   : 2.000000
 vwap  : 2.362200
 volume: 11022688.178700
 bid   : 2.303000
 ask   : 2.324900
 
 * sjcx_btc *
 last  : 0.000105
 high  : 0.000105
 low   : 0.000100
 vwap  : 0.000100
 volume: 35.000000
 bid   : 0.000050
 ask   : 0.000105
 
 * sjcx_jpy *
 last  : 57.850000
 high  : 65.000000
 low   : 55.000400
 vwap  : 60.056700
 volume: 16657.900000
 bid   : 57.850000
 ask   : 58.800000
 
 * xcp_btc *
 last  : 0.001700
 high  : 0.002350
 low   : 0.001700
 vwap  : 0.001800
 volume: 240.000000
 bid   : 0.001700
 ask   : 0.002349
 
 * xcp_jpy *
 last  : 1410.000000
 high  : 1455.000000
 low   : 1230.000100
 vwap  : 1374.974400
 volume: 1539.800000
 bid   : 1399.999700
 ask   : 1410.000000
 
 * xem_btc *
 last  : 0.000024
 high  : 0.000026
 low   : 0.000024
 vwap  : 0.000000
 volume: 585944.000000
 bid   : 0.000024
 ask   : 0.000025
 
 * xem_jpy *
 last  : 20.579900
 high  : 21.550000
 low   : 20.080000
 vwap  : 20.725500
 volume: 8292887.100000
 bid   : 20.540000
 ask   : 20.579900
 
 * zaif_btc *
 last  : 0.000001
 high  : 0.000001
 low   : 0.000001
 vwap  : 0.000000
 volume: 6466050.000000
 bid   : 0.000001
 ask   : 0.000001
 
 * zaif_jpy *
 last  : 0.515000
 high  : 0.541500
 low   : 0.459800
 vwap  : 0.505700
 volume: 757571669.500000
 bid   : 0.511100
 ask   : 0.515000


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


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

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS