Solved

RSS Feed Parsing for Images

Posted on 2016-08-26
5
45 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:N R
  • 3
  • 2
5 Comments
 
LVL 108

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 11

Author Comment

by:N R
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 108

Accepted Solution

by:
Ray Paseur earned 500 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 11

Author Closing Comment

by:N R
ID: 41774795
Great, thanks!
0
 
LVL 108

Expert Comment

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now