使用PHP通过Cryptopia API访问交易历史

时间:2018-03-12 00:08:49

标签: php arrays api

我试图使用官方API访问Cryptopia上所有硬币的最后买卖订单总和。 首先,这里是API的链接:https://www.cryptopia.co.nz/Forum/Thread/255

当我提出单个硬币的请求时,一切正常。

  $coin = 100;
  $url = "https://www.cryptopia.co.nz/api/GetMarketHistory/". $coin . "/" . 1;
  $curl_dscr = curl_init($url);
  curl_setopt($curl_dscr, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($curl_dscr, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($curl_dscr, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($curl_dscr, CURLOPT_SSLVERSION, 'CURL_SSLVERSION_SSLv3');
  curl_setopt($curl_dscr, CURLOPT_FOLLOWLOCATION, 1);
  $data = json_decode(curl_exec($curl_dscr), 1);

  if (empty($data['Data'])) exit;
  $sell_total = 0;
  $buy_total = 0;
  foreach ($data['Data'] as $pair) {
    list($market) = explode('_', $pair['Label']);
    if ($pair['Type'] == 'Sell')
      $sell_total += $pair['Total'];
    elseif ($pair['Type'] == 'Buy') {
      $buy_total += $pair['Total'];
    }
  }
  echo $sell_total;
  echo $buy_total;
  echo $url;

但是,当我创建一个硬币数组并在终端上运行脚本时,脚本不会输出任何内容。我甚至都没有收到错误。这是代码。

  $coins = array(1261,5313);
  foreach ($coins as $coin) {
      $url = "https://www.cryptopia.co.nz/api/GetMarketHistory/" . $coin . "/" . 1;
      $curl_dscr = curl_init($url);
      curl_setopt($curl_dscr, CURLOPT_RETURNTRANSFER, TRUE);
      curl_setopt($curl_dscr, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($curl_dscr, CURLOPT_SSL_VERIFYHOST, false);
      curl_setopt($curl_dscr, CURLOPT_SSLVERSION, 'CURL_SSLVERSION_SSLv3');
      curl_setopt($curl_dscr, CURLOPT_FOLLOWLOCATION, 1);
      $data = json_decode(curl_exec($curl_dscr), 1);

      if (empty($data['Data'])) exit;
      $sell_total = 0;
      $buy_total = 0;
      foreach ($data['Data'] as $pair) {
          list($market) = explode('_', $pair['Label']);
          if ($pair['Type'] == 'Sell')
              $sell_total += $pair['Total'];
          elseif ($pair['Type'] == 'Buy') {
              $buy_total += $pair['Total'];
          }
      }
      echo $sell_total;
      echo $buy_total;
      echo $url;
      unset($coin);
  }

1 个答案:

答案 0 :(得分:2)

你能解释一下你是如何测试你的代码的,我只是运行它并且它有效,它显示:

0.000635710.00052873https://www.cryptopia.co.nz/api/GetMarketHistory/1261/1