Solved

Parse enclosure tag from rss feed

Posted on 2011-09-03
6
1,665 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
clicking to find my folders on wamp 8 47
Why isn't my DIV and Form centering? 1 30
Get data from two MySQL tables 6 66
php hashing methods 3 50
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 dynamically set the form action using jQuery.

751 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