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
Solved

Parse enclosure tag from rss feed

Posted on 2011-09-03
6
1,629 Views
Last Modified: 2012-05-12
I am pulling in a rss feed with this script and parsing the standard tags like so...

<?php
error_reporting(E_ALL);

$xml = file_get_contents('http://feedurlhere');

// MUNG THE XML BECAUSE WE DO NOT HAVE THE NAMESPACE INFORMATION
$xml = str_replace('content:encoded', 'content__encoded', $xml);
$xml = str_replace('isc:',            'isc__',            $xml);

// CONVERT THE XML TO AN OBJECT
$obj = SimpleXML_Load_String($xml, 'SimpleXMLElement', LIBXML_NOCDATA);


// GET SOME FIELDS
$item_title = $obj->channel->item->title;
$item_link = $obj->channel->item->link;
$item_description = $obj->channel->item->description;

echo "$item_image<br/><strong>$item_title</strong> $item_description<br/>";

?>

Here is my problem... I want to grab the url within this enclosure tag and I am not sure how... $obj->channel->item->enclosure does not get me there.

<enclosure url="http://domain.com/blog/wp-content/uploads/2011/08/Arms21-150x150.jpg" length="6060" type="image/jpg" />
0
Comment
Question by:pixelscape
  • 2
  • 2
  • 2
6 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 36479134
A feed with a name like feedurlhere or a URL with a name like domain.com is almost completely useless.  Please post the actual URL of the feed so we can see the actual contents of the XML statements in the feed.  Then we can show you how to parse the feed and extract the URL.  It is not the same for every feed, and we need to be able to read the XML in order to answer you correctly. Thanks, ~Ray
0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 36479500
If you want the url as an "object" use:
$attrib= $obj->channel->item->enclosure->attributes()->url

If you just want the "string" value, then use:
echo (string)$obj->channel->item->enclosure->attributes()->url
0
 

Author Comment

by:pixelscape
ID: 36479516
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 82

Expert Comment

by:hielo
ID: 36479547
<?php
error_reporting(E_ALL);

$xml = file_get_contents('http://feedurlhere');

// MUNG THE XML BECAUSE WE DO NOT HAVE THE NAMESPACE INFORMATION
$xml = str_replace('content:encoded', 'content__encoded', $xml);
$xml = str_replace('isc:',            'isc__',            $xml);

// CONVERT THE XML TO AN OBJECT
$obj = SimpleXML_Load_String($xml, 'SimpleXMLElement', LIBXML_NOCDATA);


// GET SOME FIELDS
foreach($obj->channel->item as $item)
{
  $item_title = $item->title;
  $item_link = $item->link;
  $item_description = $item->description;
  $item_enclosure = $item->enclosure->attributes()->url;
  echo "$item_image<br/><strong>$item_title</strong> $item_description<br/>".$item_enclosure;
}
?>
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 36481272
http://www.laprbass.com/RAY_temp_pixelscape.php
Outputs something like this:
Latisse Rebate: http://dermrealm.com/blog/wp-content/uploads/2011/08/favicon_logo.png
Slimmer Arms with VASER Liposuction: http://dermrealm.com/blog/wp-content/uploads/2011/08/Arms21-150x150.jpg
NonSurgical FaceLift: http://dermrealm.com/blog/wp-content/uploads/2011/08/Neck1-150x150.jpg
WEEKEND PEEL: NO ENCLOSURE FOR THIS ONE
DYSPORT: NO ENCLOSURE FOR THIS ONE
Obagi NuDerm Healthy Skin Protection SPF 35: NO ENCLOSURE FOR THIS ONE

HTH, ~Ray
<?php // RAY_temp_pixelscape.php
error_reporting(E_ALL);

$url = 'http://dermrealm.com/blog/category/monthly-specials/feed/';
$xml = file_get_contents($url);

// MUNG THE XML BECAUSE WE DO NOT HAVE THE NAMESPACE INFORMATION
$xml = str_replace('content:encoded', 'content__encoded', $xml);
$xml = str_replace('isc:',            'isc__',            $xml);

// CONVERT THE XML TO AN OBJECT
$obj = SimpleXML_Load_String($xml, 'SimpleXMLElement', LIBXML_NOCDATA);

// GET TITLE AND ENCLOSURE IF THERE IS ONE
foreach($obj->channel->item as $i)
{
    $t = (string)$i->title;
    $e = (string)$i->enclosure["url"];
    if (!$e) $e = 'NO ENCLOSURE FOR THIS ONE';
    echo "<br/>$t: $e";
}

Open in new window

0
 

Author Comment

by:pixelscape
ID: 36482509
Got it working with this...   $item_image = $item->enclosure->attributes()->url;


Thanks!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

856 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