Solved

Parse rss feed

Posted on 2011-09-04
6
584 Views
Last Modified: 2012-05-12
I have the following feed ...

<any_feed_product_list>
<product>
<name>Obagi Nu-Derm Healthy Skin Protection SPF 35 (6)</name>
-
<url>
http://dermrealm.com/shop/index.php?route=product/product&product_id=87;
</url>
<category>Featured Product</category>
<image>http://dermrealm.com/shop/image/data/A0208.jpg</image>
<price>55.0000</price>
<special_price>45.0000</special_price>
</product>
</any_feed_product_list>


I am not sure how to parse these feed tags... here's what I have tried:

<?php

// TEST DATA FROM THE POST AT EE
$xml = file_get_contents('http://72.10.39.100/shop/index.php?route=feed/any_feed');

// 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);


$item_cat = $obj->channel->product->category;      

echo "$item_cat";

?>


Thanks!

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
6 Comments
 
LVL 11

Expert Comment

by:f_o_o_k_y
ID: 36483110
Hello,
Why to write your owne parser when you can use what is already avaiable on web.

http://magpierss.sourceforge.net/

This one looks nice.

Best Regards
FooKy
0
 
LVL 17

Expert Comment

by:Chris Harte
ID: 36483653
Rss feeds have a common layout of [channel] [item].
This code will show it up
$url = "http://dermrealm.com/shop/index.php?route=feed/latest_products_rss&currency=USD";

$contents = file_get_contents($url);

$data = simplexml_load_string($contents);

//echo "<pre>";     // <-- uncomment these for the layout of the rss feed
//print_r($data);

foreach ($data->channel->item as $item)
{
    echo $item->title;
    echo $item->description;
    echo $item->link;
    echo "<br />";

}

Open in new window

0
 

Author Comment

by:pixelscape
ID: 36484963
Thanks guys, I have tried both of your suggestions and I am still not able to pull say the title tag from this feed and echo as a test. http://72.10.39.100/shop/index.php?route=feed/any_feed
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 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 36485336
This question seems to keep coming up in different variants over and over.  What you have posted here is not an RSS feed!  In addition, the URL you posted for us points to http://72.10.39.100/shop/index.php?route=feed/any_feed and that is not an RSS feed, either (tags named special and price are not RSS tags).  And there is no <title> tag in anything you posted here at EE.

For better or worse, computer programming is a fairly precise activity, sort of like flying an airplane.  Errors of terminology and misunderstandings of common practice are likely to create results that are suboptimal for everyone involved.

RSS is a specialized subset of XML, with a specialized set of XML tags.  You can learn what RSS is by reading the information here.  I urge you to do that.
http://cyber.law.harvard.edu/rss/rss.html

The general design pattern for using a random XML string (what you've posted here is a random XML string) is shown in the code snippet.   It is installed on my server here
http://www.laprbass.com/RAY_temp_pixelscape.php

The output looks something like this:
Obagi Nu-Derm Healthy Skin Protection SPF 35 (6): http://dermrealm.com/shop/image/data/A0208.jpg
<?php // RAY_temp_pixelscape.php
error_reporting(E_ALL);
echo "<pre>";


// THE XML STRING POSTED AT EE
$xml = <<<ENDXML
<any_feed_product_list>
<product>
<name>Obagi Nu-Derm Healthy Skin Protection SPF 35 (6)</name>
-
<url>
http://dermrealm.com/shop/index.php?route=product/product&amp;product_id=87;
</url>
<category>Featured Product</category>
<image>http://dermrealm.com/shop/image/data/A0208.jpg</image>
<price>55.0000</price>
<special_price>45.0000</special_price>
</product>
</any_feed_product_list>
ENDXML;


// CONVERT THE XML TO AN OBJECT
$obj = SimpleXML_Load_String($xml);

// ACTIVATE THIS TO SHOW HOW WE CAN SEE WHAT IS IN THE XML STRING, AFTER CONVERSION TO AN OBJECT
// var_dump($obj);

// GET TITLE AND ENCLOSURE IF THERE IS ONE
foreach($obj->product as $p)
{
    $t = (string)$p->name;
    $i = (string)$p->image;
    echo "<br/>$t: $i";
}

Open in new window

0
 

Author Comment

by:pixelscape
ID: 36485512
Thanks Ray, I figured it out. Thanks for the links I'll read up on this some more now that it's something I am having to develop more often. thanks
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36485935
Thanks for the points.  For what it's worth, most of the books I have read about PHP and XML are not very good.  My recommendation is to use var_dump() liberally and post your questions here at EE.  I think the EE community is more up-to-date than the authors of books about the topics.  Best regards, ~Ray
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

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.  …
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

730 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