2014-06-30 30 views
-1

我正在查看我的网站rss在不同页面上的侧栏中提供。我猜我应该使用PHP,但我不是100%确定。这是与RSS页面饲料http://unleashedinteractivestudios.pcriot.com/feed/,这是同方棒http://unleashedinteractivestudios.pcriot.com/ 下面的页面是我的边栏的代码:使用php查看网页中的RSS源?

<div class='sidebar'> RSS FEED</div> 

编辑:

我加入这个代码,但它只是显示没有帖子的错误日期。

<?php 
    $rss = new DOMDocument(); 
    $rss->load('unleashedinteractivestudios.pcriot.com/feed/'); 
    $feed = array(); 
    foreach ($rss->getElementsByTagName('item') as $node) { 
    $item = array (
    'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 
    'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue, 
    'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, 
    'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue, 
); 
    array_push($feed, $item); 
    } 
    $limit = 5; 
    for($x=0;$x<$limit;$x++) { 
    $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']); 
    $link = $feed[$x]['link']; 
    $description = $feed[$x]['desc']; 
    $date = date('l F d, Y', strtotime($feed[$x]['date'])); 
    echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />'; 
    echo '<small><em>Posted on '.$date.'</em></small></p>'; 
    echo '<p>'.$description.'</p>'; 
    } 

看看它here看看我的意思。

+1

都不清楚..你的代码不会奇迹般地拉你'RSS FEED'它需要知道'location','循环','如何显示'..我的建议你会保存自己一段时间,并使用一个插件,显示rss饲料 – vico

+0

我知道,代码不拉我的RSS源,这是我的侧栏代码,我想要我的RSS饲料来显示。我要求的是帮助让我的RSS饲料在那里显示。 – TonyC

回答

0

你已经采取了看herehere?它们都包含用PHP显示RSS提要的方法。

1

或者,您也可以使用SimpleXMLElement。考虑下面这个例子:

$data = array(); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'http://unleashedinteractivestudios.pcriot.com/feed'); // put the correct full url 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$raw_rss = curl_exec($ch); 
$rss = new SimpleXMLElement($raw_rss); 

foreach($rss->channel as $entry) { 

    // if you also want the namespaces 
    $namespaces = $entry->getNamespaces(true); 
    $sy = $entry->children($namespaces['sy']); 

    $data[] = array(
     'title' => (string) $entry->title, 
     'link' => (string) $entry->link, 
     'description' => (string) $entry->description, 
     'lastBuildDate' => (string) $entry->lastBuildDate, 
     'language' => (string) $entry->language, 
     'generator' => (string) $entry->generator, 
     'sy:updatePeriod' => (string) $sy->updatePeriod, 
     'sy:updateFrequency' => (string) $sy->updateFrequency, 
    ); 

} 

echo '<pre>'; 
print_r($data); 

应该产生这样的:

Array 
(
    [0] => Array 
     (
      [title] => Unleashed Interactive Studios » Page not found 
      [link] => http://unleashedinteractivestudios.pcriot.com 
      [description] => The Cooler Side of Coding 
      [lastBuildDate] => Mon, 30 Jun 2014 21:16:17 +0000 
      [language] => en-US 
      [generator] => http://wordpress.org/?v=3.9.1 
      [sy:updatePeriod] => hourly 
      [sy:updateFrequency] => 1 
     ) 

)