Help with this php code for RSS feed
10-20-2007, 02:23 PM
|
Help with this php code for RSS feed
|
Posts: 79
Name: ian
|
I want to display an RSS feed on my website and used this code:
Quote:
<?php
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $title, $description, $link;
if ($insideitem) {
$tag = $name;
} elseif ($name == "ITEM") {
$insideitem = true;
}
}
function endElement($parser, $name) {
global $insideitem, $tag, $title, $description, $link;
if ($name == "ITEM") {
printf("<dt><b><a href='%s'>%s</a></b></dt>",
trim($link),htmlspecialchars(trim($title)));
printf("<dt>%s</dt><br><br>",htmlspecialchars(trim($description))) ;
$title = "";
$description = "";
$link = "";
$insideitem = false;
}
}
function characterData($parser, $data) {
global $insideitem, $tag, $title, $description, $link;
if ($insideitem) {
switch ($tag) {
case "TITLE":
$title .= $data;
break;
case "DESCRIPTION":
$description .= $data;
break;
case "LINK":
$link .= $data;
break;
}
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("http://michaelthompson.org/news/goo-world.xml","r")
or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);
?>
|
It works fine, however further down the page I want to set up a seperate RSS feed and tried to use the same code again and got this error:
Cannot redeclare startelement() (previously declared in /home/collegec/public_html/testmain.php:121) in /home/collegec/public_html/testmain.php on line 193
The page URL is http://www.collegecolosseum.com/testmain.php
Any idea why this is happening?
Also, is there any way for me to shorten the "description" part of the feed to a certain number of characters?
Thanks.
|
|
|
|
10-20-2007, 03:04 PM
|
Re: Help with this php code for RSS feed
|
Posts: 843
Name: Mike
Location: United Kingdom
|
Whats on line 193 of your code?
Also, in future use the [php][/php] when showing PHP code, it makes life much more simple 
__________________
My Blog/Site: Please login or register to view this content. Registration is FREE
|
|
|
|
10-21-2007, 12:47 AM
|
Re: Help with this php code for RSS feed
|
Posts: 79
Name: ian
|
Quote:
Originally Posted by rogem002
Whats on line 193 of your code?
Also, in future use the when showing PHP code, it makes life much more simple 
|
Sorry, didn't know about the code.
line 193 just has a }
line 121 has function startElement($parser, $name, $attrs) {
|
|
|
|
10-21-2007, 02:42 AM
|
Re: Help with this php code for RSS feed
|
Posts: 31
Name: Rion
Location: Portland, Oregon
|
you don't need to rewrite the functions
just call them again down the page...once you declare the function once on the page (or any inculded file) you don't need to write the function again
|
|
|
|
10-21-2007, 03:21 AM
|
Re: Help with this php code for RSS feed
|
Posts: 79
Name: ian
|
Alright so for the first RSS feed I would just use the code above as is. However, what portion should I use for the second feed?
|
|
|
|
10-21-2007, 05:05 AM
|
Re: Help with this php code for RSS feed
|
Posts: 410
Name: Harry Burt
Location: Colchester, Essex, England
|
I would guess, having never tried this myself:
PHP Code:
$xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $fp = fopen("A different feed","r") or die("Error reading RSS data."); while ($data = fread($fp, 4096)) xml_parse($xml_parser, $data, feof($fp)) or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); fclose($fp); xml_parser_free($xml_parser);
Though you can probably keep some of the bits from the first section going for optimal speed.
Last edited by Foundationflash; 10-21-2007 at 05:06 AM..
|
|
|
|
10-21-2007, 01:02 PM
|
Re: Help with this php code for RSS feed
|
Posts: 79
Name: ian
|
Hey thats great it worked!
I did have to add <?php at the beginning and ?> at the end.
One more question. Is there anyway to specify the feed description to a certain length? say 70 characters?
|
|
|
|
10-23-2007, 07:55 AM
|
Re: Help with this php code for RSS feed
|
Posts: 410
Name: Harry Burt
Location: Colchester, Essex, England
|
Duh! You didn't actually say where you were going to put it.
As for the second question, I would take the line:
PHP Code:
printf("<dt>%s</dt><br><br>",htmlspecialchars(trim($description))) ;
and alter it to (though I haven't tried this myself):
PHP Code:
printf("<dt>%s</dt><br><br>",htmlspecialchars(trim(substr($description,0,70)))) ;
I would also use <br />s which validate instead of <br>s
Hope that works!
p.s. If either of this post or my last one were indeed useful, use the "Like my post?" button the the left of them. Much appreciated.
|
|
|
|
10-23-2007, 10:58 AM
|
Re: Help with this php code for RSS feed
|
Posts: 34
Name: DesignersForHire
Location: India
|
Why you are not using RSS feed creater which can be downloaded free?
|
|
|
|
10-23-2007, 11:57 AM
|
Re: Help with this php code for RSS feed
|
Posts: 410
Name: Harry Burt
Location: Colchester, Essex, England
|
Quote:
|
Why you are not using RSS feed creater which can be downloaded free?
|
I assume you mean creator. Also, many people would prefer to actually learn PHP and not just download stuff (especially when there's a BUY button). Plus this is just about displaying feeds not creating them.
|
|
|
|
10-23-2007, 12:46 PM
|
Re: Help with this php code for RSS feed
|
Posts: 79
Name: ian
|
Quote:
Originally Posted by Foundationflash
p.s. If either of this post or my last one were indeed useful, use the "Like my post?" button the the left of them. Much appreciated.
|
Thanks for the help. Your code worked flawlessly.
talkupation added! 
|
|
|
|
10-26-2007, 03:11 AM
|
Re: Help with this php code for RSS feed
|
Posts: 79
Name: ian
|
One last thing (promise). How would I go about adding a bullet to each listed item?
Foundationflash - I sent you a PM, hope you don't mind.
|
|
|
|
10-26-2007, 10:15 AM
|
Re: Help with this php code for RSS feed
|
Posts: 410
Name: Harry Burt
Location: Colchester, Essex, England
|
Of course not... As I say in my reply, you'll probably want to get rid of the htmlspecialchars() depending on where you want the bullets.
|
|
|
|
10-26-2007, 11:12 AM
|
Re: Help with this php code for RSS feed
|
Posts: 79
Name: ian
|
Is this the proper line to be using?
PHP Code:
('<ul><li>','</li><li>','</li></ul>');
This is where I placed it.
PHP Code:
<?php $insideitem = false; $tag = ""; $title = ""; $description = ""; $link = ""; function startElement($parser, $name, $attrs) { global $insideitem, $tag, $title, $description, $link; if ($insideitem) { $tag = $name; } elseif ($name == "ITEM") { $insideitem = true; } } function endElement($parser, $name) { global $insideitem, $tag, $title, $description, $link; if ($name == "ITEM") { printf("<dt><b><a href='%s'>%s</a></b></dt>", trim($link),htmlspecialchars(trim($title))); printf("<dt>%s</dt><br><br>",htmlspecialchars(trim($description))) ; $title = ""; $description = ""; $link = ""; $insideitem = false; } } function characterData($parser, $data) { global $insideitem, $tag, $title, $description, $link; if ($insideitem) { switch ($tag) { case "TITLE": $title .= $data; ('<ul><li>','</li><li>','</li></ul>'); break; case "DESCRIPTION": $description .= $data; break; case "LINK": $link .= $data; break; } } } $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $fp = fopen("http://michaelthompson.org/news/goo-world.xml","r") or die("Error reading RSS data."); while ($data = fread($fp, 4096)) xml_parse($xml_parser, $data, feof($fp)) or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); fclose($fp); xml_parser_free($xml_parser); ?>
|
|
|
|
10-28-2007, 05:30 AM
|
Re: Help with this php code for RSS feed
|
Posts: 410
Name: Harry Burt
Location: Colchester, Essex, England
|
Do you want all the titles to be bulleted?
|
|
|
|
10-29-2007, 02:39 AM
|
Re: Help with this php code for RSS feed
|
Posts: 79
Name: ian
|
Quote:
Originally Posted by Foundationflash
Do you want all the titles to be bulleted?
|
Yes, just the titles.
|
|
|
|
10-29-2007, 08:53 AM
|
Re: Help with this php code for RSS feed
|
Posts: 410
Name: Harry Burt
Location: Colchester, Essex, England
|
Hmm... This might work - I'll try it out later...
PHP Code:
<?php $currentlyinli = false; $currentlyindesc = false $insideitem = false; $tag = ""; $title = ""; $description = ""; $link = ""; function startElement($parser, $name, $attrs) { global $insideitem, $tag, $title, $description, $link; if ($insideitem) { $tag = $name; } elseif ($name == "ITEM") { $insideitem = true; } } function endElement($parser, $name) { global $insideitem, $tag, $title, $description, $link; if ($name == "ITEM") { printf("<dt><b><a href='%s'>%s</a></b></dt>", trim($link),htmlspecialchars(trim($title))); printf("<dt>%s</dt><br><br>",htmlspecialchars(trim($description))) ; $title = ""; $description = ""; $link = ""; $insideitem = false; } } function characterData($parser, $data) { global $insideitem, $tag, $title, $description, $link; if ($insideitem) { switch ($tag) { case "TITLE": if($currentlyinli == false){ $currentlyinli = true; $title .= "<ul><li>"; } $title .= $data; break; case "DESCRIPTION": if($currentlyinli == true){ $description .= "</li></ul>"; $currentlyindesc = true; $currentlyinli = false; } $description .= $data; break; case "LINK": $currentlyindesc = false; $link .= $data; break; } } } $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $fp = fopen("http://michaelthompson.org/news/goo-world.xml","r") or die("Error reading RSS data."); while ($data = fread($fp, 4096)) xml_parse($xml_parser, $data, feof($fp)) or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); fclose($fp); xml_parser_free($xml_parser); ?>
Not going to be perfect I admit, but might work.
|
|
|
|
10-29-2007, 12:40 PM
|
Re: Help with this php code for RSS feed
|
Posts: 79
Name: ian
|
FF - I gave that code a try, but recieved this error:
Parse error: syntax error, unexpected T_VARIABLE in /home/collegec/public_html/testmain2.php on line 127
line 127 contains this line:
$insideitem = false;
|
|
|
|
10-29-2007, 01:48 PM
|
Re: Help with this php code for RSS feed
|
Posts: 410
Name: Harry Burt
Location: Colchester, Essex, England
|
Sorry, I missed a ; After further study and a bit of thought
PHP Code:
<?php $currentlyinli = false; $currentlyindesc = false; $insideitem = false; $tag = ""; $title = ""; $description = ""; $link = ""; function startElement($parser, $name, $attrs) { global $insideitem, $tag, $title, $description, $link; if ($insideitem) { $tag = $name; } elseif ($name == "ITEM") { $insideitem = true; } } function endElement($parser, $name) { global $insideitem, $tag, $title, $description, $link; if ($name == "ITEM") { printf("<dt><b><a href='%s'>%s</a></b></dt>", trim($link),trim($title)); printf("<dt>%s</dt><br><br>",trim($description)) ; $title = ""; $description = ""; $link = ""; $insideitem = false; } } function characterData($parser, $data) { global $insideitem, $tag, $title, $description, $link, $currentlyinli, $currentlyindesc; if ($insideitem) { switch ($tag) { case "TITLE": if($currentlyinli == false){ $currentlyinli = true; $title .= "<ul><li>"; } $title .= $data; break; case "DESCRIPTION": if($currentlyinli == true){ $description .= "</li></ul>"; $currentlyindesc = true; $currentlyinli = false; } $description .= $data; break; case "LINK": $currentlyindesc = false; $link .= $data; break; } } } $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $fp = fopen("http://news.google.com/?ned=us&topic=s&output=rss","r") or die("Error reading RSS data."); while ($data = fread($fp, 4096)) xml_parse($xml_parser, $data, feof($fp)) or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); fclose($fp); xml_parser_free($xml_parser); ?>
seems to work. I have change the XML file to one that works as an example.
|
|
|
|
10-29-2007, 04:20 PM
|
Re: Help with this php code for RSS feed
|
Posts: 79
Name: ian
|
Thanks it worked!
|
|
|
|
|
« Reply to Help with this php code for RSS feed
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
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
|
|
|
|