?
Solved

RSS Feed Parsing for Images

Posted on 2016-08-26
5
Medium Priority
?
283 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
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month15 days, 22 hours left to enroll

850 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