Connecting and Extracting data from XML/RSS data source

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mariocirillo
Forum Newbie
Posts: 8
Joined: Wed Jun 03, 2009 2:58 pm

Connecting and Extracting data from XML/RSS data source

Post by mariocirillo »

Hello,

Im mario - im new to the forum. Ive come from an ASP background and have only just started to tech myself PHP. So sorry you will have to bare with me. I must admit tho from what ive seen so far im very impressed.

if anyone could help me with the following it would be greatly appriciated.

i have an rss feed from a website:
http://www.fccuk.org/forum/cache/global_rss4.xml

On my php page i would like to display the first 4 tags / posts from the feed. I want to display the title as a hyperlink.

Im not sure how to go about this, ive found a few scripts on the net but have not really been able to work them out.

If anyone can help that would be fantastic,

Many Thanks,
Mario
Last edited by mariocirillo on Wed Jun 03, 2009 3:34 pm, edited 1 time in total.
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

Re: Connecting and Extracting data from XML/RSS data source

Post by divito »

What have you tried so far?
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Connecting and Extracting data from XML/RSS data source

Post by mikemike »

Magpie is generally the best in my experience. It's dead easy to use:
http://magpierss.sourceforge.net/
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

Re: Connecting and Extracting data from XML/RSS data source

Post by divito »

I second that. I use Magpie for a few different projects and it's really easy and functional.

There is also SimplePie, but I personally haven't implemented it yet.
mariocirillo
Forum Newbie
Posts: 8
Joined: Wed Jun 03, 2009 2:58 pm

Re: Connecting and Extracting data from XML/RSS data source

Post by mariocirillo »

thanks guys
i think ive nearly cracked it

my code below displays all of the titles and url, how can i get it to just show the first 5 titles and have them hyperlinked to open in a new window (_blank)

many thanks

Mario

Code: Select all

 
 
<?php
 
  $objDOM = new DOMDocument(); 
  $objDOM->load("http://www.fccuk.org/forum/cache/global_rss4.xml");
 
 
  $item = $objDOM->getElementsByTagName("item"); 
 
 
  foreach( $item as $value ) 
  { 
    $title = $value->getElementsByTagName("title"); 
    $f_title  = $title->item(0)->nodeValue; 
 
 
    $link = $value->getElementsByTagName("link"); 
    $f_link  = $link->item(0)->nodeValue; 
 
    echo "$f_title $f_link <br>"; 
  } 
 
 
?> 
 
 
Last edited by Benjamin on Tue Jun 09, 2009 11:45 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Connecting and Extracting data from XML/RSS data source

Post by mikemike »

hardcode it or loop through 5 times using a for-loop
mariocirillo
Forum Newbie
Posts: 8
Joined: Wed Jun 03, 2009 2:58 pm

Re: Connecting and Extracting data from XML/RSS data source

Post by mariocirillo »

any chance someone can help me to get the for loop to work, ive tried but i keep getting errors.
sorry,

mario
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Connecting and Extracting data from XML/RSS data source

Post by mikemike »

Show us your code and the errors
mariocirillo
Forum Newbie
Posts: 8
Joined: Wed Jun 03, 2009 2:58 pm

Re: Connecting and Extracting data from XML/RSS data source

Post by mariocirillo »

sorry :)

Code: Select all

 
<?php
 
  $objDOM = new DOMDocument(); 
  $objDOM->load("http://www.fccuk.org/forum/cache/global_rss4.xml");
  $item = $objDOM->getElementsByTagName("item"); 
  $coumt = 0
 
  foreach( $item as $value ) 
  { 
    if($count <=5) {
    $title = $value->getElementsByTagName("title"); 
    $f_title  = $title->item(0)->nodeValue; 
    $link = $value->getElementsByTagName("link"); 
    $f_link  = $link->item(0)->nodeValue; 
 
    echo "$f_title $f_link <br>"; 
  }
  } 
 
 
?> 
 
 
the error i get is:

Parse error: parse error in C:\xampp\htdocs\FCCUK\forum.php on line 15

I presume my if statement is causing it as it works fine when i take it out.
THanks for your time and patience.
Mario
Last edited by Benjamin on Tue Jun 09, 2009 5:11 pm, edited 1 time in total.
Reason: Changed code type from text to php.
mariocirillo
Forum Newbie
Posts: 8
Joined: Wed Jun 03, 2009 2:58 pm

Re: Connecting and Extracting data from XML/RSS data source

Post by mariocirillo »

its ok worked it out now!

I missed out a ; after $coumt = 0

Grrr!

Thankyou for your help guys :)
mariocirillo
Forum Newbie
Posts: 8
Joined: Wed Jun 03, 2009 2:58 pm

Re: Connecting and Extracting data from XML/RSS data source

Post by mariocirillo »

Ive come to upload my site now and been advised i need to try a parser that doesnt use DOM as for somereason when i test it on the live site it just doesnt work. I dont get an error, just a blank space where it should be?

Any ideas? or advise on what i need to change so as not to use DOM?

Thanks

MArio
Post Reply