Solved

Parse enclosure tag from rss feed

Posted on 2011-09-03
6
1,575 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 108

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

867 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

15 Experts available now in Live!

Get 1:1 Help Now