Siteβ―Builder
Editing:
rss-proxy.php
writable 0666
<?php /************************************************************************* * RSS-PROXY β /rss-proxy.php?slug=<slug>&feed=<rss_url> * * βΊ Returns JSON: {"items":[{"title","link","date"},β¦],"url":"β¦"} * βΊ Caches to /social/<slug>/rss-cache.json for 1 hour * βΊ Timeouts: 3 s connect, 10 s download, 15 s script max *************************************************************************/ declare(strict_types=1); // --------------- headers & timeouts --------------- header('Content-Type: application/json; charset=utf-8'); set_time_limit(15); // --------------- 1) validate inputs --------------- $slug = preg_replace('/[^a-z0-9_]/i','', $_GET['slug'] ?? ''); $feed = $_GET['feed'] ?? ''; if ( !$slug || !filter_var($feed, FILTER_VALIDATE_URL) ) { http_response_code(400); echo json_encode(['error' => 'Bad or missing slug/feed']); exit; } // --------------- 2) prepare cache paths --------------- $baseDir = rtrim($_SERVER['DOCUMENT_ROOT'], '/'); $profileDir = "{$baseDir}/social/{$slug}"; $cacheFile = "{$profileDir}/rss-cache.json"; $cacheTTL = 3600; // 1 hour // --------------- 3) serve cache if fresh --------------- if (is_file($cacheFile) && filemtime($cacheFile) > time() - $cacheTTL) { echo file_get_contents($cacheFile); exit; } // --------------- 4) fetch the RSS feed --------------- $ch = curl_init($feed); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_FAILONERROR => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_CONNECTTIMEOUT => 3, CURLOPT_TIMEOUT => 10, CURLOPT_USERAGENT => 'RSS-Proxy/1.0', ]); $xmlData = curl_exec($ch); curl_close($ch); // --------------- 5) parse up to 5 items --------------- $items = []; if ($xmlData !== false) { $xml = @simplexml_load_string($xmlData, 'SimpleXMLElement', LIBXML_NOCDATA); if ($xml && isset($xml->channel->item)) { foreach ($xml->channel->item as $it) { $items[] = [ 'title' => (string)$it->title, 'link' => (string)$it->link, 'date' => (string)$it->pubDate, ]; if (count($items) >= 5) { break; } } } } // --------------- 6) build JSON & write cache --------------- $result = json_encode([ 'items' => $items, 'url' => $feed ], JSON_UNESCAPED_SLASHES); // ensure the profile directory exists if (!is_dir($profileDir)) { @mkdir($profileDir, 0755, true); } // write cache (silently fail if it canβt) @file_put_contents($cacheFile, $result); // --------------- 7) output --------------- echo $result;
Save changes
Create folder
writable 0777
Create
Cancel