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'; } $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; // Extract price depending on API type 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; } // 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);