从Coinbase(PHP)获取加密货币的所有交易价格

时间:2020-03-07 00:52:26

标签: char coinbase-php

enter image description here 我的项目是从历史币库价格数据中使用chart.js绘制折线图。结果应类似于Google的加密图。我已经找到了一种获取任何加密货币实时价格的简单方法,但是我不知道如何获取历史数据。我的项目是用PHP编写的,您可以在下面看到我的实时价格采集器功能。

/**
 * coinbase api call prices
 */

 function api_call_price($from, $to) {
   $from = (trim(strtoupper($from)));
   $to = (trim(strtoupper($to)));
   $url = 'curl -s -H "CB-VERSION: 2017-12-06" "https://api.coinbase.com/v2/prices/'.$from.'-'.$to.'/spot"';
   $tmp = shell_exec($url);
   $data = json_decode($tmp, true);
   if ($data && $data['data'] && $data['data']['amount']) {
     return (float)$data['data']['amount'];
   }
   return null;
 }

 // Usage: api_call_price("BTC", "EUR");

1 个答案:

答案 0 :(得分:0)

好的,我的解决方案是现在通过cronjob抓取数据并将其存储为JSON字符串。