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?ai d=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?a id=43232&displa y.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.c om/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($p arser, $tagName, $attrs) {
if ($this->insideitem) {
$this->tag = $tagName;
} elseif ($tagName == "ITEM") {
$this->insideitem = true;
}
}
function endElement($par ser, $tagName) {
if ($tagName == "ITEM") {
printf("<dt><b> <a href='%s'>%s</a></b></dt>",
trim($this->link),htmlspec ialchars(trim($ this->title)));
printf("<dd>%s</dd>",htmlspecia lchars(trim($th is->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 "DESCRIPTIO N":
$this->description .= $data;
break;
//edit the link here
case "LINK":
{
//$this->link .= $data
$id = $this->newLink($data) ;
$this->link .= "display.php?ai d="."$id&";
break;
}
}
}
}
function newLink($newsli nk)
{
$file_name = basename($newsl ink, ".php");
$id = ereg_replace("[^0-9]", "", $file_name);
return $id;
}
}
$xml_parser = xml_parser_crea te();
$rss_parser = new RSSParser();
xml_set_object( $xml_parser,&$r ss_parser);
xml_set_element _handler($xml_p arser, "startEleme nt", "endElement ");
xml_set_charact er_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("XM L error: %s at line %d",
xml_error_strin g(xml_get_error _code($xml_pars er)),
xml_get_current _line_number($x ml_parser)));
fclose($fp);
xml_parser_free ($xml_parser);
?>
</dl>
</body>
</html>
=============== =============== =============== ====
========= Code for display.php =========
<?php
echo "<h1>Displa y news </h1>";
$id = $_GET["aid"];
$baseLink = "http://www.prweb.com/printer.php?pri d=";
include($baseLi nk.$id);
?>
=============== =============== ==========
Thank you in advance .
John Lam
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?ai d=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?a id=43232&displa y.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.c om/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($p arser, $tagName, $attrs) {
if ($this->insideitem) {
$this->tag = $tagName;
} elseif ($tagName == "ITEM") {
$this->insideitem = true;
}
}
function endElement($par ser, $tagName) {
if ($tagName == "ITEM") {
printf("<dt><b> <a href='%s'>%s</a></b></dt>",
trim($this->link),htmlspec ialchars(trim($ this->title)));
printf("<dd>%s</dd>",htmlspecia lchars(trim($th is->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 "DESCRIPTIO N":
$this->description .= $data;
break;
//edit the link here
case "LINK":
{
//$this->link .= $data
$id = $this->newLink($data) ;
$this->link .= "display.php?ai d="."$id&";
break;
}
}
}
}
function newLink($newsli nk)
{
$file_name = basename($newsl ink, ".php");
$id = ereg_replace("[^0-9]", "", $file_name);
return $id;
}
}
$xml_parser = xml_parser_crea te();
$rss_parser = new RSSParser();
xml_set_object( $xml_parser,&$r ss_parser);
xml_set_element _handler($xml_p arser, "startEleme nt", "endElement ");
xml_set_charact er_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("XM L error: %s at line %d",
xml_error_strin g(xml_get_error _code($xml_pars er)),
xml_get_current _line_number($x ml_parser)));
fclose($fp);
xml_parser_free ($xml_parser);
?>
</dl>
</body>
</html>
=============== =============== =============== ====
========= Code for display.php =========
<?php
echo "<h1>Displa y news </h1>";
$id = $_GET["aid"];
$baseLink = "http://www.prweb.com/printer.php?pri d=";
include($baseLi nk.$id);
?>
=============== =============== ==========
Thank you in advance .
John Lam
Comment