$query, 'lang_ID' => 1, 'limit' => 5]); $headers = [ 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' . 'AppleWebKit/537.36 (KHTML, like Gecko) ' . 'Chrome/124.0.0.0 Safari/537.36', 'Accept: application/json, text/plain, */*', 'Accept-Language: en-US,en;q=0.9', 'Domain-Id: www.investing.com', 'Origin: https://www.investing.com', 'Referer: https://www.investing.com/', ]; $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_HTTPGET => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => $headers, CURLOPT_FOLLOWLOCATION => true, CURLOPT_TIMEOUT => 15, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_COOKIEFILE => '', CURLOPT_ENCODING => '', ]); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $curlError = curl_error($ch); curl_close($ch); if ($curlError) return ['error' => 'cURL error: ' . $curlError]; if ($httpCode !== 200) return ['error' => 'HTTP ' . $httpCode]; $data = json_decode($response, true); if ( !is_array($data) || empty($data['quotes']) || !is_array($data['quotes'][0]) || empty($data['quotes'][0]['url']) ) { return ['error' => 'No results found for: ' . $query]; } return ['url' => 'https://www.investing.com' . $data['quotes'][0]['url']]; } function searchYahooFinance(string $query): array { $url = 'https://query1.finance.yahoo.com/v1/finance/search?' . http_build_query([ 'q' => $query, 'count' => 5, 'lang' => 'en-US', 'region' => 'US' ]); $headers = [ 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' . 'AppleWebKit/537.36 (KHTML, like Gecko) ' . 'Chrome/124.0.0.0 Safari/537.36', 'Accept: application/json, text/plain, */*', ]; $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_HTTPGET => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => $headers, CURLOPT_TIMEOUT => 15, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_ENCODING => '', ]); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $curlError = curl_error($ch); curl_close($ch); if ($curlError) { return ['error' => 'Yahoo cURL error: ' . $curlError]; } if ($httpCode !== 200) { return ['error' => 'Yahoo HTTP ' . $httpCode]; } $data = json_decode($response, true); if ( !is_array($data) || empty($data['quotes']) || !is_array($data['quotes'][0]) || empty($data['quotes'][0]['shortname']) ) { return ['error' => 'No Yahoo Finance results found for: ' . $query]; } $firstQuote = $data['quotes'][0]; return [ 'shortname' => $firstQuote['shortname'], 'symbol' => $firstQuote['symbol'] ?? 'N/A', 'quoteType' => $firstQuote['quoteType'] ?? 'N/A', 'exchange' => $firstQuote['exchange'] ?? 'N/A', 'fullData' => $firstQuote ]; } function searchWithYahooFallback(string $originalQuery): array { // First try Investing.com $investingResult = searchInvestingCom($originalQuery); // If Investing.com succeeded, return it if (!isset($investingResult['error'])) { return $investingResult; } // Otherwise, fallback to Yahoo Finance $yahooResult = searchYahooFinance($originalQuery); if (isset($yahooResult['error'])) { return ['error' => 'Both Investing.com and Yahoo Finance failed. ' . $yahooResult['error']]; } // Try retrying Investing.com with the Yahoo shortname $retryQuery = $yahooResult['shortname']; $retryResult = searchInvestingCom($retryQuery); if (!isset($retryResult['error'])) { return $retryResult; // Success with retry } // Still failed - return suggestion data for the HTML form return [ 'needs_manual' => true, 'suggestion' => $yahooResult['shortname'], 'symbol' => $yahooResult['symbol'], 'original_query' => $originalQuery, 'yahoo_url' => 'https://finance.yahoo.com/quote/' . urlencode($yahooResult['symbol']) ]; } // ── Entry point ─────────────────────────────────────────────────────────────── // Check if this is a retry request (from the form) $retryQuery = trim($_GET['retry'] ?? ''); if ($retryQuery !== '') { $result = searchInvestingCom($retryQuery); if (!isset($result['error'])) { // Success on retry - redirect if (!headers_sent()) { header('Location: ' . $result['url'], true, 302); exit; } $safeUrl = htmlspecialchars($result['url'], ENT_QUOTES, 'UTF-8'); echo ''; exit; } // Retry still failed - show form again with the same suggestion $suggestion = htmlspecialchars($retryQuery, ENT_QUOTES, 'UTF-8'); $originalQuery = htmlspecialchars($_GET['original'] ?? $retryQuery, ENT_QUOTES, 'UTF-8'); showManualForm($suggestion, $originalQuery, 'Still no results for: ' . htmlspecialchars($retryQuery, ENT_QUOTES, 'UTF-8')); exit; } // Normal flow $query = trim($_GET['search'] ?? ''); if ($query === '') { http_response_code(400); die('Error: ?search= parameter is required. Example: ?search=GME'); } $result = searchWithYahooFallback($query); if (isset($result['error'])) { http_response_code(404); die('Error: ' . htmlspecialchars($result['error'], ENT_QUOTES, 'UTF-8')); } // If manual input is needed, show the form if (isset($result['needs_manual']) && $result['needs_manual'] === true) { showManualForm($result['suggestion'], $result['original_query']); exit; } // Normal redirect to Investing.com if (!headers_sent()) { header('Location: ' . $result['url'], true, 302); exit; } $safeUrl = htmlspecialchars($result['url'], ENT_QUOTES, 'UTF-8'); echo ''; exit; // ── Helper function to display the manual form ───────────────────────────────── function showManualForm(string $suggestion, string $originalQuery, string $errorMessage = '') { $safeSuggestion = htmlspecialchars($suggestion, ENT_QUOTES, 'UTF-8'); $safeOriginal = htmlspecialchars($originalQuery, ENT_QUOTES, 'UTF-8'); $safeError = htmlspecialchars($errorMessage, ENT_QUOTES, 'UTF-8'); ?>
Investing.com couldn't find results for ""