Google Answers Logo
View Question
Q: How can I change a Javascript newsfeed code to a PHP newsfeed code? ( Answered,   0 Comments )
Subject: How can I change a Javascript newsfeed code to a PHP newsfeed code?
Category: Computers > Internet
Asked by: webjam-ga
List Price: $40.00
Posted: 20 Apr 2003 12:49 PDT
Expires: 20 May 2003 12:49 PDT
Question ID: 193020
I am using a JavaScript newsfeed code and want to use a PHP code
Please check a sample of the JavaScript code in this link
(View source for the code)

I am not using MySQL, so plain PHP is what I am after. I also would
like to be able to use ‘News Is Free News Feeds’ if I can, for their language options!

Please could suggest other PHP codes I could incorporate (without
MySQL) to the personal homepage I am developing. Please elaborate in
the explanation.

Request for Question Clarification by sycophant-ga on 20 Apr 2003 15:19 PDT
Hi Webjam, 

I have previous done exactly what you asked, with "News Is Free",
however their structure has changed since I used their service last.

What you are asking is very possible with with a RSS feed, such as the
ones they provide with their "Premium Export" service. However it may
not be possible with the basic service.

I am looking into their product offering more at the moment, however,
in the meantime, can you give me an idea of what you are looking to
achieve on your site? And if you are willing to signup to their
Premium service?

They also have a headline syndicator, which may be usable, I am
currently evaluating that.

Request for Question Clarification by sycophant-ga on 20 Apr 2003 15:27 PDT
Hi Webjam,

Seems I may have posted my clarification request a little too soon.
The News Is Free personal syndication service seems like it will be
just fine for your needs.

Although it is not very customisable - it only offers 5 headlines from
a selected newsfeed, and descriptions are not available. Further
customisation is offered by the Premium service I mentioned earlier.

I will go ahead and work with the basic service. If you choose to use
the premium service, the code I provide should be able to deal with
that too.

Subject: Re: How can I change a Javascript newsfeed code to a PHP newsfeed code?
Answered By: sycophant-ga on 20 Apr 2003 16:57 PDT

I have come up with the following code. It uses a RSS parsing class
(phpRSS) availble from here:

It does not require any unusual PHP libraries or modules and should
work pretty much as is.

My code assumes the phpRSS class ("class.RSS.php") is in a
subdirectory called "RSS". It also has a caching function which
expects a writable directory called "cache" under the directory from
which the file is executed. This can be disabled with a global
variable. Also, the directory and default cache times are easily

It can open remote RSS files (via HTTP) or local files.

It should be fairly self explainatory, but let me know if you run into



$cachetime = 600;
$cachedir = "cache/"; // needs trailing slash.

function webFetch($host,$path,$port=80) {
	$fp = fsockopen ($host,$port);
	if(!$fp) {
		die("Could not connect to host.");
	$header_done = false;

	$request = "GET ".$path." HTTP/1.0\r\n";
	$request .= "User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows
	$request .= "Host: ".$host."\r\n";
	$request .= "Connection: Close\r\n\r\n";
	$content = '';
	fputs ($fp, $request);
	$line = fgets ($fp, 128);
	$header["status"] = $line;
	while (!feof($fp)) {
		$line = fgets ( $fp, 128 );
		if($header_done) {
			$content .= $line;
		} else {
			if($line == "\r\n") {
			} else {
				$data = explode(": ",$line);
				$header[$data[0]] = $data[1];
	fclose ($fp);
	return array("header"=>$header,"content"=>$content);

function cacheExpired($feed) {
	global $cachedir,$cachetime;
	$cachefile = md5($feed);
	if ((is_readable($cachedir.$cachefile))&&(is_writable($cachedir.$cachefile)))
		$mtime = filemtime($cachedir.$cachefile);
		$now = time();
		print "<!-- Mtime: $mtime -->\n";
		print "<!-- Expiry: ".($mtime+$cachetime)." -- Time: $now -->\n";
		if (($mtime+$cachetime) < $now) {
			return TRUE;
		} else {
			return FALSE;
	} else {
		return TRUE;

function checkCache($feed) {
	global $cachedir,$cachetime;
	$cachefile = md5($feed);
	if (cacheExpired($feed)) {
		return ($feed);
	} else {
		return $cachedir.$cachefile;

function refreshCache($feed,$data) {
	global $cachedir,$cachetime;
	$cachefile = md5($feed);
	if (cacheExpired($feed)) {
		$fp = @fopen($cachedir.$cachefile,"w");
		if (!$fp) {
			return FALSE; // Open failed. Cannot write cache.
		} else {
			return TRUE;
	return TRUE;

function getFeed($feed) {
	global $caching;
	if ($caching) {
		$get = checkCache($feed);
	} else {
		$get = $feed;
	print "<!-- Getting: $get -->\n";
	if (eregi("^http://([^/]+)(.+)",$get,$reg)) {
		if (ereg(":",$reg[1])) {
			list($host,$port) = split(":",$reg[1]);
		} else {
			$host = $reg[1];
			$port = 80;
		$path = $reg[2];
		$out = webFetch($host,$path,$port);
		$feeddata = $out["content"];
	} else {
		$ftemp = file($feed);
		$feeddata = implode($ftemp,"");
	if ($caching) {
		if(!refreshCache($feed,$feeddata)) {
			//There was an error!
			print "\n\nCaching Error\n\n";
	return $feeddata;

function processFeed($data) {
	$rss = new RSS($data);
	$allItems = $rss->getAllItems();
	$itemCount = count($allItems);
		print "<a href=\"".$allItems[$y]['LINK']."\">" .
$caching = true;

$data = getFeed("");


Some lines will have been wrapped when I pasted the code, but this
should not cause any problems.

The feed I am using in the example is the BBC News feed available to
me as a personal user of News Is Free.


Request for Answer Clarification by webjam-ga on 20 Apr 2003 20:58 PDT
Good day sycophant,

Thank you for your contribution,
I tried to use the code you wrote, and created a file: feed_php.htm,
and posted it on:
It is giving ‘Caching Error’
I created an RSS directory and copied the extracted PHP-RSS files in
1) What am I missing? I just copied the PHP code and pasted it
2) When I click on the newslinks, it come with ‘The page cannot be
3) Still do not know how I could customize it to get the news
categories or the news source languages.
4) Could I use it to get the news from Moreover? In another section of
the page.

Therefore, I am still looking for the answer.
Thank you for you help.

Clarification of Answer by sycophant-ga on 21 Apr 2003 04:31 PDT
Hi Webjam, 

The Caching Error, is more for my debugging than anything - it will
happen if there is no writable directory called "cache" -- it does not
affect the operation, it may just slow the loading of the page

You can remove it by finding the line that says: "//There was an
error!" and delete the print line below it.

I am not sure about the link problem you are talking about? I checked
your example page, and it worked fine.

To customize it, you just need to change the link that is provided to
getFeed(). You can get the RSS links from NewsIsFree, by browsing the
feed categories and getting the unique RSS URL for the ones you want
to use. For information about that, see here: -- Basically, go to the feed
page, click on the blue RSS link and then generate a URL for RSS 0.92
with no descriptions (all that's available to free users).

I know that Moreover used to offer RSS/RDF newsfeeds, however I have
not been able to find them on the current site, so I do not know if
they are still available.

Basically, it should work with any RSS 0.92 compliant newsfeed, try
the following third parth ones for exmaple:

You could include the functions in a page, and then use getFeed() to
call different feeds in different parts of the page.

Hope this helps.

There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  

Google Home - Answers FAQ - Terms of Service - Privacy Policy