0
我有一個rss源,由雅虎管道創建,我需要從它隨機發布。如何才能實現這個在PHP?從rss feed獲取隨機帖子
我有一個rss源,由雅虎管道創建,我需要從它隨機發布。如何才能實現這個在PHP?從rss feed獲取隨機帖子
使用XML Parser閱讀提要並將其放入數組中。然後,使用array_rand從數組中選取一個隨機項目。
<?
function load_xml_feed($feed)
{
global $RanVal;
$i= 1;
$FeedXml = simplexml_load_file($feed);
foreach ($FeedXml->channel->item as $topic) {
$title[$i] = (string)$topic->title;
$link[$i] = (string)$topic->link;
$description[$i] = (string)$topic->description;
$i++;
}
$randtopic = rand(2, $i);
$link = trim($link[$randtopic]);
$title = trim($title[$randtopic]);
$description = trim($description[$randtopic]);
$RanVal = array($title,$link,$description);
return $RanVal;
}
$rss = "http://www.sabaharabi.com/rss/rss.xml";
load_xml_feed($rss);
$link = $RanVal[1];
$title = $RanVal[0];
$description = $RanVal[2];
echo "<h1>".$title."</h1><h2>".$link."</h2><p>".$description."</p>";