このエントリーをはてなブックマークに追加


coincheckの全取引履歴APIのサンプルコード(PHP)

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

2017/12/19に仕様変更がありましたので記事を修正しました。
取引履歴取得APIの仕様変更について
https://coincheck.com/blog/4599


PHPを使って Coincheck の全取引履歴の使用例を以下に記します。


スポンサーリンク

関連記事

ブラウザで全取引履歴APIを呼び出してみる

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

全取引履歴のAPI仕様(Coincheck APIページから抜粋)
https://coincheck.com/ja/documents/exchange/api#public-trades

全取引履歴
最新の取引履歴を取得できます。

HTTP REQUEST
GET /api/trades

PARAMETERS
*pair 取引ペア。現在は "btc_jpy" のみです。

またページネーションパラメータを指定して件数や並び順を指定することができます。

ページネーション
coincheckの一部APIではページネーションにてデータを分割して取得することが可能です。
PARAMETERS

limit 1ページあたりの取得件数を指定できます。
order "desc", "asc" を指定できます。
starting_after IDを指定すると絞り込みの開始位置を設定できます。
ending_before IDを指定すると絞り込みの終了位置を設定できます。

ブラウザで全取引履歴を取得する

GETなのでブラウザで全取引履歴を取得することができます。
以下のようなURLになります。
https://coincheck.com/api/trades?pair=btc_jpy

50件取得(limit=50)
https://coincheck.com/api/trades?pair=btc_jpy&limt=50

ブラウザで全取引履歴APIにアクセスした結果

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

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

PHPで全取引履歴APIを呼び出してみる(サンプルソース)

PHPのサンプルソースは以下のようになります。

<?php
// API doc : https://coincheck.com/ja/documents/exchange/api#public-trades
// API url : https://coincheck.com/api/trades

$coincheck_url  = "https://coincheck.com";
$trades_api = "/api/trades";
$url = $coincheck_url . $trades_api;

// arguments -> http query
$options = getopt("l:");
if (!check_arguments($options)) {
    usage();
    die(1);
}

$http_query = array(
    "pair"=>"btc_jpy"   
);
foreach ($options as $key => $value) {
    if     ($key == "l") { $http_query["limit"] = $value; }
}

if (count($http_query) > 0) {
    $url = $url . "?" . http_build_query($http_query);
}

// proxy settings
$proxy      = "";
$proxy_port = "";

$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_URL, $url);
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
$response = curl_exec($curl);
if ($response == FALSE) {
    fputs(STDERR, "[ERR] curl_exec(): " . curl_error($curl) . PHP_EOL);
    die(1);
}
curl_close($curl);

// 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);
}
// output json_decode
print_r($json_decode);

exit(0);

//----------------------------------------------------------
// functions
//----------------------------------------------------------
function check_arguments($options) {
    if (array_key_exists("l", $options)) {
		if (!ctype_digit($options["l"])) {
			return FALSE;
		}
    }
    return TRUE;
}

function usage() {
    $usage =
    "Uasge: php trades.php [-l limit]" . PHP_EOL .
    "Options:" . PHP_EOL .
    "  -l : limit " . PHP_EOL .
    PHP_EOL;
    fputs(STDERR, $usage);
}

実行例

上記のPHPサンプルソースを実行してみます。
オプション-lにより指定された数分表示します。
数の範囲は0〜99のようです。

$ php trades.php | head -30
Array
(
   [success] => 1
   [pagination] => Array
       (
           [limit] => 10
           [order] => desc
           [starting_after] => 
           [ending_before] => 
       )

   [data] => Array
       (
           [0] => Array
               (
                   [id] => 63673299
                   [amount] => 0.009
                   [rate] => 2110080
                   [pair] => btc_jpy
                   [order_type] => sell
                   [created_at] => 2017-12-19T15:20:01.000Z
               )

           [1] => Array
               (
                   [id] => 63673298
                   [amount] => 0.03
                   [rate] => 2110080
                   [pair] => btc_jpy
                   [order_type] => sell
$ php trades.php -l 1
Array
(
   [success] => 1
   [pagination] => Array
       (
           [limit] => 1
           [order] => desc
           [starting_after] => 
           [ending_before] => 
       )

   [data] => Array
       (
           [0] => Array
               (
                   [id] => 63673728
                   [amount] => 0.007
                   [rate] => 2109969
                   [pair] => btc_jpy
                   [order_type] => sell
                   [created_at] => 2017-12-19T15:21:24.000Z
               )

       )

)

BTC/JPYの取引履歴が返却されます。

以上、 Coincheck のpublic APIである全取引履歴APIのブラウザによるアクセスとPHPによる全取引履歴呼び出しのサンプルコードの記事でした。


スポンサーリンク

添付ファイル: filetrades.php.zip 305件 [詳細] file02.png 310件 [詳細] file01.png 326件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2017-12-20 (水) 00:27:05