Hi I'm using this simple rss parser that i would like to have open in
another window. But I'm very much a beginner in php scripting.
Here is the script on the page
<?php
require('./class.phpnet.ph p');
$phpxml = new phpnet_news("ht tp://au.biz.yahoo.co m/financenews/htt/financenews.xml ",
1);
$news = $phpxml->get_headlines( 8);
?><?php
for ($i = 0; $i < sizeof($news['title']); $i++) {
echo '<li><a href="' . $news['link'][$i] . '">' . $news['title'][$i].
'</a></li>';
echo "\n";
}
?>
------------------
And this is the main script
class phpnet_news {
var $titles, $descriptions, $links, $ccdate;
function phpnet_news($ur l, $ignore_cache)
{
$url = parse_url($url) ;
$data = '';
$this->get_rss_file($ url['host'], $url['path'], &$data,
$ignore_cache);
$this->parse_contents (&$data);
unset($data);
}
function is_cache($file, $ignore_cache)
{
if ($ignore_cache)
return 0;
$file = '.' . $file . '.cache';
$fp = fopen($file, 'a+');
fseek($fp, 0);
$rd = fread($fp, filesize($file) );
fclose($fp);
if (time() - $rd > 300)
return 0;
else
return 1;
}
function update_cache($f ile, &$data, $ignore_cache)
{
if ($ignore_cache)
return 0;
$fp = fopen('.' . $file, 'w');
fwrite($fp, $data);
fclose($fp);
$fp = fopen('.' . $file . '.cache', 'w');
fwrite($fp, time());
fclose($fp);
}
function get_rss_file($h ost, $path, &$data, $ignore_cache)
{
if (!$this->is_cache($path , $ignore_cache)) {
$fp = @fsockopen($hos t, 80, $errno, $errstr, 30);
if (!$fp) {
fclose($fp);
echo "$errstr ($errno)\n";
} else {
fwrite($fp, "GET $path HTTP/1.1\r\nHost: $host\r\n\r\n") ;
while (!feof($fp))
{
$data .= fread($fp, 4096);
}
fclose($fp);
$data = str_replace("\n ", "", $data);
$this->update_cache($ path, &$data, $ignore_cache);
}
} else {
$fp = fopen('.' . $path, 'r');
$data = fread($fp, filesize('.' . $path));
fclose($fp);
}
}
function parse_contents( &$data)
{
preg_match_all( "|<description> (.*)</description>|U" , $data,
$this->descriptions );
preg_match_all( "|<title>(. *)</title>|U", $data, $this->titles);
preg_match_all( "|<link>(.* )</link>|U", $data, $this->links);
preg_match_all( "|<cc:date>(.*) </cc:date>|U", $data, $this->ccdate);
}
function get_headlines($ num)
{
for ($i = 0; $i < ($num); $i++) {
$news['title'][$i] = &$this->titles[1][$i+1];
$news['link'][$i] = &$this->links[1][$i+1];
$news['description'][$i] = &$this->descriptions[1][$i+1];
$news['ccdate'][$i] = &$this->ccdate[1][$i];
}
return $news;
}
}
?>
-------------------------------------
Any help would be greatly appreciated
Thanks
another window. But I'm very much a beginner in php scripting.
Here is the script on the page
<?php
require('./class.phpnet.ph p');
$phpxml = new phpnet_news("ht tp://au.biz.yahoo.co m/financenews/htt/financenews.xml ",
1);
$news = $phpxml->get_headlines( 8);
?><?php
for ($i = 0; $i < sizeof($news['title']); $i++) {
echo '<li><a href="' . $news['link'][$i] . '">' . $news['title'][$i].
'</a></li>';
echo "\n";
}
?>
------------------
And this is the main script
class phpnet_news {
var $titles, $descriptions, $links, $ccdate;
function phpnet_news($ur l, $ignore_cache)
{
$url = parse_url($url) ;
$data = '';
$this->get_rss_file($ url['host'], $url['path'], &$data,
$ignore_cache);
$this->parse_contents (&$data);
unset($data);
}
function is_cache($file, $ignore_cache)
{
if ($ignore_cache)
return 0;
$file = '.' . $file . '.cache';
$fp = fopen($file, 'a+');
fseek($fp, 0);
$rd = fread($fp, filesize($file) );
fclose($fp);
if (time() - $rd > 300)
return 0;
else
return 1;
}
function update_cache($f ile, &$data, $ignore_cache)
{
if ($ignore_cache)
return 0;
$fp = fopen('.' . $file, 'w');
fwrite($fp, $data);
fclose($fp);
$fp = fopen('.' . $file . '.cache', 'w');
fwrite($fp, time());
fclose($fp);
}
function get_rss_file($h ost, $path, &$data, $ignore_cache)
{
if (!$this->is_cache($path , $ignore_cache)) {
$fp = @fsockopen($hos t, 80, $errno, $errstr, 30);
if (!$fp) {
fclose($fp);
echo "$errstr ($errno)\n";
} else {
fwrite($fp, "GET $path HTTP/1.1\r\nHost: $host\r\n\r\n") ;
while (!feof($fp))
{
$data .= fread($fp, 4096);
}
fclose($fp);
$data = str_replace("\n ", "", $data);
$this->update_cache($ path, &$data, $ignore_cache);
}
} else {
$fp = fopen('.' . $path, 'r');
$data = fread($fp, filesize('.' . $path));
fclose($fp);
}
}
function parse_contents( &$data)
{
preg_match_all( "|<description> (.*)</description>|U" , $data,
$this->descriptions );
preg_match_all( "|<title>(. *)</title>|U", $data, $this->titles);
preg_match_all( "|<link>(.* )</link>|U", $data, $this->links);
preg_match_all( "|<cc:date>(.*) </cc:date>|U", $data, $this->ccdate);
}
function get_headlines($ num)
{
for ($i = 0; $i < ($num); $i++) {
$news['title'][$i] = &$this->titles[1][$i+1];
$news['link'][$i] = &$this->links[1][$i+1];
$news['description'][$i] = &$this->descriptions[1][$i+1];
$news['ccdate'][$i] = &$this->ccdate[1][$i];
}
return $news;
}
}
?>
-------------------------------------
Any help would be greatly appreciated
Thanks
Comment