/** * First we check if we've made a request within in the wait limit, and if so, * we return stale data. We also check if the static variable $_btcTickerStr is * null (this will only happen on the first run). If its null, we are cool to make * a call to MtGox to get the current ticker. * * Attempts to query the MtGox API. If the Request fails, we print an error message * and do NOT update the _lastTickerRequest time. * * If the Request succeeds, we format a message for the channel, update the lastTickerRequest * time and also set the current price of a BTC to the last sale price. This price is used * to check limits/triggers against. * * @return string Return a ticker formatted string, or an error message on failure. */ private function getBitCoinData() { // Enforce that we can't hit the mtGox server more than once every 5 seconds. if (time() > $this->_lastTickerRequest + self::mtgoxWaitSec || null == self::$_btcTickerStr) { $btcJSON = file_get_contents(self::mtgoxTicketURL); if (FALSE == $btcJSON) { return 'Error retreiving ticker, try again soon.'; } $btcSet = json_decode($btcJSON); $spread = bcsub($btcSet->ticker->sell, $btcSet->ticker->buy, 6); self::$_btcTickerStr = sprintf("(\$) High:%s Low:%s Vol:%s Bid:%s Ask:%s Last:%s Spread:%s", $btcSet->ticker->high, $btcSet->ticker->low, number_format($btcSet->ticker->vol), $btcSet->ticker->buy, $btcSet->ticker->sell, $btcSet->ticker->last, $spread); $this->_lastTickerRequest = time(); // Set the current price to the last sale price. $this->_currentBTCPrice = $btcSet->ticker->last; return self::$_btcTickerStr; } return sprintf("[%ss stale] %s", time() - $this->_lastTickerRequest, self::$_btcTickerStr); }