- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2017-11-08T14:40:50+09:00","","")
#author("2017-11-08T21:09:23+09:00","","")
#navi(../)
* zaifのlast_price・現在の終値を取得APIのサンプルコード(PHP) [#r04bff5a]
&color(red){当サイトに記載されている会社名、製品名などは一般に各社または団体の商標または登録商標です。&br;当サイトの資料により直接的および間接的障害が生じても一切責任を負いません。&br;あらかじめご了承ください。};
----
PHPを使って&htmlinsert(zaif.html);の[[last_price・現在の終値を取得API>http://techbureau-api-document.readthedocs.io/ja/latest/public/2_individual/3_last_price.html]]APIの使用例を以下に記します。
#contents
----
以下のバナーは&htmlinsert(zaif.html);へのリンクです。~
#htmlinsert(zaif_wide_1.html)
* 関連記事 [#t71ff04c]
-[[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)]]
* ブラウザで通貨情報を取得APIを呼び出してみる [#tb8aab3f]
公開APIなので&htmlinsert(zaif.html);に取引口座を開設していなくても使用することができます。
「last_price・現在の終値を取得」API仕様(Zaif APIページから抜粋) ~
http://techbureau-api-document.readthedocs.io/ja/latest/public/2_individual/3_last_price.html
last_price
現在の終値を取得します。
リクエスト
/last_price/{currency_pair}
例. https://api.zaif.jp/api/1/last_price/btc_jpy
currency_pairに指定できる値は currency_pairs を参照してください。
パラメータ
なし
戻り値
last_price 現在の終値 float
上記の指定するcurrency_pairsですが、以下のAPIサンプルコードを実行して取得したcurrency_pairsを使用しています。
-[[currency_pairs・通貨ペア情報を取得(PHP)>API/zaif/currency_pairs(PHP)]]
* ブラウザで現在の終値を取得 [#n1ff8c26]
GETなのでブラウザで現在の終値を取得することができます。~
以下は{currency_pair}にxem_jpyを指定しています。~
https://api.zaif.jp/api/1/last_price/xem_jpy
* ブラウザで現在の終値を取得APIにアクセスした結果 [#s0a0d66f]
以下ブラウザから上記URLにアクセスしたときの出力です。
-Firefox ver56.0~
JSONが整形され表示されています。~
見やすいですね。~
(生データをクリックすると上記同様にJSON形式の返却された文字列が表示されます。)
#ref(01.png)
----
以下のバナーは&htmlinsert(zaif.html);へのリンクです。~
#htmlinsert(zaif_wide_2.html)
* PHPで「last_price・現在の終値を取得」APIを呼び出してみる(サンプルコード) [#fb464675]
PHPのサンプルコードは以下のようになります。
#ref(last_price.php.zip)
<?php
// API doc : http://techbureau-api-document.readthedocs.io/ja/latest/public/2_individual/3_last_price.html
// API url : https://api.zaif.jp/api/1/last_price/{currency_pair}
// last_price api url
$zaif_api_url = "https://api.zaif.jp/api/1/last_price/";
// 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 last_price 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 : %f" . PHP_EOL, $currency, $json_decode["last_price"]);
}
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 last_price OPTION" . PHP_EOL .
"Usage: php last_price.php OPTION" . PHP_EOL .
"OPTION:" . PHP_EOL .
" all : show all currency pair price" . PHP_EOL .
" list : show all currency pair code" . PHP_EOL .
" currency pair code : show specified currency pair" . PHP_EOL .
" e.g. php last_price btc_jpy" . PHP_EOL .
" e.g. php last_price.php btc_jpy" . PHP_EOL .
PHP_EOL);
}
* 実行例 [#kae1674d]
上記のPHPサンプルソースを実行してみます。~
** オプション [#hf6d14ec]
-- all すべての通貨の現在の終値を表示します。
-- list 通貨ペアコード一覧を表示します。
-- 通貨ペアコード(e.g. btc_jpy) 指定した通貨ペアコードの現在の終値を表示します。
** listで実行 [#cde823bc]
$ php last_price.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
** allで実行 [#g504dc36]
$ php last_price.php all
bch_btc : 0.085200
bch_jpy : 70100.000000
bitcrystals_btc : 0.000050
bitcrystals_jpy : 36.100100
btc_jpy : 815005.000000
cicc_btc : 0.000023
cicc_jpy : 14.500000
eth_btc : 0.041000
eth_jpy : 33600.000000
fscc_btc : 0.000041
fscc_jpy : 33.000100
jpyz_jpy : 1.300000
mona_btc : 0.000380
mona_jpy : 302.900000
ncxc_btc : 0.000148
ncxc_jpy : 111.900200
pepecash_btc : 0.000003
pepecash_jpy : 2.190200
sjcx_btc : 0.000105
sjcx_jpy : 64.889900
xcp_btc : 0.001781
xcp_jpy : 1360.100000
xem_btc : 0.000025
xem_jpy : 20.439900
zaif_btc : 0.000001
zaif_jpy : 0.492700
** 通貨ペアコードを指定して実行 [#q0461ebe]
$ php last_price.php btc_jpy
btc_jpy : 816530.000000
$ php last_price.php zaif_jpy
zaif_jpy : 0.492700
$ php last_price.php mona_jpy
mona_jpy : 305.400000
以上、&htmlinsert(zaif.html);の公開APIである現在の終値を取得APIのブラウザによるアクセスとPHPによる現在の終値を取得呼び出しのサンプルコードの記事でした。
----
以下のバナーは&htmlinsert(zaif.html);へのリンクです。~
#htmlinsert(zaif_wide_3.html)