Jump to content

Widget:UPDEvents

From Iskomunidad
Revision as of 01:24, 29 October 2026 by IskoAdmin (talk | contribs) (Created page with " <?php →* * UP Diliman Events feed for Iskomunidad. * Retrieves up to five upcoming events from upd.edu.ph.: header('Content-Type: application/json; charset=utf-8'); header('Cache-Control: public, max-age=900'); date_default_timezone_set('Asia/Manila'); $sourceUrl = 'https://upd.edu.ph/events-list/'; function respond($events, $status = 200) { http_response_code($status); echo json_encode( $events, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

<?php /**

* UP Diliman Events feed for Iskomunidad.
* Retrieves up to five upcoming events from upd.edu.ph.
*/

header('Content-Type: application/json; charset=utf-8'); header('Cache-Control: public, max-age=900');

date_default_timezone_set('Asia/Manila');

$sourceUrl = 'https://upd.edu.ph/events-list/';

function respond($events, $status = 200) {

   http_response_code($status);
   echo json_encode(
       $events,
       JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
   );
   exit;

}

if (!function_exists('curl_init')) {

   respond(['error' => 'PHP cURL is not installed.'], 500);

}

$ch = curl_init($sourceUrl);

curl_setopt_array($ch, [

   CURLOPT_RETURNTRANSFER => true,
   CURLOPT_FOLLOWLOCATION => true,
   CURLOPT_MAXREDIRS => 3,
   CURLOPT_CONNECTTIMEOUT => 8,
   CURLOPT_TIMEOUT => 15,
   CURLOPT_USERAGENT => 'Iskomunidad-Events/1.0',
   CURLOPT_SSL_VERIFYPEER => true,
   CURLOPT_SSL_VERIFYHOST => 2,

]);

$html = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $error = curl_error($ch); curl_close($ch);

if ($html === false || $httpCode < 200 || $httpCode >= 300) {

   error_log('UPD events retrieval failed: ' . $error);
   respond(['error' => 'The UP Diliman events page is temporarily unavailable.'], 502);

}

libxml_use_internal_errors(true);

$dom = new DOMDocument(); $loaded = $dom->loadHTML('<?xml encoding="UTF-8">' . $html);

libxml_clear_errors();

if (!$loaded) {

   respond(['error' => 'Could not parse the UP Diliman events page.'], 502);

}

$xpath = new DOMXPath($dom); $links = $xpath->query('//a[@href]'); $today = new DateTimeImmutable('today', new DateTimeZone('Asia/Manila'));

$events = []; $seen = [];

foreach ($links as $link) {

   $url = trim($link->getAttribute('href'));
   $title = trim(preg_replace('/\s+/u', ' ', $link->textContent));
   // Only accept links to individual UP Diliman event pages.
   $path = parse_url($url, PHP_URL_PATH) ?: ;
   if (!preg_match('~^/events/[^/]+/$~', $path)) {
       continue;
   }
   // Event URLs contain their start date, e.g. ...-2026-10-26/
   if (!preg_match('/-(\d{4}-\d{2}-\d{2})\/?$/', $path, $match)) {
       continue;
   }
   $dateString = $match[1];
   $eventDate = DateTimeImmutable::createFromFormat(
       '!Y-m-d',
       $dateString,
       new DateTimeZone('Asia/Manila')
   );
   if (!$eventDate || $eventDate->format('Y-m-d') !== $dateString) {
       continue;
   }
   // Include events starting today or later.
   if ($eventDate < $today) {
       continue;
   }
   if ($title ===  || isset($seen[$path])) {
       continue;
   }
   $seen[$path] = true;
   $events[] = [
       'title' => $title,
       'url' => 'https://upd.edu.ph' . $path,
       'date' => $dateString,
   ];

}

// Sort by event start date, earliest first. usort($events, function ($a, $b) {

   return strcmp($a['date'], $b['date']);

});

respond(array_slice($events, 0, 5));