【问题标题】:Saving API Data to a file PHP将 API 数据保存到文件 PHP
【发布时间】:2015-10-16 01:34:49
【问题描述】:

我从六个比特币交易所获取价格和交易量数据来制作价格转换器,但速度非常慢,因为每次加载页面时,它都必须查询所有这些网站的数据。 我想每 1 分钟在后台运行一次获取数据并将其保存到服务器上的文件中,然后当用户访问该站点时,它只是从已检索到的 API 数据的文件中获取它。我一直在尝试使用 file_put_contents 无济于事。 这是我目前得到的代码:

<?php

function getData($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$rawData = curl_exec($curl);
curl_close($curl);
return json_decode($rawData, true);
}


//BTC Volume LocalBitcoins
$BTCVolumeLocal = getData('https://localbitcoins.com/bitcoinaverage/ticker-all-currencies/');
$LocalVolume = $BTCVolumeLocal["USD"]["volume_btc"];

//BTC Volume BTCE
$BTCVolumeBTCE = getData('https://btc-e.com/api/3/ticker/btc_usd');
$BTCEVolume = $BTCVolumeBTCE["btc_usd"]["vol_cur"];

//BTC Volume Bitstamp
$BTCVolumeStamp = getData('https://www.bitstamp.net/api/ticker/');
$StampVolume = $BTCVolumeStamp["volume"];

//BTC Volume Bitfinex
$BTCVolumeFinex = getData('https://api.bitfinex.com/v1/pubticker/btcusd');
$FinexVolume = $BTCVolumeFinex["volume"];

//BTC Volume OKCoin
$BTCVolumeOK = getData('https://www.okcoin.com/api/ticker.do?ok=1');
$OKCoinVolume = $BTCVolumeOK["ticker"]["vol"];

//BTC Volume LakeBTC
$BTCVolumeLake = getData('https://www.lakebtc.com/api_v1/ticker');
$LakeVolume = $BTCVolumeLake["USD"]["volume"];

//Totals the Volumes
$TotalVolume = $LakeVolume + $FinexVolume + $OKCoinVolume + $StampVolume + $BTCEVolume + $LocalVolume;
//Percents of Total Volume
$BTCEPercent = $BTCEVolume / $TotalVolume;
$StampPercent = $StampVolume / $TotalVolume;
$FinexPercent = $FinexVolume / $TotalVolume;
$OKPercent = $OKCoinVolume / $TotalVolume;
$LakePercent = $LakeVolume / $TotalVolume;
$LocalPercent = $LocalVolume / $TotalVolume;

//BTC Price BTCE
$BTCPriceBTCE = getData('https://btc-e.com/api/3/ticker/btc_usd');
$BTCEPrice = $BTCPriceBTCE["btc_usd"]["last"];

//BTC Price Bitstamp
$BTCPriceStamp = getData('https://www.bitstamp.net/api/ticker/');
$StampPrice = $BTCPriceStamp["last"];

//BTC Price Bitfinex
$BTCPriceFinex = getData('https://api.bitfinex.com/v1/pubticker/btcusd');
$FinexPrice = $BTCPriceFinex["last_price"];

//BTC Price OKCoin
$BTCPriceOK = getData('https://www.okcoin.com/api/ticker.do?ok=1');
$OKPrice = $BTCPriceOK["ticker"]["last"];

//BTC Price LakeBTC
$BTCPriceLake = getData('https://www.lakebtc.com/api_v1/ticker');
$LakePrice = $BTCPriceLake["USD"]["last"];

//BTC Price LocalBitcoins
$BTCPriceLocal = getData('https://localbitcoins.com/bitcoinaverage/ticker-all-currencies/');
$LocalPrice = $BTCPriceLocal["USD"]["avg_1h"];

//BTC Price * Percent
$BTCEPricePercent = $BTCEPrice * $BTCEPercent;
$StampPricePercent = $StampPrice * $StampPercent;
$FinexPricePercent = $FinexPrice * $FinexPercent;
$OKPricePercent = $OKPrice * $OKPercent;
$LakePricePercent = $LakePrice * $LakePercent;
$LocalPricePercent = $LocalPrice * $LocalPercent;

//Bitcoin Price
$bitcoinPrice = round($LakePricePercent + $OKPricePercent + $FinexPricePercent + $StampPricePercent + $BTCEPricePercent + $LocalPricePercent, 2);

?>

【问题讨论】:

  • 重点是你用file_put_contents做什么?你得到的错误?如果您分享这些信息,也许有人可以帮助您。
  • 另外,您选择文件存储而不是数据库是否有特定原因?
  • 没有具体原因,我只是认为文件更容易处理。
  • Dat Pham 我尝试了很多东西,但都没有奏效,所以我最终将其全部删除。我只是在寻找最简单的方法来保存 API 数据是数据库或某种文件。

标签: php cron bitcoin


【解决方案1】:

我已经使用各种比特币 API 完成了几个项目,并且出于同样的滞后原因花费了大量时间存储 API 数据。

1.) 将数据放入数据库,...你会发疯地使用平面文件和这么多的来源。不需要很大或很复杂,但它是适合这项工作的工具,而且从长远来看会更加灵活。

2.) CRON 作业是在您想要的任何时间间隔触发代码的理想方式。过去我使用用户访问来触发处理脚本,它确实有效,我这样做的经验让我不再这样做了。

在您的示例中,如果您不是每分钟都有访问者,那么每个访问者仍将不得不坐下来完成 API 调用,并且收益会蒸发。如果您每分钟确实有一个或多个访问者,那么这种方法看起来会更好一些,...但是其中一个访问者仍然需要等待。

3.) 使用 CRON 方法,您将拥有 API 的完整日志,...这不是一件坏事。可以提供一些有用的历史信息,如果有任何 API 停机时间,可以覆盖您。如果它是由用户触发的,那么您将在该数据中出现漏洞,并且将来的选项会更少。

祝你好运。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 2013-02-03
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多