27

I have the following snippet of code:

function getFeed($feed_url) {

$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);

echo "<ul>";

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

echo "</ul>";
}

It works EXCEPT the $entry->content

That part doesn't register. In the actual feed the tag is listed as <content:encoded> but I can't get it to feed. Any suggestions?

kylex
  • 14,178
  • 33
  • 114
  • 175

6 Answers6

46

The Tag name here is "encoded". Try this:

$url = 'put_your_feed_URL';

    $rss = new DOMDocument();
    $rss->load($url);
    $feed = array();
    foreach ($rss->getElementsByTagName('item') as $node) {
        $item = array (
                'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
                'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
                'pubDate' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
                'description' => $node->getElementsByTagName('description')->item(0)->nodeValue,
                'content' => $node->getElementsByTagName('encoded')->item(0)->nodeValue

                );
        array_push($feed, $item);
    }
Arindam Mani Das
  • 608
  • 5
  • 10
44

In <content:encoded>, content is the namespace and encoded is the tag name.

You have to use SimpleXMLElement::children. See the output of

var_dump($entry->children("content", true));
Artefacto
  • 96,375
  • 17
  • 202
  • 225
  • 1
    Thank you for explaining what the tag meant along with your answer. I was trying to solve this parsing issue with a different library (TouchXML on iPhone) and this helped me get it working. Commenting so that this will show on on searches for that library as well. :) – Jonathan Zhan Mar 02 '11 at 15:40
12

I'll suggest you the following code:

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>";
        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>";
    }

Hope this works for you.

fortytwo
  • 580
  • 4
  • 9
  • I improved this answer for another question see http://stackoverflow.com/a/17731547/1815624 – CrandellWS Jul 26 '15 at 05:09
  • I am trying to fetch data from the medium using this URL - blog.botreetechnologies.com/feed. Got title, blog URL and publish date. But unable to fetch the description and an image. For the description, it's a

    tag. So how I can get the description and the cover image?

    – Parthiv Jul 26 '17 at 07:28
6

.... PHP example

<?php 
// --------------------------------------------------------------------

$feed_url = 'http://www.tagesschau.de/xml/rss2'; 
$xml_data = simplexml_load_file($feed_url);

// -------------------------------------------------------------------- 

$i=0; 
foreach($xml_data->channel->item as $ritem) { 

// -------------------------------------- 

$e_title       = (string)$ritem->title; 
$e_link        = (string)$ritem->link; 
$e_pubDate     = (string)$ritem->pubDate; 
$e_description = (string)$ritem->description; 
$e_guid        = (string)$ritem->guid; 

$e_content     = $ritem->children("content", true);
$e_encoded     = (string)$e_content->encoded; 

$n = ($i+1);

// -------------------------------------- 

print '<p> ---------- '. $n .' ---------- </p>'."\n";

print "\n"; 
print '<div class="entry" style="margin:0 auto; padding:4px; text-align:left;">'."\n"; 
print '<p> Title: '. $e_title .'</p>'."\n"; 
print '<p> Link:  '. $e_link .'</p>'."\n"; 
print '<p> Date:  '. $e_pubDate .'</p>'."\n"; 
print '<p> Desc:  '. $e_description .'</p>'."\n"; 
print '<p> Guid:  '. $e_guid .'</p>'."\n"; 
print '<p> Content: </p>'."\n"; 
print '<p style="background:#DEDEDE">'. $e_encoded .'</p>'."\n"; 
print '</div>'."\n"; 


// -------------------------------------- 

print '<br />'."\n"; 
print '<br />'."\n";

$i++; 
} 

// -------------------------------------------------------------------- 
?>

if you want to see the content HTML Source Code in your Browser, use eg:

print '<pre style="background:#DEDEDE">'. htmlentities($e_encoded) .'</pre>'."\n";

:=)

lukepress
  • 79
  • 1
  • 1
4

The working answer for this is just:

$e_content = $entry->children("content", true);
$e_encoded = (string)$e_content->encoded;
0

Using SimpleXmlElement or loading via simplexml_load_file you could access it via $entry->children("http://purl.org/rss/1.0/modules/content/")->encoded, just remember to cast it to a string:

foreach($x->channel->item as $entry) {
    echo "<li><a href='$entry->link' title='$entry->title'>" . $entry->title . "</a></li>";
    echo "<li>" . (string)$entry->children("http://purl.org/rss/1.0/modules/content/")->encoded . "</li>";

}