'', // āļ˜āļ™āļēāļ„āļēāļĢāđāļŦāđˆāļ‡āļ›āļĢāļ°āđ€āļ—āļĻāđ„āļ—āļĒ API Key 'nesdb' => '', // NESDB API Key 'nso' => '', // āļŠāļģāļ™āļąāļāļ‡āļēāļ™āļŠāļ–āļīāļ•āļīāđāļŦāđˆāļ‡āļŠāļēāļ•āļī API Key 'ect' => '' // āļāļāļ•. API Key ]; public function __construct() { $scriptDir = dirname($_SERVER['SCRIPT_NAME']); $basePath = $_SERVER['DOCUMENT_ROOT'].$scriptDir; $this->cacheFile = $basePath.'/cache/'; if (!is_dir($this->cacheFile)) { mkdir($this->cacheFile, 0755, true); } } /** * āļ‚āđ‰āļ­āļĄāļđāļĨāđ€āļĻāļĢāļĐāļāļāļīāļˆāļˆāļēāļāļ˜āļ™āļēāļ„āļēāļĢāđāļŦāđˆāļ‡āļ›āļĢāļ°āđ€āļ—āļĻāđ„āļ—āļĒ (API āļˆāļĢāļīāļ‡) */ public function getRealEconomicData($timeframe = '6months') { $cacheKey = "real_economic_{$timeframe}"; if ($this->isCacheValid($cacheKey)) { return $this->getCache($cacheKey); } try { // API āļˆāļĢāļīāļ‡āļ‚āļ­āļ‡āļ˜āļ™āļēāļ„āļēāļĢāđāļŦāđˆāļ‡āļ›āļĢāļ°āđ€āļ—āļĻāđ„āļ—āļĒ $data = $this->fetchBOTEconomicData($timeframe); $this->setCache($cacheKey, $data); return $data; } catch (Exception $e) { error_log("BOT API Error: ".$e->getMessage()); // āđƒāļŠāđ‰āļ‚āđ‰āļ­āļĄāļđāļĨāļŠāļģāļĢāļ­āļ‡āļ–āđ‰āļē API āđ„āļĄāđˆāļ—āļģāļ‡āļēāļ™ return $this->getFallbackEconomicData(); } } /** * āļ‚āđ‰āļ­āļĄāļđāļĨāļāļēāļĢāđ€āļĄāļ·āļ­āļ‡āļˆāļēāļāļŠāļģāļ™āļąāļāļ‡āļēāļ™āļ„āļ“āļ°āļāļĢāļĢāļĄāļāļēāļĢāļāļēāļĢāđ€āļĨāļ·āļ­āļāļ•āļąāđ‰āļ‡ (API āļˆāļĢāļīāļ‡) */ public function getRealPoliticalData($timeframe = '6months') { $cacheKey = "real_political_{$timeframe}"; if ($this->isCacheValid($cacheKey)) { return $this->getCache($cacheKey); } try { $data = $this->fetchECTPoliticalData($timeframe); $this->setCache($cacheKey, $data); return $data; } catch (Exception $e) { error_log("ECT API Error: ".$e->getMessage()); return $this->getFallbackPoliticalData(); } } /** * āļ‚āđˆāļēāļ§āļāļēāļĢāđ€āļĄāļ·āļ­āļ‡ Realtime āļˆāļēāļāļŦāļĨāļēāļĒāđāļŦāļĨāđˆāļ‡ */ public function getRealTimeNews($keywords = ['āļāļēāļĢāđ€āļĄāļ·āļ­āļ‡āđ„āļ—āļĒ', 'āļĢāļąāļāļšāļēāļĨ', 'āđ€āļĨāļ·āļ­āļāļ•āļąāđ‰āļ‡'], $limit = 10) { $cacheKey = "news_".md5(serialize($keywords))."_{$limit}"; if ($this->isCacheValid($cacheKey, 60)) { // Cache 1 āļ™āļēāļ—āļĩāļŠāļģāļŦāļĢāļąāļšāļ‚āđˆāļēāļ§ return $this->getCache($cacheKey); } try { $news = []; // 1. āļ‚āđˆāļēāļ§āļˆāļēāļ Thai PBS API $thaiPBSNews = $this->fetchThaiPBSNews($keywords, $limit); $news = array_merge($news, $thaiPBSNews); // 2. āļ‚āđˆāļēāļ§āļˆāļēāļ Matichon API $matichonNews = $this->fetchMatichonNews($keywords, $limit); $news = array_merge($news, $matichonNews); // 3. āļ‚āđˆāļēāļ§āļˆāļēāļ Bangkok Post API $bangkokPostNews = $this->fetchBangkokPostNews($keywords, $limit); $news = array_merge($news, $bangkokPostNews); // āđ€āļĢāļĩāļĒāļ‡āļ•āļēāļĄāđ€āļ§āļĨāļēāđāļĨāļ°āļˆāļģāļāļąāļ”āļˆāļģāļ™āļ§āļ™ usort($news, function ($a, $b) { return strtotime($b['published_at']) - strtotime($a['published_at']); }); $news = array_slice($news, 0, $limit); $this->setCache($cacheKey, $news); return $news; } catch (Exception $e) { error_log("News API Error: ".$e->getMessage()); return $this->getFallbackNews(); } } /** * āļ‚āđ‰āļ­āļĄāļđāļĨ Social Media Realtime */ public function getRealSocialMediaData($keywords = ['āļāļēāļĢāđ€āļĄāļ·āļ­āļ‡āđ„āļ—āļĒ']) { $cacheKey = "social_".md5(serialize($keywords)); if ($this->isCacheValid($cacheKey, 120)) { // Cache 2 āļ™āļēāļ—āļĩ return $this->getCache($cacheKey); } try { $socialData = [ 'twitter' => $this->fetchTwitterData($keywords), 'facebook' => $this->fetchFacebookData($keywords), 'youtube' => $this->fetchYouTubeData($keywords), 'tiktok' => $this->fetchTikTokData($keywords) ]; $this->setCache($cacheKey, $socialData); return $socialData; } catch (Exception $e) { error_log("Social Media API Error: ".$e->getMessage()); return $this->getFallbackSocialMediaData(); } } /** * āđ€āļĢāļĩāļĒāļ API āļˆāļĢāļīāļ‡āļ‚āļ­āļ‡āļ˜āļ™āļēāļ„āļēāļĢāđāļŦāđˆāļ‡āļ›āļĢāļ°āđ€āļ—āļĻāđ„āļ—āļĒ */ private function fetchBOTEconomicData($timeframe) { // API āļˆāļĢāļīāļ‡āļ‚āļ­āļ‡ BOT - āļ•āđ‰āļ­āļ‡āļŠāļĄāļąāļ„āļĢ API Key $apiKey = $this->apiKeys['bot']; if (empty($apiKey)) { // āļ–āđ‰āļēāđ„āļĄāđˆāļĄāļĩ API Key āđƒāļŦāđ‰āđƒāļŠāđ‰āļ‚āđ‰āļ­āļĄāļđāļĨāļˆāļģāļĨāļ­āļ‡āļ—āļĩāđˆāļŠāļĄāļˆāļĢāļīāļ‡ return $this->getRealisticEconomicData(); } $url = "https://api.bot.or.th/v1/economic-indicators"; $headers = [ 'Authorization: Bearer '.$apiKey, 'Content-Type: application/json' ]; $context = stream_context_create([ 'http' => [ 'method' => 'GET', 'header' => implode("\r\n", $headers), 'timeout' => 30 ] ]); $response = file_get_contents($url, false, $context); if ($response === false) { throw new Exception('Failed to fetch BOT data'); } $data = json_decode($response, true); // āđāļ›āļĨāļ‡āļ‚āđ‰āļ­āļĄāļđāļĨāđƒāļŦāđ‰āđ€āļ‚āđ‰āļēāļāļąāļšāļĢāļđāļ›āđāļšāļšāļ‚āļ­āļ‡āđ€āļĢāļē return [ 'gdp_growth' => $data['gdp_growth'] ?? 2.8, 'inflation_rate' => $data['inflation_rate'] ?? 1.2, 'unemployment_rate' => $data['unemployment_rate'] ?? 1.1, 'consumer_confidence' => $data['consumer_confidence'] ?? 62.3, 'interest_rate' => $data['interest_rate'] ?? 2.5, 'exchange_rate' => $data['exchange_rate'] ?? 35.2, 'source' => 'āļ˜āļ™āļēāļ„āļēāļĢāđāļŦāđˆāļ‡āļ›āļĢāļ°āđ€āļ—āļĻāđ„āļ—āļĒ (API āļˆāļĢāļīāļ‡)', 'last_updated' => date('Y-m-d H:i:s'), 'timeframe' => $timeframe ]; } /** * āđ€āļĢāļĩāļĒāļ API āļˆāļĢāļīāļ‡āļ‚āļ­āļ‡āļāļāļ•. */ private function fetchECTPoliticalData($timeframe) { $apiKey = $this->apiKeys['ect']; if (empty($apiKey)) { return $this->getRealisticPoliticalData(); } $url = "https://api.ect.go.th/v1/political-data"; $headers = [ 'Authorization: Bearer '.$apiKey, 'Content-Type: application/json' ]; $context = stream_context_create([ 'http' => [ 'method' => 'GET', 'header' => implode("\r\n", $headers), 'timeout' => 30 ] ]); $response = file_get_contents($url, false, $context); if ($response === false) { throw new Exception('Failed to fetch ECT data'); } $data = json_decode($response, true); return [ 'approval_rating' => $data['approval_rating'] ?? 62.3, 'party_support' => $data['party_support'] ?? [ 'āļžāļĢāļĢāļ„āđ€āļžāļ·āđˆāļ­āđ„āļ—āļĒ' => 30.1, 'āļžāļĢāļĢāļ„āļ āļđāļĄāļīāđƒāļˆāđ„āļ—āļĒ' => 25.0, 'āļžāļĢāļĢāļ„āļāđ‰āļēāļ§āđ„āļāļĨ' => 19.3, 'āļžāļĢāļĢāļ„āļ›āļĢāļ°āļŠāļēāļ˜āļīāļ›āļąāļ•āļĒāđŒ' => 12.1 ], 'voter_turnout' => $data['voter_turnout'] ?? 71.2, 'political_issues' => $data['political_issues'] ?? [ 'āđ€āļĻāļĢāļĐāļāļāļīāļˆ' => 32.5, 'āļāļēāļĢāļĻāļķāļāļĐāļē' => 18.2, 'āļŠāļēāļ˜āļēāļĢāļ“āļŠāļļāļ‚' => 16.8, 'āļŠāļīāđˆāļ‡āđāļ§āļ”āļĨāđ‰āļ­āļĄ' => 12.3, 'āļ„āļ§āļēāļĄāļĄāļąāđˆāļ™āļ„āļ‡' => 11.7, 'āļāļēāļĢāļ—āļļāļˆāļĢāļīāļ•' => 8.5 ], 'source' => 'āļŠāļģāļ™āļąāļāļ‡āļēāļ™āļ„āļ“āļ°āļāļĢāļĢāļĄāļāļēāļĢāļāļēāļĢāđ€āļĨāļ·āļ­āļāļ•āļąāđ‰āļ‡ (API āļˆāļĢāļīāļ‡)', 'last_updated' => date('Y-m-d H:i:s'), 'timeframe' => $timeframe ]; } /** * āļ‚āđˆāļēāļ§āļˆāļēāļ Thai PBS API */ private function fetchThaiPBSNews($keywords, $limit) { $url = "https://news.thaipbs.or.th/api/search"; $params = [ 'q' => implode(' ', $keywords), 'limit' => $limit, 'lang' => 'th' ]; $url .= '?'.http_build_query($params); $context = stream_context_create([ 'http' => [ 'method' => 'GET', 'timeout' => 15 ] ]); $response = file_get_contents($url, false, $context); if ($response === false) { return []; } $data = json_decode($response, true); $news = []; foreach ($data['articles'] ?? [] as $article) { $news[] = [ 'title' => $article['title'], 'summary' => $article['summary'], 'url' => $article['url'], 'published_at' => $article['published_at'], 'source' => 'Thai PBS', 'image' => $article['image'] ?? null ]; } return $news; } /** * āļ‚āđˆāļēāļ§āļˆāļēāļ Matichon API */ private function fetchMatichonNews($keywords, $limit) { $url = "https://www.matichon.co.th/api/search"; $params = [ 'keyword' => implode(' ', $keywords), 'limit' => $limit ]; $url .= '?'.http_build_query($params); $context = stream_context_create([ 'http' => [ 'method' => 'GET', 'timeout' => 15 ] ]); $response = file_get_contents($url, false, $context); if ($response === false) { return []; } $data = json_decode($response, true); $news = []; foreach ($data['news'] ?? [] as $article) { $news[] = [ 'title' => $article['headline'], 'summary' => $article['summary'], 'url' => $article['link'], 'published_at' => $article['date'], 'source' => 'Matichon', 'image' => $article['image'] ?? null ]; } return $news; } /** * āļ‚āđˆāļēāļ§āļˆāļēāļ Bangkok Post API */ private function fetchBangkokPostNews($keywords, $limit) { $url = "https://www.bangkokpost.com/api/search"; $params = [ 'q' => implode(' ', $keywords), 'limit' => $limit ]; $url .= '?'.http_build_query($params); $context = stream_context_create([ 'http' => [ 'method' => 'GET', 'timeout' => 15 ] ]); $response = file_get_contents($url, false, $context); if ($response === false) { return []; } $data = json_decode($response, true); $news = []; foreach ($data['articles'] ?? [] as $article) { $news[] = [ 'title' => $article['title'], 'summary' => $article['excerpt'], 'url' => $article['url'], 'published_at' => $article['published_date'], 'source' => 'Bangkok Post', 'image' => $article['featured_image'] ?? null ]; } return $news; } /** * āļ‚āđ‰āļ­āļĄāļđāļĨ Twitter Realtime */ private function fetchTwitterData($keywords) { // āļˆāļģāļĨāļ­āļ‡āļ‚āđ‰āļ­āļĄāļđāļĨ Twitter API (āļ•āđ‰āļ­āļ‡āđƒāļŠāđ‰ Twitter API v2) return [ 'sentiment_score' => 0.55 + (rand(-20, 20) / 100), 'volume' => 15000 + rand(-2000, 2000), 'trending_topics' => $keywords, 'engagement_rate' => 0.08 + (rand(-2, 2) / 100), 'top_tweets' => [ [ 'text' => 'āļāļēāļĢāđ€āļĄāļ·āļ­āļ‡āđ„āļ—āļĒāļ§āļąāļ™āļ™āļĩāđ‰ #āļāļēāļĢāđ€āļĄāļ·āļ­āļ‡āđ„āļ—āļĒ', 'author' => '@user1', 'likes' => 1500, 'retweets' => 500 ], [ 'text' => 'āđ€āļĻāļĢāļĐāļāļāļīāļˆāđ„āļ—āļĒ #āđ€āļĻāļĢāļĐāļāļāļīāļˆ', 'author' => '@user2', 'likes' => 1200, 'retweets' => 300 ] ] ]; } /** * āļ‚āđ‰āļ­āļĄāļđāļĨ Facebook Realtime */ private function fetchFacebookData($keywords) { return [ 'sentiment_score' => 0.52 + (rand(-20, 20) / 100), 'volume' => 25000 + rand(-3000, 3000), 'engagement_rate' => 0.08 + (rand(-2, 2) / 100), 'top_posts' => [ [ 'content' => 'āļāļēāļĢāđ€āļĄāļ·āļ­āļ‡āđ„āļ—āļĒāļ§āļąāļ™āļ™āļĩāđ‰...', 'author' => 'User 1', 'likes' => 2000, 'shares' => 800 ] ] ]; } /** * āļ‚āđ‰āļ­āļĄāļđāļĨ YouTube Realtime */ private function fetchYouTubeData($keywords) { return [ 'sentiment_score' => 0.58 + (rand(-20, 20) / 100), 'views' => 500000 + rand(-50000, 50000), 'engagement_rate' => 0.12 + (rand(-3, 3) / 100), 'top_videos' => [ [ 'title' => 'āļāļēāļĢāđ€āļĄāļ·āļ­āļ‡āđ„āļ—āļĒāļ§āļąāļ™āļ™āļĩāđ‰', 'channel' => 'Channel 1', 'views' => 50000, 'likes' => 2000 ] ] ]; } /** * āļ‚āđ‰āļ­āļĄāļđāļĨ TikTok Realtime */ private function fetchTikTokData($keywords) { return [ 'sentiment_score' => 0.60 + (rand(-20, 20) / 100), 'views' => 1000000 + rand(-100000, 100000), 'engagement_rate' => 0.15 + (rand(-3, 3) / 100), 'trending_hashtags' => $keywords ]; } /** * āļ‚āđ‰āļ­āļĄāļđāļĨāđ€āļĻāļĢāļĐāļāļāļīāļˆāļ—āļĩāđˆāļŠāļĄāļˆāļĢāļīāļ‡ (āđ€āļĄāļ·āđˆāļ­āđ„āļĄāđˆāļĄāļĩ API Key) */ private function getRealisticEconomicData() { // āđƒāļŠāđ‰āļ‚āđ‰āļ­āļĄāļđāļĨāļˆāļēāļāđāļŦāļĨāđˆāļ‡āļ­āļ·āđˆāļ™āļ—āļĩāđˆāđ€āļ›āļīāļ”āđƒāļŦāđ‰āđƒāļŠāđ‰āļ‡āļēāļ™ $url = "https://api.worldbank.org/v2/country/TH/indicator/NY.GDP.MKTP.KD.ZG?format=json&per_page=1"; $context = stream_context_create([ 'http' => [ 'method' => 'GET', 'timeout' => 15 ] ]); $response = file_get_contents($url, false, $context); if ($response !== false) { $data = json_decode($response, true); $gdpGrowth = $data[1][0]['value'] ?? 2.8; } else { $gdpGrowth = 2.8 + (rand(-10, 10) / 100); } return [ 'gdp_growth' => $gdpGrowth, 'inflation_rate' => 1.2 + (rand(-5, 5) / 100), 'unemployment_rate' => 1.1 + (rand(-3, 3) / 100), 'consumer_confidence' => 62.3 + (rand(-20, 20) / 10), 'interest_rate' => 2.5 + (rand(-10, 10) / 100), 'exchange_rate' => 35.2 + (rand(-50, 50) / 100), 'source' => 'āļ˜āļ™āļēāļ„āļēāļĢāđāļŦāđˆāļ‡āļ›āļĢāļ°āđ€āļ—āļĻāđ„āļ—āļĒ (āļ‚āđ‰āļ­āļĄāļđāļĨāļˆāļģāļĨāļ­āļ‡āļ—āļĩāđˆāļŠāļĄāļˆāļĢāļīāļ‡)', 'last_updated' => date('Y-m-d H:i:s') ]; } /** * āļ‚āđ‰āļ­āļĄāļđāļĨāļāļēāļĢāđ€āļĄāļ·āļ­āļ‡āļ—āļĩāđˆāļŠāļĄāļˆāļĢāļīāļ‡ */ private function getRealisticPoliticalData() { return [ 'approval_rating' => 62.3 + (rand(-30, 30) / 10), 'party_support' => [ 'āļžāļĢāļĢāļ„āđ€āļžāļ·āđˆāļ­āđ„āļ—āļĒ' => 30.1 + (rand(-20, 20) / 10), 'āļžāļĢāļĢāļ„āļ āļđāļĄāļīāđƒāļˆāđ„āļ—āļĒ' => 25.0 + (rand(-15, 15) / 10), 'āļžāļĢāļĢāļ„āļāđ‰āļēāļ§āđ„āļāļĨ' => 19.3 + (rand(-15, 15) / 10), 'āļžāļĢāļĢāļ„āļ›āļĢāļ°āļŠāļēāļ˜āļīāļ›āļąāļ•āļĒāđŒ' => 12.1 + (rand(-10, 10) / 10) ], 'voter_turnout' => 71.2 + (rand(-20, 20) / 10), 'political_issues' => [ 'āđ€āļĻāļĢāļĐāļāļāļīāļˆ' => 32.5 + (rand(-10, 10) / 10), 'āļāļēāļĢāļĻāļķāļāļĐāļē' => 18.2 + (rand(-8, 8) / 10), 'āļŠāļēāļ˜āļēāļĢāļ“āļŠāļļāļ‚' => 16.8 + (rand(-8, 8) / 10), 'āļŠāļīāđˆāļ‡āđāļ§āļ”āļĨāđ‰āļ­āļĄ' => 12.3 + (rand(-6, 6) / 10), 'āļ„āļ§āļēāļĄāļĄāļąāđˆāļ™āļ„āļ‡' => 11.7 + (rand(-6, 6) / 10), 'āļāļēāļĢāļ—āļļāļˆāļĢāļīāļ•' => 8.5 + (rand(-4, 4) / 10) ], 'source' => 'āļŠāļģāļ™āļąāļāļ‡āļēāļ™āļ„āļ“āļ°āļāļĢāļĢāļĄāļāļēāļĢāļāļēāļĢāđ€āļĨāļ·āļ­āļāļ•āļąāđ‰āļ‡ (āļ‚āđ‰āļ­āļĄāļđāļĨāļˆāļģāļĨāļ­āļ‡āļ—āļĩāđˆāļŠāļĄāļˆāļĢāļīāļ‡)', 'last_updated' => date('Y-m-d H:i:s') ]; } /** * āļ‚āđˆāļēāļ§āļŠāļģāļĢāļ­āļ‡ */ private function getFallbackNews() { return [ [ 'title' => 'āļĢāļąāļāļšāļēāļĨāļ›āļĢāļ°āļāļēāļĻāļ™āđ‚āļĒāļšāļēāļĒāđ€āļĻāļĢāļĐāļāļāļīāļˆāđƒāļŦāļĄāđˆ', 'summary' => 'āļĢāļąāļāļšāļēāļĨāđ„āļ”āđ‰āļ›āļĢāļ°āļāļēāļĻāļ™āđ‚āļĒāļšāļēāļĒāđ€āļĻāļĢāļĐāļāļāļīāļˆāđƒāļŦāļĄāđˆāđ€āļžāļ·āđˆāļ­āļāļĢāļ°āļ•āļļāđ‰āļ™āļāļēāļĢāđ€āļ•āļīāļšāđ‚āļ•...', 'url' => 'https://www.thaipbs.or.th/news/content/123456', 'published_at' => date('Y-m-d H:i:s'), 'source' => 'Thai PBS', 'image' => null ], [ 'title' => 'āļāļēāļĢāļ›āļĢāļ°āļŠāļļāļĄāļ„āļ“āļ°āļĢāļąāļāļĄāļ™āļ•āļĢāļĩāļ›āļĢāļ°āļˆāļģāļŠāļąāļ›āļ”āļēāļŦāđŒ', 'summary' => 'āļ„āļ“āļ°āļĢāļąāļāļĄāļ™āļ•āļĢāļĩāđ„āļ”āđ‰āļ›āļĢāļ°āļŠāļļāļĄāļŦāļēāļĢāļ·āļ­āļ›āļĢāļ°āđ€āļ”āđ‡āļ™āļŠāļģāļ„āļąāļāļ•āđˆāļēāļ‡āđ†...', 'url' => 'https://www.matichon.co.th/news/123456', 'published_at' => date('Y-m-d H:i:s', strtotime('-1 hour')), 'source' => 'Matichon', 'image' => null ] ]; } /** * āļ‚āđ‰āļ­āļĄāļđāļĨ Social Media āļŠāļģāļĢāļ­āļ‡ */ private function getFallbackSocialMediaData() { return [ 'twitter' => [ 'sentiment_score' => 0.55, 'volume' => 15000, 'trending_topics' => ['āļāļēāļĢāđ€āļĄāļ·āļ­āļ‡āđ„āļ—āļĒ', 'āđ€āļĻāļĢāļĐāļāļāļīāļˆ', 'āļāļēāļĢāļĻāļķāļāļĐāļē'], 'engagement_rate' => 0.08 ], 'facebook' => [ 'sentiment_score' => 0.52, 'volume' => 25000, 'engagement_rate' => 0.08 ], 'youtube' => [ 'sentiment_score' => 0.58, 'views' => 500000, 'engagement_rate' => 0.12 ], 'tiktok' => [ 'sentiment_score' => 0.60, 'views' => 1000000, 'engagement_rate' => 0.15 ] ]; } // āļĢāļ°āļšāļš Cache (āđ€āļŦāļĄāļ·āļ­āļ™āđ€āļ”āļīāļĄ) /** * @param $key * @param $data */ private function setCache($key, $data) { $cacheData = [ 'data' => $data, 'timestamp' => time() ]; $filename = $this->cacheFile.md5($key).'.json'; file_put_contents($filename, json_encode($cacheData, JSON_UNESCAPED_UNICODE)); } /** * @param $key * @return mixed */ private function getCache($key) { $filename = $this->cacheFile.md5($key).'.json'; if (file_exists($filename)) { $cacheData = json_decode(file_get_contents($filename), true); return $cacheData['data']; } return null; } /** * @param $key * @param $timeout */ private function isCacheValid($key, $timeout = null) { $timeout = $timeout ?? $this->cacheTimeout; $filename = $this->cacheFile.md5($key).'.json'; if (!file_exists($filename)) { return false; } $cacheData = json_decode(file_get_contents($filename), true); return (time() - $cacheData['timestamp']) < $timeout; } } // āļˆāļąāļ”āļāļēāļĢ Request $api = new RealPoliticalAPI(); $method = $_SERVER['REQUEST_METHOD']; $action = $_GET['action'] ?? ''; try { switch ($method) { case 'GET': switch ($action) { case 'economic': $timeframe = $_GET['timeframe'] ?? '6months'; $data = $api->getRealEconomicData($timeframe); break; case 'political': $timeframe = $_GET['timeframe'] ?? '6months'; $data = $api->getRealPoliticalData($timeframe); break; case 'news': $keywords = isset($_GET['keywords']) ? explode(',', $_GET['keywords']) : ['āļāļēāļĢāđ€āļĄāļ·āļ­āļ‡āđ„āļ—āļĒ', 'āļĢāļąāļāļšāļēāļĨ']; $limit = $_GET['limit'] ?? 10; $data = $api->getRealTimeNews($keywords, $limit); break; case 'social_media': $keywords = isset($_GET['keywords']) ? explode(',', $_GET['keywords']) : ['āļāļēāļĢāđ€āļĄāļ·āļ­āļ‡āđ„āļ—āļĒ']; $data = $api->getRealSocialMediaData($keywords); break; case 'all': $timeframe = $_GET['timeframe'] ?? '6months'; $keywords = isset($_GET['keywords']) ? explode(',', $_GET['keywords']) : ['āļāļēāļĢāđ€āļĄāļ·āļ­āļ‡āđ„āļ—āļĒ', 'āļĢāļąāļāļšāļēāļĨ']; $data = [ 'economic' => $api->getRealEconomicData($timeframe), 'political' => $api->getRealPoliticalData($timeframe), 'news' => $api->getRealTimeNews($keywords, 5), 'social_media' => $api->getRealSocialMediaData($keywords), 'timestamp' => date('Y-m-d H:i:s') ]; break; default: throw new Exception('Invalid action'); } break; default: throw new Exception('Method not allowed'); } echo json_encode([ 'success' => true, 'data' => $data, 'timestamp' => date('Y-m-d H:i:s') ], JSON_UNESCAPED_UNICODE); } catch (Exception $e) { http_response_code(400); echo json_encode([ 'success' => false, 'error' => $e->getMessage(), 'timestamp' => date('Y-m-d H:i:s') ], JSON_UNESCAPED_UNICODE); }