?
Solved

Parse rss feed

Posted on 2011-09-04
6
Medium Priority
?
600 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
Tutorial: Introduction to Managing a Linux Server

In this tutorial on systemd, we will explore:
-OS/Distro Adoption
-chkconfig and Other Legacy Commands
-Summary and Key Commands

 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …
Suggested Courses

762 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