获取内容:从RSS源编码

时间:2012-11-16 02:55:38

标签: php parsing rss feed

我用它来解析rss feed中的内容:

$rss = new DOMDocument();
$rss->load($feedurl);
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
    $item = array(
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'content' => $node->getElementsByTagName('content:encoded')
            ->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
        'guid' => $node->getElementsByTagName('guid')->item(0)->nodeValue,
    );
    array_push($feed, $item);
}
$limit = 1;
for ($x = 0; $x < $limit; $x++) {
    $title   = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
    $link    = $feed[$x]['link'];
    $content = $feed[$x]['content'];
    $guid    = $feed[$x]['guid'];
}

并将内容分配给变量。

除了那个之外,他们都在工作。为什么是这样?

有人请帮我解决这个问题:)

2 个答案:

答案 0 :(得分:3)

遇到类似的问题,我按照fortytwo提到的链接 并找到了this answer哪个没有按照显示的方式工作(将提交一个关于该答案的编辑),虽然通过一些小的编辑,我能够让它按照需要运行。这使它变得非常简单。

<?php
$xml=("https://en.blog.wordpress.com/feed/");


function getFeed($feed_url) {
        $feeds = file_get_contents($feed_url);
        $feeds = str_replace("<content:encoded>","<contentEncoded>",$feeds);
        $feeds = str_replace("</content:encoded>","</contentEncoded>",$feeds);
        $rss = simplexml_load_string($feeds);

    echo "<ul>";
    $x=$rss;
        foreach($x->channel->item as $entry) {
            echo "<li><a href='$entry->link' title='$entry->title'>" . $entry->title . "</a></li>";
            echo "<li>$entry->contentEncoded</li>";
        }
    echo "</ul>";
}

getFeed($xml);

答案 1 :(得分:0)

代码 PHP 如果您想从提要 rss 中获取数据:

   <?php
        $invalidurl = false;
        if(@simplexml_load_file($url)){

           $feeds = file_get_contents($url);
        $feeds = str_replace("<content:encoded>","<contentEncoded>",$feeds);
        $feeds = str_replace("</content:encoded>","</contentEncoded>",$feeds);
        $feeds = simplexml_load_string($feeds);



          //  $feeds = simplexml_load_file($feeds);
        }else{
            $invalidurl = true;
            echo "<h2>Invalid RSS feed URL.</h2>";
        }


        $i=0;
        if(!empty($feeds)){
          
            $site = $feeds->channel->title;
            $sitelink = $feeds->channel->link;
            echo '<h1>'.$site.'</h1><div class="row">';
            foreach ($feeds->channel->item as $item) {

                $title = $item->title;
                $link = $item->link;
                $description = $item->description;
                $postDate = $item->pubDate;
                $content = $item->contentEncoded;
                $pubDate = date('D, d M Y',strtotime($postDate));

                if($i>=20) break;      
        
 ?>         
           <div class="col-12 p-3 GeetMark-col">
                  <div class="p-1">
                    <div class="row">
                      <div class="col-sm-5 col-md-4">
                        <div class="position-relative h-sm-100"><a class="d-block h-100" href="<?= $link ?>"><img class="img-fluid fit-cover w-sm-100 h-sm-100 rounded absolute-sm-centered" src="<?= $meta_img ?>" alt="<?= $title ?>" /></a></div>
                      </div>
                      <div class="col-sm-7 col-md-8 GeetMark-col-sm">
                        <div class="">
                           
                            <h5 class="mt-3 mt-sm-0"><a class="text-dark fs-0 fs-lg-1" href="<?= $link ?>"><?= $title ?></a></h5>
                            <span class="notification-time"><span class="mr-1" role="img" aria-label="Emoji">?</span><?= $pubDate ?> </span>
                            <p class="fs--1 mb-2 mb-md-3"> <?= $description ?> </p>
                            <hr>
                            <p class="fs--1 mb-2 mb-md-3"> <?= $content ?> </p>
                             
                        </div>
                      </div>
                    </div>
                  </div>
                </div>      
   <?php
                $i++;
            }
            echo "</div>";
        }else{
            if(!$invalidurl){
                echo "<h2>No item found</h2>";
            }
        }
      ?> 

exemple