0

I'm trying to retrieve an enclosure's URL via PHP, but am getting an error that says the enclosure is a non-object. This code works when I run it locally, but doesn't seem to work when uploaded to my server and run directly on the site. It's being inserted into an Expression Engine page, fwiw.

$rss = new DOMDocument();
$rss->load('http://www.blueskyresumes.com/blog/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,
                        'creator' => $node->getElementsByTagName('creator')->item(0)->nodeValue,
                        'image' => $node->getElementsByTagName('enclosure')->item(0) ? $node->getElementsByTagName('enclosure')->item(0)->getAttribute('url') : '',
                        );
                    array_push($feed, $item);
}

$limit = 3;
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']));
                    $author = $feed[$x]['creator'];
                    $image = $feed[$x]['image'];
                    echo '<span><img src="'. $image . '" /></span><h4 class="blog_title"><a href="'.$link.'" title="'.$title.'">'.$title.'</a><br />';
                    echo '<small><em>by '.$author.'</em></small></h4>';
}
hakre
  • 193,403
  • 52
  • 435
  • 836
  • probably your tag "item" is not found ( or less then 3 elements ), there is no check on the length of the $feed, it's being assumed in the code it will be at least 3 elements deep, I would print_r( $feed ). etc. to see. Also just efficiency wise, I would do it all in the foreach loop, instead of pulling all the items then only showing 3 of them, unless you need them all somewhere else. – ArtisticPhoenix Apr 10 '15 at 15:57
  • @ArtisiticPhoenix I'm not sure why the "item" wouldn't be found, and there are definitely more than 3 (10 to be exact). Any thoughts? – Chase Livingston Apr 10 '15 at 19:27
  • Casing is always the first thing I think of when a problem only happens on a Live Server, are you on Windows and the server is Linux, then casing can matter, you may not be loading what you think you are loading. – ArtisticPhoenix Apr 10 '15 at 20:15
  • I'm not sure either why the "item" wouldn't be found, but rest assurred that if any of those elements you access via `item(0)`are not found, that method returns `null` which causes the exact error you report. You might have missed that we keep an error reference on site, so I close against it as a duplicate until you can provide some more information which part of the error message you do not specifically understand and whay and how you've checked that on the remove system. - just seeing there was the more fitting duplicate already (please only ask questions about your own code here on SO). – hakre Apr 11 '15 at 17:16
  • The PHP error reference is here: http://stackoverflow.com/q/12769982/367456 – hakre Apr 11 '15 at 17:18

0 Answers0