我正在尝试从 RSS 提要创建一组项目。我试图通过呼应第一项的标题来测试它是否有效。到目前为止我一直没有成功......我非常感谢任何建议!
我有两个文件,一个“index.php”和一个“test.php”。
<!DOCTYPE html>
<html>
<head>
<link rel = "stylesheet" type= "text/css" href = "style.css">
</head>
<body>
<h1>TEST SLIDER</h1>
<p>First Title:<br>
<?php
include 'test.php';
$NPR_url = 'http://www.npr.org/rss/rss.php?id=1001';
$NPR = GetFeed($NPR_url);
echo $NPR[0]['title'];
?>
</p>
</body>
</html>
和'test.php'
<?php
function GetFeed($url){
$feed = new DOMDocument;
$feed->load($url);
$feed_array = array();
foreach($feed->getElementsByTagName('item') as $story){
$story_array = array (
'title' => $story->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $story->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $story->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $story->getElementsByTagName('pubDate')->item(0)->nodeValue
);
array_push($feed_array, $story_array);
}
return $feed_array;
}
?>