Connecting Tech Pros Worldwide Help | Site Map

News feed webpage ?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 01:30 PM
John
Guest
 
Posts: n/a
Default News feed webpage ?

Hi everyone !

I am a newbie to this newsgroup. I would appreciate if someone can help
me to solve this problem.

OK, I am currently writing a webpage in PHP that will get RSS file(XML
file) (news feed) and parse it using PHP and display articles' link and
description. I want everytime a user click on one article's link, it
take the user to another page which was written by me (for example,
"display.php?aid=43232") (I don't want user to leave my website) and
pass the article ID to the URL string. Everything run fine at this
point but I got duplicate filename on the URL
("display.php?aid=43232&display.php="). I don't know how to fix this.

Here is the code.
================== Code for RSS parsing ======================
//This code was copied from the following tutorial.
//http://www.sitepoint.com/article/php-xml-parsing-rss-1-0

<html>
<head>
<title> The core </title>
</head>
<body>


<dl>
<?php

class RSSParser {

var $insideitem = false;
var $tag = "";
var $title = "";
var $description = "";
var $link = "";


function startElement($parser, $tagName, $attrs) {
if ($this->insideitem) {
$this->tag = $tagName;
} elseif ($tagName == "ITEM") {
$this->insideitem = true;
}
}

function endElement($parser, $tagName) {
if ($tagName == "ITEM") {

printf("<dt><b><a href='%s'>%s</a></b></dt>",
trim($this->link),htmlspecialchars(trim($this->title)));

printf("<dd>%s</dd>",htmlspecialchars(trim($this->description)));
$this->title = "";
$this->description = "";
$this->link = "";
$this->insideitem = false;

}
}

function characterData($parser, $data) {
if ($this->insideitem) {
switch ($this->tag) {
case "TITLE":
$this->title .= $data;
break;
case "DESCRIPTION":
$this->description .= $data;
break;
//edit the link here
case "LINK":
{
//$this->link .= $data
$id = $this->newLink($data);
$this->link .= "display.php?aid="."$id&";
break;
}
}
}
}


function newLink($newslink)
{
$file_name = basename($newslink, ".php");
$id = ereg_replace("[^0-9]", "", $file_name);

return $id;
}


}

$xml_parser = xml_parser_create();
$rss_parser = new RSSParser();
xml_set_object($xml_parser,&$rss_parser);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("http://www.prweb.com/rss2/computers.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);

?>
</dl>
</body>
</html>

=================================================

========= Code for display.php =========
<?php

echo "<h1>Display news </h1>";

$id = $_GET["aid"];

$baseLink = "http://www.prweb.com/printer.php?prid=";

include($baseLink.$id);

?>
========================================

Thank you in advance .

John Lam


  #2  
Old July 17th, 2005, 01:30 PM
Janwillem Borleffs
Guest
 
Posts: n/a
Default Re: News feed webpage ?

John wrote:[color=blue]
> OK, I am currently writing a webpage in PHP that will get RSS
> file(XML file) (news feed) and parse it using PHP and display
> articles' link and description. I want everytime a user click on one
> article's link, it take the user to another page which was written by
> me (for example, "display.php?aid=43232") (I don't want user to leave
> my website) and pass the article ID to the URL string. Everything run
> fine at this point but I got duplicate filename on the URL
> ("display.php?aid=43232&display.php="). I don't know how to fix this.
>[/color]

The feed is indented, which creates so called text nodes between the
elements.

To skip these nodes on a non-Windows environment, you can apply:

xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);

When you are working on a Windows box, you can do something like:

case "LINK" && trim($data): {
...
}


JW



  #3  
Old July 17th, 2005, 01:30 PM
Janwillem Borleffs
Guest
 
Posts: n/a
Default Re: News feed webpage ?

Janwillem Borleffs wrote:[color=blue]
> When you are working on a Windows box, you can do something like:
>
> case "LINK" && trim($data): {
> ...
> }
>[/color]

In this case, you should assign the value to the $this->link variable,
rather then concatenate it:

case "LINK" && trim($data): {
$id = $this->newLink($data);
$this->link = "display.php?aid=$id";
break;
}


JW



  #4  
Old July 17th, 2005, 01:30 PM
Marcin Dobrucki
Guest
 
Posts: n/a
Default Re: News feed webpage ?

John wrote:
[color=blue]
> OK, I am currently writing a webpage in PHP that will get RSS file(XML
> file) (news feed) and parse it using PHP and display articles' link and
> description.[/color]

Any reason not to use the existing code:
http://pear.php.net/manual/en/package.xml.xml-rss.php

/Marcin
  #5  
Old July 17th, 2005, 01:30 PM
John
Guest
 
Posts: n/a
Default Re: News feed webpage ?

Great help. It works. Thanks everyone !

John Lam

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.