KT-personal-website/app/Fetching/RssFeed.php
2024-10-22 12:26:08 +02:00

47 lines
1.3 KiB
PHP

<?php
namespace App\Fetching;
use App\Utils\Debug;
use DateTimeImmutable;
class RssFeed {
public $xml;
/**
* Loads XML data from the specified URL and stores it as a SimpleXMLElement object
*
* @param string $url The URL of feed
* @return object Returns the current object
* @throws \Exception If an error occurs while loading the XML
*/
public function load(string $url): object {
try {
/*
***** FOR LOCAL DEV ONLY *****
***** DISABLE VERIFICATION OF SSL *****
$context = stream_context_create(array('ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false
)));
libxml_set_streams_context($context);*/
$xml = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->xml = $xml;
return $this;
} catch (\Exception $e) {
echo 'Catch in : ', $e->getMessage(), "\n";
}
}
/**
* Retrieves the last update time from feed
*
* @return string The timestamp of the last update
*/
public function lastUpdate(): string {
$feedUpdateTime = new DateTimeImmutable((string)$this->xml->updated);
return $feedUpdateTime->format("U");
}
}