Solved

Parse enclosure tag from rss feed

Posted on 2011-09-03
6
1,542 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
Comment Utility
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
Comment Utility
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
Comment Utility
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 82

Expert Comment

by:hielo
Comment Utility
<?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
Comment Utility
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
Comment Utility
Got it working with this...   $item_image = $item->enclosure->attributes()->url;


Thanks!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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 look for a specific file type in a local or remote server directory using PHP.

744 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

18 Experts available now in Live!

Get 1:1 Help Now