How can I change a Javascript newsfeed code to a PHP newsfeed code?
-
I am using a JavaScript newsfeed code and want to use a PHP code instead. Please check a sample of the JavaScript code in this link http://www.incross.ca/feed_js.htm (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 http://www.newsisfree.com/ 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.
-
Answer:
Okay, I have come up with the following code. It uses a RSS parsing class (phpRSS) availble from here: http://www.thewebmasters.net/ 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 modified. 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 problems. The CODE: ---- <?php $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 98)\r\n"; $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") { $header_done=true; } 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 { fwrite($fp,$data); fclose($fp); 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) { include("RSS/class.RSS.php"); $rss = new RSS($data); $allItems = $rss->getAllItems(); $itemCount = count($allItems); for($y=0;$y<$itemCount;$y++) { print "<a href=\"".$allItems[$y]['LINK']."\">" . $allItems[$y]['TITLE']."</a><br>\n"; } } $caching = true; $data = getFeed("http://xml.newsisfree.com/a/a3/a3b0130fb5d59fa647121895617891e4.xml"); processFeed($data); ?> ---- 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. Regards, sycophant-ga
webjam-ga at Google Answers Visit the source
Related Q & A:
- How can I change a value in an array?Best solution by Stack Overflow
- How can I change a member's name?Best solution by Yahoo! Answers
- How can I get a job at Universal in a show?Best solution by universalstudioshollywood.com
- How can I change a password with windows 7?Best solution by Yahoo! Answers
- How can I change a mobile number in Yahoo Messenger?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.