Welcome to Geeklog, Anonymous Thursday, November 28 2024 @ 07:16 am EST
Geeklog Forums
RDF/RSS Feed in static page.
Status: offline
wlparks
Forum User
Junior
Registered: 02/21/03
Posts: 24
I made this little hack because I wanted to use geeklogs XML parser for a news feed in a static page. So heres a little workaround.
0. Backup your database
1. Make a portal block with all the correct information, and uncheck the enabled box.
2. From the blocks section of your admin options take note of the block id by looking at the link where you edit the page. It'll say something like http://www.webpage.com/admin/block.php?mode=edit&bid=22 .. bid is your block id.
3. Insert the following code into the bottom of your lib-custom.php file in your system folder.
// Custom RDF / RSS feed on static page
function static_rdf($blockid = false) {
$error = array();
if ($blockid != false && is_numeric($blockid) && $blockid != "") {
global $_TABLES;
if (DB_getItem( $_TABLES['blocks'], 'type', "bid = '{$blockid}'" ) == 'portal') {
$sql = "SELECT UNIX_TIMESTAMP(rdfupdated) as date, content, rdfurl FROM {$_TABLES['blocks']} WHERE bid = '" . $blockid . "'";
$result = DB_query( $sql );
$A = DB_fetchArray( $result );
if ( COM_rdfCheck($blockid, $A['rdfurl'], $A['date'] ))
{
$A['content'] = DB_getItem( $_TABLES['blocks'], 'content',
"bid = '{$blockid}'" );
}
$return = $A['content'];
} else {
$error[] = 'Block is not of Portal Type';
}
} else {
$error[] = 'Is not a valid BlockID';
}
if (count($error) > 0) {
$return = "Something is wrong with the blockid that you entered. Fix this. Otherwise there could be issues";
$return .= '<ul>';
for ( $x = 0; count($error) > $x; $x++ ) {
$return .= '<li>' . $error[$x] . '</li>';
}
$return .= "</ul>";
}
return $return;
}
4. create a static page that has php enabled.
simply put the following code in the page
replace 82 with the number of your block.
Just so you know I'm not going to take reponsibility if bad things happen with this, they shouldn't, I have done quite a bit of error handling. . . but if they do its not my fault
0. Backup your database
1. Make a portal block with all the correct information, and uncheck the enabled box.
2. From the blocks section of your admin options take note of the block id by looking at the link where you edit the page. It'll say something like http://www.webpage.com/admin/block.php?mode=edit&bid=22 .. bid is your block id.
3. Insert the following code into the bottom of your lib-custom.php file in your system folder.
Text Formatted Code
// Custom RDF / RSS feed on static page
function static_rdf($blockid = false) {
$error = array();
if ($blockid != false && is_numeric($blockid) && $blockid != "") {
global $_TABLES;
if (DB_getItem( $_TABLES['blocks'], 'type', "bid = '{$blockid}'" ) == 'portal') {
$sql = "SELECT UNIX_TIMESTAMP(rdfupdated) as date, content, rdfurl FROM {$_TABLES['blocks']} WHERE bid = '" . $blockid . "'";
$result = DB_query( $sql );
$A = DB_fetchArray( $result );
if ( COM_rdfCheck($blockid, $A['rdfurl'], $A['date'] ))
{
$A['content'] = DB_getItem( $_TABLES['blocks'], 'content',
"bid = '{$blockid}'" );
}
$return = $A['content'];
} else {
$error[] = 'Block is not of Portal Type';
}
} else {
$error[] = 'Is not a valid BlockID';
}
if (count($error) > 0) {
$return = "Something is wrong with the blockid that you entered. Fix this. Otherwise there could be issues";
$return .= '<ul>';
for ( $x = 0; count($error) > $x; $x++ ) {
$return .= '<li>' . $error[$x] . '</li>';
}
$return .= "</ul>";
}
return $return;
}
4. create a static page that has php enabled.
simply put the following code in the page
Text Formatted Code
<br />$return = static_rdf('82');<br />return $return;<br />replace 82 with the number of your block.
Just so you know I'm not going to take reponsibility if bad things happen with this, they shouldn't, I have done quite a bit of error handling. . . but if they do its not my fault
22
13
Quote
Status: offline
jackbox
Forum User
Newbie
Registered: 12/24/05
Posts: 5
Right now this function is only showinng the headline of the RSS feed stories. How can I make it display the introductory text also (the text is in the portal block) but the function is only returning the headers. Thanks.
16
13
Quote
Status: offline
beewee
Forum User
Full Member
Registered: 08/05/03
Posts: 969
Location:The Netherlands, where else?
I'm using this, if you put in multiple feed URL's it 'rotates' the feeds, but you can use it for a single feed as well. Sometimes you get a problem if a feed contains HTML tags. Just put it in a PHP-enabled static page, that's all.
$tag = "";
$title = "";
$description = "";
$link = "";
$locations = array('feed1URL','feed2URL','feed3URL');
srand((float) microtime() * 10000000); // seed the random gen
$random_key = array_rand($locations);
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("<a href='%s' target=new><b>%s</b></a>",
trim($link),htmlspecialchars(trim($title)));
printf("<dt>%s<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($locations[$random_key], '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);
Dutch Geeklog sites about camping/hiking:
www.kampeerzaken.nl | www.campersite.nl | www.caravans.nl | www.caravans.net
Text Formatted Code
$insideitem = false;$tag = "";
$title = "";
$description = "";
$link = "";
$locations = array('feed1URL','feed2URL','feed3URL');
srand((float) microtime() * 10000000); // seed the random gen
$random_key = array_rand($locations);
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("<a href='%s' target=new><b>%s</b></a>",
trim($link),htmlspecialchars(trim($title)));
printf("<dt>%s<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($locations[$random_key], '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);
Dutch Geeklog sites about camping/hiking:
www.kampeerzaken.nl | www.campersite.nl | www.caravans.nl | www.caravans.net
13
14
Quote
Status: offline
graffixjones
Forum User
Newbie
Registered: 01/12/06
Posts: 5
Heya beewee,
I really like your RSS implementation, but have run into a dilemma. You mention that your code has problems with html marked-up feeds, and I was wondering if there's a way around the problem. The reason I'm asking is that I'd like to add a Google news feed to my site, and of course they use html marked-up feeds, and it appears that the xml parser is subbing in ascii codes for all the markup, and displaying essentially the 'raw' html in the feed body.
I was hoping that I could use some sort of 'convert_to_html' function after parsing so that I can coerce all the markup back to it's original state, so that the feeds all display properly. Is this even possible? I assume that if it was, you would have incorporated it into your original code, but I thought I'd ask anyway.
Thanks.
I really like your RSS implementation, but have run into a dilemma. You mention that your code has problems with html marked-up feeds, and I was wondering if there's a way around the problem. The reason I'm asking is that I'd like to add a Google news feed to my site, and of course they use html marked-up feeds, and it appears that the xml parser is subbing in ascii codes for all the markup, and displaying essentially the 'raw' html in the feed body.
I was hoping that I could use some sort of 'convert_to_html' function after parsing so that I can coerce all the markup back to it's original state, so that the feeds all display properly. Is this even possible? I assume that if it was, you would have incorporated it into your original code, but I thought I'd ask anyway.
Thanks.
17
17
Quote
Status: offline
beewee
Forum User
Full Member
Registered: 08/05/03
Posts: 969
Location:The Netherlands, where else?
Well, I'm no developer, and it's not my code either, and I got the same problem sometimes. Perhaps you could use a service like Feedburner to get rid of the HTML, or to convert it to HTML.
Dutch Geeklog sites about camping/hiking:
www.kampeerzaken.nl | www.campersite.nl | www.caravans.nl | www.caravans.net
Dutch Geeklog sites about camping/hiking:
www.kampeerzaken.nl | www.campersite.nl | www.caravans.nl | www.caravans.net
30
17
Quote
All times are EST. The time is now 07:16 am.
- Normal Topic
- Sticky Topic
- Locked Topic
- New Post
- Sticky Topic W/ New Post
- Locked Topic W/ New Post
- View Anonymous Posts
- Able to post
- Filtered HTML Allowed
- Censored Content