Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Parse rss feed

Posted on 2011-09-04
6
Medium Priority
?
610 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

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!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…
Suggested Courses

609 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