2015-10-16 89 views
1

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

<?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); 

?> 
+1

重要的一点是你如何处理file_put_contents?你得到的错误?如果你分享这些信息,也许有人可以帮助你。 –

+0

另外,为什么选择文件存储而不是数据库有特定的原因吗? – blackpla9ue

+0

没有具体的原因,我只是认为一个文件会更容易处理。 – Darkstar

回答

2

我已经做了各种比特币API的几个项目,并已花费的时间存储API数据的很多对于同样的原因滞后。

1.)将数据放入数据库,......你会疯狂地使用flatfiles和这么多来源。不需要很大或很复杂,但它是适合这项工作的正确工具,而且会长期更灵活。

2.)CRON工作将是您希望以任意间隔触发代码的理想方式。在过去,我使用用户访问来触发处理脚本,它确实有效,我的经历使我不再那么做。

在你的例子中,如果你没有每分钟都有一个访问者,那么每个访问者都将不得不坐在API调用中,并且增益被消除。如果你每分钟有一个或多个访问者,那么这种方法看起来好一些,但是其中一个访问者仍然需要等待。

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

祝你好运。