Syndicating XML data into a website using PHP...
07-07-04, 11:33 PM
Newbie Coder
Join Date: Sep 2003
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Syndicating XML data into a website using PHP...
Ok, let's say we have the following XML document located at
www.anothersite.com/rss.xml
Now I wanna syndicate some (items 1 and 3) of that information into my own site.
I have proficient PHP skills and I know that it is possible to use PHP to display data like that on a website. Can someone tell me how to?
Using PHP I wanna turn each tag in the XML file into a variable then include that on the site. All I need to know is how to get a tag from the XML document displayed on a HTML document.
To break it down let's say I wanna have a small box on my site like:
Just some info:
- Item One: Something
- Item Three: Some data
Using the data from the XML document.
Thanks.
07-09-04, 01:32 AM
Community VIP
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
there are plenty of RSS parser out there !
one of them is:
http://pear.php.net/package/XML_RSS
unfortunatly, parsing XML in php4 is kinda difficult, but in php5 the new extension SimpleXML makes reading XML files VERY easy ..
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9 ]
07-09-04, 06:53 AM
Community VIP
Join Date: Jan 2004
Location: Liverpool, England
Posts: 752
Thanks: 0
Thanked 0 Times in 0 Posts
Just an example is this:
PHP Code:
<?php /*************************************************************************** * nationstate.php * ------------------- * begin : Friday, 23, May, 2003 * copyright : (C) Robin Morrison (People's Republic of Winnipeg) * * ***************************************************************************/ /*************************************************************************** * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * ***************************************************************************/ //check to see if a nation has been specified if (isset( $_REQUEST [ 'nation' ])) { $nation = $_REQUEST [ 'nation' ]; } else { $nation = 'rapid_dr3am' ; //You can feel free to change this to the default nation of your choice. } // Open and read the XML Output $fileName = "http://www.nationstates.net/cgi-bin/nationdata.cgi/nation=" . $nation ; $size = "800" ; $fp = fopen ( $fileName , 'r' ); $text = fread ( $fp , $size ); fclose ( $fp ); //Create the Array of feed sections. Just use the ones you need. //To see all the avalible sections visit [url]http://www.nationstates.net/cgi-bin/nationdata.cgi/nation=xxx[/url] $section = array( 1 => "FULLNAME" , 2 => "MOTTO" , 3 => "CATEGORY" , 4 => "UNSTATUS" , 5 => "CIVILRIGHTS" , 6 => "ECONOMY" , 7 => "POLITICALFREEDOM" , 8 => "REGION" , 9 => "POPULATION" , 10 => "TAX" , 11 => "ANIMAL" , 12 => "CURRENCY" , 13 => "FLAG" , 14 => "MAJORINDUSTRY" , 15 => "GOVTPRIORITY" , 16 => "LASTLOGIN" , 17 => "LASTACTIVITY" , ); //Now run a foreach that goes through the array we just created. foreach ( $section as $var ) { $open = "<" . $var . ">" ; $close = "</" . $var . ">" ; $start = strpos ( $text , $open ); $end = strpos ( $text , $close ); $length = $end - $start ; $val = substr ( $text , $start , $length ); //Make sure that these if() statements reflect the options you used in the $section array if ( $var == 'FULLNAME' ) { $name = str_replace ( $open , '' , $val ); } if ( $var == 'MOTTO' ) { $motto = str_replace ( $open , '' , $val ); } if ( $var == 'CATEGORY' ) { $category = str_replace ( $open , '' , $val ); } if ( $var == 'UNSTATUS' ) { $unstatus = str_replace ( $open , '' , $val ); } if ( $var == 'CIVILRIGHTS' ) { $civilrights = str_replace ( $open , '' , $val ); } if ( $var == 'ECONOMY' ) { $economy = str_replace ( $open , '' , $val ); } if ( $var == 'POLITICALFREEDOM' ) { $political = str_replace ( $open , '' , $val ); } if ( $var == 'REGION' ) { $region = str_replace ( $open , '' , $val ); } if ( $var == 'POPULATION' ) { $pop = str_replace ( $open , '' , $val ); //This Just changes the Population Output to Billions if it is more than 1000 if ( $pop >= 1000 ) { $pop = round ( $pop / 1000 , 2 ); $postfix = "Billion" ; } else { $postfix = "Million" ; } } if ( $var == 'TAX' ) { $tax = str_replace ( $open , '' , $val ); } if ( $var == 'ANIMAL' ) { $animal = str_replace ( $open , '' , $val ); } if ( $var == 'CURRENCY' ) { $currency = str_replace ( $open , '' , $val ); } if ( $var == 'FLAG' ) { $flag = str_replace ( $open , '' , $val ); } if ( $var == 'MAJORINDUSTRY' ) { $industry = str_replace ( $open , '' , $val ); } if ( $var == 'GOVTPRIORITY' ) { $priority = str_replace ( $open , '' , $val ); } if ( $var == 'LASTLOGIN' ) { $lastlogin = str_replace ( $open , '' , $val ); } if ( $var == 'LASTACTIVITY' ) { $lastactivity = str_replace ( $open , '' , $val ); } } //Check to see if a Message has been set ($msg) if (isset( $msg )) { echo "<font color=\"red\" size=\"3\">" . $msg . "</font><br><br>" ; } ?> <table border="0"> <tr> <td valign="top" align="right"><font color="FFFFFF">FLAG:</font></td> <td align="center"><img src="<? print( $flag ); ?> " style="border: 0 solid black;"></td> </tr><tr> <td align="right"><font color="FFFFFF">Full Name:</font></td> <td><? print( $name ); ?> </td> </tr><tr> <td align="right"><font color="FFFFFF">Motto:</font></td> <td><? print( $motto ); ?> </td> </tr><tr> <td align="right"><font color="FFFFFF">Category:</font></td> <td><? print( $category ); ?> </td> </tr><tr> <td align="right"><font color="FFFFFF">UN Status:</font></td> <td><? print( $unstatus ); ?> </td> </tr><tr> <td align="right"><font color="FFFFFF">Civilrights:</font></td> <td><? print( $civilrights ); ?> </td> </tr><tr> <td align="right"><font color="FFFFFF">Economy:</font></td> <td><? print( $economy ); ?> </td> </tr><tr> <td align="right"><font color="FFFFFF">Political Freedoms:</font></td> <td><? print( $political ); ?> </td> </tr><tr> <td align="right"><font color="FFFFFF">Region:</font></td> <td><? print( $region ); ?> </td> </tr><tr> <td align="right"><font color="FFFFFF">Population:</font></td> <td><? print( $pop . " " . $postfix ); ?> </td> </tr><tr> <td align="right"><font color="FFFFFF">Tax:</font></td> <td><? print( $tax ); ?> %</td> </tr><tr> <td align="right"><font color="FFFFFF">Animal:</font></td> <td><? print( $animal ); ?> </td> </tr><tr> <td align="right"><font color="FFFFFF">Currency:</font></td> <td><? print( $currency ); ?> </td> </tr><tr> <td align="right"><font color="FFFFFF">Major Industry:</font></td> <td><? print( $industry ); ?> </td> </tr><tr> <td align="right"><font color="FFFFFF">Government Priority:</font></td> <td><? print( $priority ); ?> </td> </tr><tr> <td align="right"><font color="FFFFFF">Last Login:</font></td> <td><? print( $lastlogin ); ?> </td> </tr><tr> <td align="right"><font color="FFFFFF">Last Active:</font></td> <td><? print( $lastactivity ); ?> </td> </tr> </table>
__________________
Placeholder, place signature here.
07-16-04, 03:38 PM
Newbie Coder
Join Date: Jul 2004
Location: Singapore
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
I was facing the same concern too.
I posted a thread with my solution on this.
http://www.programmingtalk.com/showthread.php?t=11064
Hope you find it useful! :-)
__________________
God bless,
Benjamin Lim
BENRUTH SOFTWARE CONSULTANCY
Web Feed : http://benruth.com/feed.xml
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off