2

I am using this to parse content from an 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'];
}

and assigning the content to variables.

All of them are working except for the one. Why is this?

Someone please help me fix this :)

Alexander Yancharuk
  • 13,817
  • 5
  • 55
  • 55
MarkP
  • 2,546
  • 5
  • 31
  • 48
  • 2
    You used `content:encoded` as a tag name in your XML? `content:` here may be treated as namespace. Check [getElementxByTagName](http://www.php.net/manual/en/domelement.getelementsbytagnamens.php) and see if it helps. – Passerby Nov 16 '12 at 03:53
  • 1
    http://stackoverflow.com/questions/3346628/retrieving-rss-feed-with-tag-contentencoded – fortytwo Jul 18 '13 at 18:53

2 Answers2

3

Having a similar issue I followed the link mentioned by fortytwo and found this answer Which was not working as it was shown (will be submitting an edit on that answer), though with a few minor edits I was able to get it to work as desired. This makes it super simple.

<?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);
Community
  • 1
  • 1
CrandellWS
  • 2,708
  • 5
  • 49
  • 111
0

Code PHP If you want to get data from feed 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>";
            }
        }
      ?> 

See exemple

lookly Dev
  • 325
  • 3
  • 5