By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,620 Members | 1,303 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,620 IT Pros & Developers. It's quick & easy.

open in new window from RSS news feed help

P: n/a
Cam
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.php');

$phpxml = new phpnet_news("http://au.biz.yahoo.com/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($url, $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($file, &$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($host, $path, &$data, $ignore_cache)
{
if (!$this->is_cache($path, $ignore_cache)) {
$fp = @fsockopen($host, 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
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Cam wrote:
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.php');

$phpxml = new phpnet_news("http://au.biz.yahoo.com/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($url, $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($file, &$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($host, $path, &$data, $ignore_cache)
{
if (!$this->is_cache($path, $ignore_cache)) {
$fp = @fsockopen($host, 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

You need to use the client browser to open the new (RSS) page in a new
window, look at the docs for the onclick event
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.