Extracting certain item from rss using simplexml

Discussion in 'PHP' started by lasserh, Apr 6, 2010.

  1. #1
    Hi all,
    I'm trying to parse an rss feed and only extract a certain item that has a specific title. The title I'm looking for is set by $_GET[''] and I whant alle the information from that item to be stored in a variable.

    It is very easy to parse and output the entire rss feed using simpleXML, but when I try to use an if-condition, such as
    if($this->title=$_GET['what']){echo $this->title;}
    Code (markup):
    I get alle the items only with the titles changed to the $_GET['what'] value.

    Here's my code:
    <?php // Load and parse the XML document 
    $rss =  simplexml_load_file('http://www.myfeedrss.com/feed);
    $title =  $rss->channel->title;
    ?>
    <html xml:lang="en" lang="en">
    <head>
      <title><?php echo $title; ?></title>
    </head>
    <body>
    
    <h1><?php echo $title; ?></h1>
    
    <?php
    // Here we'll put a loop to include each item's title and description
    foreach ($rss->channel->item as $item) {
    if($item->title=$_GET[who]){
      echo "<h2><a href='" . $item->link . "'>" . $item->title . "</a></h2>";
      echo "<p>" . $item->description . "</p>";
    
    }
    }
    
    ?>
    </body>
    </html>
    Code (markup):
    I'm sure it's easily possible to do what I whant, but I cannot find any support for it anywhere, so I hope someone can help me out here.

    Thanks in advance
     
    lasserh, Apr 6, 2010 IP
  2. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #2
    I'm not sure what you want to do, but check this example from the PHP manual


    I guess you can do the same and replace "//character" with the name of the XML element you want to select and then do whatever you want inside the foreach.
     
    nabil_kadimi, Apr 6, 2010 IP
  3. lasserh

    lasserh Guest

    Messages:
    8
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi nabil_kadimi and thanks for your reply.

    Each <item> in the rss feed contains a <title>, a <link>, and <description> and a <pubDate>. I can easilly parse the feed and display these elements as html. But I want to find an <item> that has a specifoc <title> and the display all the info from that <item> as html.
     
    lasserh, Apr 6, 2010 IP
  4. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #4
    This worked on my testing server

    <?php
    
    $rss =  simplexml_load_file('http://www.example.com/feed');
    $item_title = $_GET['title'];
    $xpath = "//channel/item[./title = '$item_title']";
    
    foreach ($rss->xpath($xpath) as $item) {
      echo $item->title;   # ...or whatever you want
      break;               # This assumes you need only one item
    }
    
    PHP:
    This requires that the $_GET['title'] matches the exact title on the rss feed, if you want to specify only a part of the title the script will need a lit bit more work
     
    Last edited: Apr 6, 2010
    nabil_kadimi, Apr 6, 2010 IP
  5. lasserh

    lasserh Guest

    Messages:
    8
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Fantastic! This works. Thank you very much, nabil_kadimi, this is exactly what I wanted!
     
    lasserh, Apr 6, 2010 IP
    nabil_kadimi likes this.