?
Solved

RSS Feed Parsing for Images

Posted on 2016-08-26
5
Medium Priority
?
184 Views
Last Modified: 2016-08-29
I'm trying to parse out an RSS feed for images. My original code grabbed the title, description, link, and pubDate just fine. I read a tutorial online on how to get the images out as well, but now that I've updated my pull it gives me the errror:

Fatal error: Call to a member function getAttribute() on a non-object in /home/ode/public_html/dev/cron-insights2.php on line 10

Here is my code:
<?php

$rss = new DOMDocument();
$rss->load('http://feeds.feedburner.com/feedburner/Dvzy?format=xml');
$feed = array();

//FOR EACH ITEM RETURN RSS
foreach ($rss->getElementsByTagName('item') as $node) {

    $img = $rss->getElementsByTagName('img')->item(0)->getAttribute('src');

    $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,
        'image' => $img,
    );
    array_push($feed, $item);
}

echo '<pre>';
var_dump($feed);
echo '</pre>';

Open in new window

0
Comment
Question by:Nathan Riley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41772215
Not sure if this is exactly what you want, but it might get you pretty close.
https://iconoun.com/demo/temp_gallitin.php
<?php // demo/temp_gallitin.php
/**
 * https://www.experts-exchange.com/questions/28965889/RSS-Feed-Parsing-for-Images.html
 */
error_reporting(E_ALL);
echo '<meta charset="utf-8" />';
echo '<pre>';

$url = 'http://feeds.feedburner.com/feedburner/Dvzy?format=xml';
$xml = file_get_contents($url);
$xml = str_replace('<content:encoded>', '<content_encoded>', $xml);
$xml = str_replace('</content:encoded>', '</content_encoded>', $xml);
$obj = SimpleXML_Load_String($xml, 'SimpleXMLElement', LIBXML_NOCDATA);

// ACTIVATE THIS TO SEE THE XML
// echo htmlentities($xml);

// ACTIVATE THIS TO SEE THE OBJECT
// var_dump($obj);

// COLLECT INFORMATION HERE
$feed = [];

// A REGULAR EXPRESSION THAT FINDS IMAGE TAGS SRC ATTRIBUTE
$rgx
= '~'                  // REGEX DELIMITER
. 'src="'              // SRC INSIDE IMAGE TAG
. '('                  // START CAPTURE GROUP
. '[^"]*'              // ANYTHING UP TO END
. ')'                  // ENDOF CAPTURE GROUP
. '"'                  // END OF SRC ATTRIBUTE
. '~'                  // REGEX DELIMITER
. 'i'                  // FLAG: CASE INSENSITIVE
. 's'                  // FLAG: SINGLE LINE
;

foreach ($obj->channel->item as $item)
{
    $node['title'] = (string)$item->title;
    $node['desc']  = (string)$item->description;
    $node['link']  = (string)$item->link;
    $node['date']  = (string)$item->pubDate;

    $content       = $item->content_encoded;
    preg_match_all($rgx, $content, $match);
    $node['image'] = $match[0];
    $feed[] = $node;
}
var_dump($feed);

Open in new window

0
 
LVL 12

Author Comment

by:Nathan Riley
ID: 41772328
Perfect, thanks Ray!

How can I have the image field only grab the first image as the image value instead of an array of them all?
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 41772484
First image only?  Like this.  See line 46.
<?php // demo/temp_gallitin.php
/**
 * https://www.experts-exchange.com/questions/28965889/RSS-Feed-Parsing-for-Images.html
 */
error_reporting(E_ALL);
echo '<meta charset="utf-8" />';
echo '<pre>';

$url = 'http://feeds.feedburner.com/feedburner/Dvzy?format=xml';
$xml = file_get_contents($url);
$xml = str_replace('<content:encoded>', '<content_encoded>', $xml);
$xml = str_replace('</content:encoded>', '</content_encoded>', $xml);
$obj = SimpleXML_Load_String($xml, 'SimpleXMLElement', LIBXML_NOCDATA);

// ACTIVATE THIS TO SEE THE XML
// echo htmlentities($xml);

// ACTIVATE THIS TO SEE THE OBJECT
// var_dump($obj);

// COLLECT INFORMATION HERE
$feed = [];

// A REGEX THAT FINDS IMAGE TAGS SRC ATTRIBUTE
$rgx
= '~'                  // REGEX DELIMITER
. 'src="'              // SRC INSIDE IMAGE TAG
. '('                  // START CAPTURE GROUP
. '[^"]*'              // ANYTHING UP TO END
. ')'                  // ENDOF CAPTURE GROUP
. '"'                  // END OF SRC ATTRIBUTE
. '~'                  // REGEX DELIMITER
. 'i'                  // FLAG: CASE INSENSITIVE
. 's'                  // FLAG: SINGLE LINE
;

foreach ($obj->channel->item as $item)
{
    $node['title'] = (string)$item->title;
    $node['desc']  = (string)$item->description;
    $node['link']  = (string)$item->link;
    $node['date']  = (string)$item->pubDate;

    $content       = $item->content_encoded;
    preg_match_all($rgx, $content, $match);
    $node['image'] = $match[0][0];
    $feed[] = $node;
}
var_dump($feed);

Open in new window

0
 
LVL 12

Author Closing Comment

by:Nathan Riley
ID: 41774795
Great, thanks!
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41774799
Glad to help!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question