Solved

Parse rss feed

Posted on 2011-09-04
6
582 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
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
LVL 109

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 109

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
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.  …
The viewer will learn how to count occurrences of each item in an array.
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.

839 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