php-llm-agent/lib/actions/rss_first_title.action.php
Frederico @ VilaRosa02 436e0e57c5 new version
2025-09-10 11:40:03 +00:00

41 lines
1.1 KiB
PHP

<?php
registerAction((new Action())->setFunctionName("getFirstTitleFromRSSFeed")->setArgumentsTypes(["url"]));
function getFirstTitleFromRSSFeed(string $url) {
// Initialize cURL
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_TIMEOUT => 10,
CURLOPT_USERAGENT => "PHP cURL RSS Reader"
]);
$response = curl_exec($ch);
if (curl_errno($ch)) die("cURL error: " . curl_error($ch));
curl_close($ch);
// Parse as XML
$xml = @simplexml_load_string($response);
if (!$xml) die("Failed to parse XML.");
// Find first <title>
$firstTitle = '';
if (isset($xml->channel->item[0]->title)) {
// RSS 2.0 style
$firstTitle = (string)$xml->channel->item[0]->title;
} elseif (isset($xml->entry[0]->title)) {
// Atom style
$firstTitle = (string)$xml->entry[0]->title;
} elseif (isset($xml->title)) {
// fallback
$firstTitle = (string)$xml->title;
}
return $firstTitle;
}