php文件获取Kraken API的内容json

时间:2018-01-01 06:22:18

标签: php json api

我正在尝试从硬币交易所kraken.com提取数据。我在使用php时遇到了麻烦。我想提取所有数据。

这是页面https://api.kraken.com/0/public/Trades?pair=XXBTZEUR&since=0

中的json
{"error":[],"result":{"XXBTZEUR":[["97.00000","1.00000000",1378856831.546,"s","m",""],["99.90000","0.10000000",1378859634.7626,"b","m",""],["99.90000","0.10000000",1378859669.3147,"b","m",""],["98.20000","0.10000000",1378869758.1198,"b","l",""],["96.91000","0.50000000",1378875023.0442,"s","m",""],["96.90000","0.50000000",1378875023.0529,"s","m",""],["96.80000","0.25000000",1378885271.7763,"s","l",""],["96.75000","0.50000000",1378891354.3994,"b","l",""],["96.00000","0.01000000",1379070996.3774,"s","m",""],["96.99000","0.00987833",1379071035.0723,"b","m",""],["96.00000","0.01100000",1379071113.4515,"s","m",""],["96.30000","0.10384000",1379085887.0434,"s","l",""],["96.20000","0.10395000",1379086063.4595,"s","l",""],["96.00000","0.79221000",1379086126.0679,"s","l",""],["96.00000","0.18679000",1379177019.7967,"s","l",""],["96.00000","0.10416000",1379177019.8048,"s","l",""],["95.00000","2.00000000",1379191972.5572,"s","l",""],["95.00000","0.10000000",1379223876.1675,"b","m",""],["95.00000","0.10000000",1379224436.4174,"b","m",""],["95.00000","0.10000000",1379227678.2785,"b","m",""],["95.00000","0.10526000",1379233704.2801,"b","l",""],["95.00000","0.52631578",1379269026.0017,"b","l",""],["95.00000","0.10526000",1379351677.2834,"b","l",""],["95.00000","3.25263000",1379361238.7824,"b","l",""],["94.99990","0.50000000",1379367396.9455,"s","m",""],["90.00000","0.10000000",1379367396.963,"s","m",""],["90.00000","0.40000000",1379367396.9714,"s","m",""],["95.00000","0.10526000",1379430554.4711,"b","l",""],["94.50000","1.00000000",1379452710.6395,"b","l",""],["94.50000","0.69312169",1379452872.1018,"s","l",""],["94.25000","0.21220000",1379469812.0172,"s","l",""],["94.25000","0.09467831",1379485875.3221,"s","l",""],["94.75000","1.00000000",1379485967.4924,"b","l",""],["95.00000","1.60527422",1379485967.4997,"b","l",""],["94.50000","2.57470899",1379516828.9905,"s","l",""],["93.60000","0.50000000",1379624491.8829,"s","l",""],["93.20000","2.00000000",1379624575.014,"s","l",""],["94.00000","0.50000000",1379751409.6507,"b","l",""],["94.00000","0.50000000",1379751453.7603,"b","l",""],["94.49000","0.50000000",1379751453.7694,"b","l",""],["93.00000","1.00000000",1379861091.9959,"s","l",""],["92.50000","0.50000000",1379960149.1993,"s","l",""],["90.25000","0.05506000",1380018049.8094,"s","m",""],["91.11000","0.20000000",1380125346.323,"s","m",""],["90.25000","0.05000000",1380125346.3315,"s","m",""],["91.20000","0.01000000",1380393449.7629,"s","m",""],["93.25000","0.50000000",1380450988.9032,"b","l",""],["94.50000","3.44402116",1380469809.9718,"b","l",""],["94.50000","2.22222222",1380483688.0604,"b","m",""],["94.50000","0.10137566",1380484004.1102,"b","m",""],["94.50000","1.05820105",1380538264.8179,"b","l",""],["94.50000","0.06600000",1380542779.1098,"b","l",""],["94.50000","0.51126349",1380556132.4255,"b","m",""],["94.50000","0.02220743",1380607691.1631,"b","l",""],["96.00000","0.70905000",1380657580.8253,"b","l",""],["93.50000","0.01000000",1380679387.1966,"s","l",""],["93.50000","0.01000000",1380699876.4497,"s","m",""],["96.99999","0.01000000",1380721281.0258,"b","m",""],["96.99999","0.10000000",1380723193.6339,"b","l",""],["96.99999","0.20000000",1380723418.0082,"b","l",""],["96.99999","0.19000000",1380724551.1446,"b","l",""],["96.99999","0.81000000",1380735225.9713,"s","l",""],["93.50000","4.98000000",1380735225.9792,"s","l",""],["93.25000","0.50000000",1380735225.9865,"s","l",""],:["90.10000","0.50000000",1380735225.9938,"s","l",""],

我的php代码如下所示:

<?php
$fgc = json_decode(file_get_contents("https://api.kraken.com/0/public/Trades?pair=XXBTZEUR&since=0"), true);

$contents = $fgc["result"];
  foreach($contents as $details){
    echo "".$details["XXBTZEUR"];
    echo PHP_EOL;

  }

?>

2 个答案:

答案 0 :(得分:0)

以下PHP示例代码可能有助于您入门。

$fgc = json_decode(file_get_contents("https://api.kraken.com/0/public/Trades?pair=XXBTZEUR&since=0"), true);

// Was there a problem getting the data?
if ((! $fgc) || ($fgc['error'])) {
    echo 'Error getting the data';
    exit;
}

// We have data. Dump some to see what we have
echo '<pre>';
foreach($fgc['result']['XXBTZEUR'] as $k => $v) {
    var_dump($v);
}
echo '</pre>';

这会吐出1000个这样的条目:

array(6) {
  [0]=>
  string(8) "97.00000"
  [1]=>
  string(10) "1.00000000"
  [2]=>
  float(1378856831.546)
  [3]=>
  string(1) "s"
  [4]=>
  string(1) "m"
  [5]=>
  string(0) ""
}

所以看起来每个条目都有六个元素。我假设元素在某处记录。

答案 1 :(得分:0)

你可以尝试这个,得到你期望的答案:

$fgc = json_decode(file_get_contents("https://api.kraken.com/0/public/Trades?pair=XXBTZEUR&since=0"), true);

$contents = $fgc["result"];

foreach ($contents['XXBTZEUR'] as $details) {

    echo"<pre>";
    print_r($details);
    echo PHP_EOL;
}