apiKey = $apiKey; $this->timeout = $timeout; } // ────────────────────────────────────────────── // Market Data // ────────────────────────────────────────────── /** * Get a list of all available cryptocurrencies. * * @return array * @throws FreeCryptoAPIException */ public function getCryptoList(): array { return $this->request('/getCryptoList'); } /** * Get detailed data for a specific cryptocurrency. * * @param string $symbol Cryptocurrency symbol (e.g. "BTC") * @return array * @throws FreeCryptoAPIException */ public function getData(string $symbol): array { return $this->request('/getData', ['symbol' => $symbol]); } /** * Get top cryptocurrencies by market cap. * * @param int|null $top Number of results to return (optional) * @return array * @throws FreeCryptoAPIException */ public function getTop(?int $top = null): array { return $this->request('/getTop', ['top' => $top]); } /** * Get cryptocurrency data converted to a specific fiat currency. * * @param string $symbol Cryptocurrency symbol (e.g. "BTC") * @param string $local Fiat currency code (e.g. "EUR") * @return array * @throws FreeCryptoAPIException */ public function getDataCurrency(string $symbol, string $local): array { return $this->request('/getDataCurrency', [ 'symbol' => $symbol, 'local' => $local, ]); } /** * Get performance metrics for a cryptocurrency. * * @param string $symbol Cryptocurrency symbol (e.g. "BTC") * @return array * @throws FreeCryptoAPIException */ public function getPerformance(string $symbol): array { return $this->request('/getPerformance', ['symbol' => $symbol]); } /** * Get volatility data. * * @param string|null $symbol Cryptocurrency symbol (optional) * @param int|null $top Number of results (optional) * @return array * @throws FreeCryptoAPIException */ public function getVolatility(?string $symbol = null, ?int $top = null): array { return $this->request('/getVolatility', [ 'symbol' => $symbol, 'top' => $top, ]); } /** * Get all-time high and all-time low data. * * @param string|null $symbol Cryptocurrency symbol (optional) * @param int|null $months Number of months to look back (optional) * @return array * @throws FreeCryptoAPIException */ public function getATHATL(?string $symbol = null, ?int $months = null): array { return $this->request('/getATHATL', [ 'symbol' => $symbol, 'months' => $months, ]); } /** * Get the current Fear & Greed Index. * * @return array * @throws FreeCryptoAPIException */ public function getFearGreed(): array { return $this->request('/getFearGreed'); } // ────────────────────────────────────────────── // Technical Analysis // ────────────────────────────────────────────── /** * Get technical analysis indicators for a cryptocurrency. * * @param string $symbol Cryptocurrency symbol (e.g. "BTC") * @return array * @throws FreeCryptoAPIException */ public function getTechnicalAnalysis(string $symbol): array { return $this->request('/getTechnicalAnalysis', ['symbol' => $symbol]); } /** * Get breakout signals. * * @param string|null $symbol Cryptocurrency symbol (optional) * @return array * @throws FreeCryptoAPIException */ public function getBreakouts(?string $symbol = null): array { return $this->request('/getBreakouts', ['symbol' => $symbol]); } /** * Get correlation data between cryptocurrencies. * * @param string $symbols Comma-separated cryptocurrency symbols (e.g. "BTC,ETH,SOL") * @param int|null $days Number of days for correlation window (optional) * @return array * @throws FreeCryptoAPIException */ public function getCorrelation(string $symbols, ?int $days = null): array { return $this->request('/getCorrelation', [ 'symbols' => $symbols, 'days' => $days, ]); } /** * Get support and resistance levels for a cryptocurrency. * * @param string $symbol Cryptocurrency symbol (e.g. "BTC") * @param int|null $period Period in days (optional) * @return array * @throws FreeCryptoAPIException */ public function getSupportResistance(string $symbol, ?int $period = null): array { return $this->request('/getSupportResistance', [ 'symbol' => $symbol, 'period' => $period, ]); } /** * Get Moving Average Ribbon data. * * @param string $symbol Cryptocurrency symbol (e.g. "BTC") * @param int|null $days Number of days (optional) * @return array * @throws FreeCryptoAPIException */ public function getMARibbon(string $symbol, ?int $days = null): array { return $this->request('/getMARibbon', [ 'symbol' => $symbol, 'days' => $days, ]); } /** * Get Bollinger Bands data. * * @param string $symbol Cryptocurrency symbol (e.g. "BTC") * @param int|null $days Number of days (optional) * @param int|null $period Bollinger period (optional) * @param float|null $stdDev Standard deviation multiplier (optional) * @return array * @throws FreeCryptoAPIException */ public function getBollinger(string $symbol, ?int $days = null, ?int $period = null, ?float $stdDev = null): array { return $this->request('/getBollinger', [ 'symbol' => $symbol, 'days' => $days, 'period' => $period, 'stdDev' => $stdDev, ]); } // ────────────────────────────────────────────── // Exchange Data // ────────────────────────────────────────────── /** * Get data for a specific exchange. * * @param string $exchange Exchange identifier (e.g. "binance") * @return array * @throws FreeCryptoAPIException */ public function getExchange(string $exchange): array { return $this->request('/getExchange', ['exchange' => $exchange]); } // ────────────────────────────────────────────── // Conversion // ────────────────────────────────────────────── /** * Convert an amount between two currencies. * * @param string $from Source currency symbol (e.g. "BTC") * @param string $to Target currency symbol (e.g. "ETH") * @param float $amount Amount to convert * @return array * @throws FreeCryptoAPIException */ public function getConversion(string $from, string $to, float $amount): array { return $this->request('/getConversion', [ 'from' => $from, 'to' => $to, 'amount' => $amount, ]); } // ────────────────────────────────────────────── // Historical Data // ────────────────────────────────────────────── /** * Get historical price data for a cryptocurrency. * * @param string $symbol Cryptocurrency symbol (e.g. "BTC") * @param int $days Number of days of history * @return array * @throws FreeCryptoAPIException */ public function getHistory(string $symbol, int $days): array { return $this->request('/getHistory', [ 'symbol' => $symbol, 'days' => $days, ]); } /** * Get price data for a specific time range. * * @param string $symbol Cryptocurrency symbol (e.g. "BTC") * @param string $start Start date (YYYY-MM-DD) * @param string $end End date (YYYY-MM-DD) * @return array * @throws FreeCryptoAPIException */ public function getTimeframe(string $symbol, string $start, string $end): array { return $this->request('/getTimeframe', [ 'symbol' => $symbol, 'start' => $start, 'end' => $end, ]); } /** * Get OHLC (Open, High, Low, Close) candlestick data. * * @param string $symbol Cryptocurrency symbol (e.g. "BTC") * @param int|null $days Number of days (optional) * @param string|null $startDate Start date in YYYY-MM-DD format (optional) * @param string|null $endDate End date in YYYY-MM-DD format (optional) * @return array * @throws FreeCryptoAPIException */ public function getOHLC(string $symbol, ?int $days = null, ?string $startDate = null, ?string $endDate = null): array { return $this->request('/getOHLC', [ 'symbol' => $symbol, 'days' => $days, 'startDate' => $startDate, 'endDate' => $endDate, ]); } // ────────────────────────────────────────────── // Internal helpers // ────────────────────────────────────────────── /** * Send a GET request to the API. * * @param string $endpoint API endpoint path (e.g. "/getData") * @param array $params Query parameters (null values are filtered out) * @return array Decoded JSON response * @throws FreeCryptoAPIException */ private function request(string $endpoint, array $params = []): array { // Remove null values from parameters $params = array_filter($params, function ($value) { return $value !== null; }); $url = self::BASE_URL . $endpoint; if (!empty($params)) { $url .= '?' . http_build_query($params); } $ch = curl_init(); if ($ch === false) { throw new FreeCryptoAPIException('Failed to initialise cURL', 0); } curl_setopt_array($ch, [ CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => $this->timeout, CURLOPT_HTTPHEADER => [ 'Authorization: Bearer ' . $this->apiKey, 'Accept: application/json', ], ]); $response = curl_exec($ch); $statusCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); $curlError = curl_error($ch); curl_close($ch); if ($response === false) { throw new FreeCryptoAPIException( 'cURL request failed: ' . $curlError, 0 ); } $decoded = json_decode($response, true); if (json_last_error() !== JSON_ERROR_NONE) { throw new FreeCryptoAPIException( 'Failed to decode JSON response: ' . json_last_error_msg(), $statusCode ); } if ($statusCode < 200 || $statusCode >= 300) { $message = isset($decoded['message']) ? $decoded['message'] : 'API request failed'; throw new FreeCryptoAPIException($message, $statusCode); } return $decoded; } }