Solved

Parse rss feed

Posted on 2011-09-04
6
570 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&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>


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
Comment Utility
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 16

Expert Comment

by:Chris Harte
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

728 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

10 Experts available now in Live!

Get 1:1 Help Now