setErrorMessage('Could not get reply: '.curl_error($ch)); return false; } $dec = json_decode($res, true); if (!$dec) { $this->setErrorMessage('Invalid data received, please make sure connection is working and requested API exists'); return false; } return $dec; } /** * Detect the API to properly extract information * @param url string API URL * @return data string API type **/ private function getApiType($url) { if (preg_match('/coinchoose.com/', $url)) { return 'coinchose'; } else if (preg_match('/btc-e.com/', $url)) { return 'btce'; } else if (preg_match('/cryptsy.com/', $url)) { return 'cryptsy'; } else if (preg_match('/cryptorush.in/', $url)) { return 'cryptorush'; } else if (preg_match('/mintpal.com/', $url)) { return 'mintpal'; } $this->setErrorMessage("API URL unknown"); return false; } /** * Extract price information from API data **/ public function getPrice() { $aData = $this->getApi($this->config['price']['url'], $this->config['price']['target']); $strCurrency = $this->config['currency']; // Check the API type for configured URL if (!$strApiType = $this->getApiType($this->config['price']['url'])) return false; // if api data is valid, extract price depending on API type if (is_array($aData)) { switch ($strApiType) { case 'coinchose': foreach ($aData as $aItem) { if($strCurrency == $aItem[0]) return $aItem['price']; } break; case 'btce': return $aData['ticker']['last']; break; case 'cryptsy': return @$aData['return']['markets'][$strCurrency]['lasttradeprice']; break; case 'cryptorush': return @$aData["$strCurrency/" . $this->config['price']['currency']]['last_trade']; break; case 'mintpal': return @$aData['0']['last_price']; break; } } else { $this->setErrorMessage("Got an invalid response from ticker API"); return false; } // Catchall, we have no data extractor for this API url $this->setErrorMessage("Undefined API to getPrice() on URL " . $this->config['price']['url']); return false; } } $tools = new Tools(); $tools->setDebug($debug); $tools->setConfig($config);