Solved

Parse enclosure tag from rss feed

Posted on 2011-09-03
6
1,647 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
[X]
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
  • 2
  • 2
  • 2
6 Comments
 
LVL 110

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 110

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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.  …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 count occurrences of each item in an array.

756 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