473,396 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Jul 17 '05 #1
4 1772
John wrote:
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.


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

Jul 17 '05 #2
Janwillem Borleffs wrote:
When you are working on a Windows box, you can do something like:

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


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

Jul 17 '05 #3
John wrote:
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.


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

/Marcin
Jul 17 '05 #4
Great help. It works. Thanks everyone !

John Lam

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Zhang Le | last post by:
Hello, I'm writing a little Tkinter application to retrieve news from various news websites such as http://news.bbc.co.uk/, and display them in a TK listbox. All I want are news title and url...
1
by: it hates me | last post by:
Hey gang, I hope you're all well and you're having an enjoyable week I'd love to be able to have a custom news feed on my web page through Javascript The only custom news feed I can find for...
0
by: Jon | last post by:
All, I'm extremely new to XML and have been given a project that at this time seems way over my head. Was hoping someone here could help. Basically, I need a page that functions much like...
5
by: sck10 | last post by:
Hello, I am using the following to get RSS Feeds. I've been able to get the xml files from every site except CNN. When I try to get there feed, I get the exception: A column named 'link'...
6
by: affiliateian | last post by:
Total newbie here for this so please be patient. We manually update our XML feed when we publish an article on our website. Can we add a javascript tracking pixel (from phpadsnew) into the XML...
4
by: Florian Lindner | last post by:
Hello, I'm looking for python RSS feed parser library. Feedparser http://feedparser.org/ does not seem to maintained anymore. What alternatives are recommendable? Thanks, Florian
1
by: Ellie | last post by:
I need to be able to get a feed and then have it displayed on my website. I'm not sure if I can get the feed, but if I can, what would be the best way to create a web page from the feed? Also, if I...
4
by: Blake Garner | last post by:
I'm looking for suggestions on how to approach generating rss feed ..xml files using python. What modules to people recommend I start with? Thanks! Blake
2
jamwil
by: jamwil | last post by:
What's up guys. I'm having some issues... I've created a method as part of my lifestreaming class which takes an rss feed, and puts the data into a database... It's fairly simple... Check...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.