Reply
Syndicating XML data into a website using PHP...
Old 07-07-2004, 10:42 PM Syndicating XML data into a website using PHP...
Junior Talker

Posts: 4
Ok, let's say we have the following XML document located at www.anothersite.com/rss.xml
Code:
<rss version="2.0">
<channel>
<item1>Something</item1>
<item2>Something else</item2>
<item1>Some data</item1>
</channel>
</rss>
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.
Jackal05 is offline
Reply With Quote
View Public Profile Visit Jackal05's homepage!
 
When You Register, These Ads Go Away!
     
Old 07-08-2004, 12:26 AM
cmonkey's Avatar
Ultra Talker

Posts: 268
here is part of the code I use to display various feeds from wired.com on one of my sites, this might get you started in the right direction

PHP Code:
// READ AND PARSE THE SOURCE FEED
$data implode(""file($url));
$parser xml_parser_create();
xml_parser_set_option($parserXML_OPTION_CASE_FOLDING1);
xml_parser_set_option($parserXML_OPTION_SKIP_WHITE1);
xml_parse_into_struct($parser$data$values$tags);
xml_parser_free($parser);

// ARRAYS TO STORE FEED INFO IN
$keys = array();
$titles = array();
$links = array();
$descriptions = array();

// EXTRACT DESIRED INFO AND STORE IN ARRAYS
foreach($values as $key=>$item) {
    if(
$item[tag] == "TITLE") {
        
$titles[$key] = $item[value];
        
$sub $key;
        
array_push($keys$key);
    }
    if(
$item[tag] == "LINK") {
        
$links[$sub] = $item[value];
    }
    if(
$item[tag] == "DESCRIPTION") {
        
$descriptions[$sub] = $item[value];
    }
    if(
$item[tag] == "URL") {
        
$image $item[value];
    }

With the example above, you can then display the data stored in the arrays however you want on your page

Now this is by no means the only way to parse a feed, but this was the easiest solution to implement in my case.

Here is a simplified version of the code I use to display the feed data:
PHP Code:
<?
// DISPLAY NEWS ARTICLES
for($n 2$n count($keys); $n++) {
$k $keys[$n];
echo 
"<a href='$links[$k]' target='_blank'><b>$titles[$k]</b></a><br>";
echo 
"$descriptions[$k]<br><br>";
}
?>

Last edited by cmonkey : 07-08-2004 at 12:29 AM.
cmonkey is offline
Reply With Quote
View Public Profile
 
Old 07-08-2004, 11:33 PM
Junior Talker

Posts: 4
Thanks man.
Finally after crawling around the net for a few weeks looking for an answer I found one.
This is way easier with Perl I don't know why it's not too common with PHP.
__________________
http://www.jackaldesigns.com/
Jackal05 is offline
Reply With Quote
View Public Profile Visit Jackal05's homepage!
 
Old 07-09-2004, 11:29 AM
ibbo's Avatar
Super Spam Talker

Posts: 880
Location: Leeds UK
Because PERL can do anything.

Did you get this to show only the criteria you want? If so can you post what you did.

Ibbo
ibbo is offline
Reply With Quote
View Public Profile Visit ibbo's homepage!
 
Old 07-09-2004, 10:17 PM
Junior Talker

Posts: 4
Right now Im working on a reconstruction of my site's code along with some major enhancements such as a new color scheme and new typography.
I was hoping to get started sooner but unfortuanately it will take some time. It's going to be a weather system using a feed from weather.com. My first priority is to enhance a set of weather icons I already have then move on to the coding part.
__________________
http://www.jackaldesigns.com/
Jackal05 is offline
Reply With Quote
View Public Profile Visit Jackal05's homepage!
 
Reply     « Reply to Syndicating XML data into a website using PHP...
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.13520 seconds with 13 queries