View Full Version : Rss
im not really sure where this would go, so sorry mods if this is in the wrong forum!
All i wanted to know is how i can put mtv's rss news feed on my html page ?
link to rss: http://www.mtv.com/rss/news/latestcached.jhtml?partner=rssMozilla
BonRouge
01-21-2007, 05:34 PM
http://bonrouge.com/demos/mtv_parser.php
<?php
$page="http://www.mtv.com/rss/news/latestcached.jhtml?partner=rssMozilla";
$tags=array('title','description','link','pubDate');
$xml=file_get_contents($page);
preg_match_all('/<item>.+<\/item>/sU',$xml, $items);
$items=$items[0];
$itemsArray=array();
foreach ($items as $item) {
for($i=0; $i<count($tags); $i++) {
preg_match("/<$tags[$i]>(.+)<\/$tags[$i]>/sU", $item, $tag);
$this[$i]=preg_replace("/<$tags[$i]>(.+)<\/$tags[$i]>/sU",'$1',$tag);
$this[$i]=preg_replace("/<!\[CDATA\[(.+)\]\]>/sU",'$1',$this[$i]);
}
array_push($itemsArray, $this);
}
$theData="<dl>";
foreach ($itemsArray as $item) {
for($i=0; $i<count($tags); $i++) {
$data[$i]=$item[$i][0];
}
$theData.="<dt><a href=\"$data[2]\">$data[0]</a></dt>
<dd class=\"story\">$data[1]</dd>
<dd>Date: $data[3]</dd>\r";
}
$theData.="</dl>";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>mtv parser</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
* {
margin:0;
padding:0;
}
body {
background-color:white;
}
dt {
font-weight:bold;
margin-top:10px;
}
dd {
margin:5px;
}
dl {
width:500px;
margin:auto;
}
.story {
padding:5px;
border-top:1px solid gray;
border-bottom:1px solid gray;
}
</style>
</head>
<body>
<?php echo $theData; ?>
</body>
</html>
cheers mate, your the best, for a different rss feed i would just change $page right?
BonRouge
01-22-2007, 04:21 AM
You need to change the page, but you also need to look at the names of the tags on the xml page itself - sometimes they're different.
crbpn6
02-14-2007, 05:59 AM
wow...this script seems to do what I've been trying to find for hours now...an easy way to display RSS feeds on a webpage...couple questions...
How can you fix the code to only display 3 or 4 or 5 of the feeds? Is there a way to make it display pictures from some of the feeds? If you wanted to show some stories from one feed and some stories from another feed, what would you have to change in order to add a second RSS link? I know that you could just make two different files with their own code, then make the page you want to display them and just includefile, right? I'm sure this isn't the best way to do it. Sorry I'm such a PHP newbie! :)
Here is the code I'm working with:
<?php
$page="http://rss.cnn.com/rss/cnn_topstories.rss";
$tags=array('title','description','link','pubDate');
$xml=file_get_contents($page);
preg_match_all('/<item>.+<\/item>/sU',$xml, $items);
$items=$items[0];
$itemsArray=array();
foreach ($items as $item) {
for($i=0; $i<count($tags); $i++) {
preg_match("/<$tags[$i]>(.+)<\/$tags[$i]>/sU", $item, $tag);
$this[$i]=preg_replace("/<$tags[$i]>(.+)<\/$tags[$i]>/sU",'$1',$tag);
$this[$i]=preg_replace("/<!\[CDATA\[(.+)\]\]>/sU",'$1',$this[$i]);
}
array_push($itemsArray, $this);
}
$theData="<dl>";
foreach ($itemsArray as $item) {
for($i=0; $i<count($tags); $i++) {
$data[$i]=$item[$i][0];
}
$theData.="<hr /><heading><a href=\"$data[2]\">$data[0]</a></heading>
<dd class=\"story\">$data[1]</dd>
<dd>Date: $data[3]</dd>\r";
}
$theData.="</dl>";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>mtv parser</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
* {
margin:0;
padding:0;
}
body {
background-color:white;
}
heading {
font-weight:bold;
margin-top:10px;
}
dd {
margin:5px;
font-color:#336699;
}
dl {
width:500px;
margin:auto;
}
.story {
padding:5px;
}
</style>
</head>
<body>
<?php echo $theData; ?>
</body>
</html>
BonRouge
02-14-2007, 09:59 AM
http://bonrouge.com/demos/xml_parser_2.php
<?php
function xml_parser($page,$tags,$number) {
if (!$number) {$number=100;}
$stories=0;
$xml=file_get_contents($page);
preg_match_all('/<item>.+<\/item>/sU',$xml, $items);
$items=$items[0];
$itemsArray=array();
foreach ($items as $item) {
for($i=0; $i<count($tags); $i++) {
preg_match("/<$tags[$i]>(.+)<\/$tags[$i]>/sU", $item, $tag);
$this[$i]=preg_replace("/<$tags[$i]>(.+)<\/$tags[$i]>/sU",'$1',$tag);
$this[$i]=preg_replace("/<!\[CDATA\[(.+)\]\]>/sU",'$1',$this[$i]);
}
if (count($itemsArray)<$number) {array_push($itemsArray, $this);}
}
$theData="<dl>";
foreach ($itemsArray as $item) {
for($i=0; $i<count($tags); $i++) {
$data[$i]=$item[$i][0];
}
$theData.="<dt><a href=\"$data[2]\">$data[0]</a></dt>
<dd class=\"story\">$data[1]</dd>
<dd>Date: $data[3]</dd>\r";
}
$theData.="</dl>";
return $theData;
}
$tags=array('title','description','link','pubDate');
$mtv=xml_parser("http://www.mtv.com/rss/news/latestcached.jhtml?partner=rssMozilla",$tags,5);
$bbc=xml_parser("http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml",$tags,10);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>feed parser</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
* {
margin:0;
padding:0;
}
body {
background-color:white;
}
h1 {
font-size:1.5em;
}
h2 {
font-size:1.2em;
}
h1, h2 {
text-align:center;
margin:1em;
}
dt {
font-weight:bold;
margin-top:10px;
}
dd {
margin:5px;
}
dl {
width:500px;
margin:auto;
}
.story {
padding:5px;
border-top:1px solid gray;
border-bottom:1px solid gray;
}
</style>
</head>
<body>
<h1>feeds</h1>
<h2>mtv</h2>
<?php echo $mtv; ?>
<h2>bbc</h2>
<?php echo $bbc; ?>
</body>
</html>
crbpn6
02-14-2007, 04:45 PM
wow, getting real close. Is there a reason that this wont work with
http://rss.cnn.com/rss/cnn_topstories.rss
? I've seen this feed used other places and work, I just don't know why it wont work with this code, shows a bunch of code instead of displaying the code. Should show an image as well. Thanks for your help.
BonRouge
02-14-2007, 07:06 PM
Well maybe this whole thing isn't as easy as I thought. I've got the code showing right (check the link again and look at the source), but the images that are in the source don't seem to exist...
crbpn6
02-14-2007, 08:07 PM
Link isn't working...
This Account Has Been Suspended
Please contact the billing/support department as soon as possible.
Check out http://www.semo.net/ They have the same rss file with images and text. Not sure how they're doing it? I'm thinking that the way it shows up, it might be because it can't display that code in a PHP file maybe? I wonder if there is another easy way to parse this w/out php? I couldnt find another way after looking all day yesterday..
BonRouge
02-15-2007, 04:41 AM
I've had problems with my site being hacked. That's why the link wasn't working.
Here's the code for the above link: http://bonrouge.com/demos/xml_parser_2.php <?php
function xml_parser($page,$container,$tags,$number) {
if (!$number) {$number=100;}
$stories=0;
$xml=file_get_contents($page);
preg_match_all("/<$container>.+<\/$container>/sU",$xml, $items);
$items=$items[0];
$itemsArray=array();
foreach ($items as $item) {
for($i=0; $i<count($tags); $i++) {
preg_match("/<$tags[$i](.+)(<\/$tags[$i]>|\/>)/sU", $item, $tag);
$this[$i]=preg_replace("/<$tags[$i]>(.+)(<\/$tags[$i]>|\/>)/sU",'$1',$tag);
$this[$i]=preg_replace("/<!\[CDATA\[(.+)\]\]>/sU",'$1',$this[$i]);
$this[$i]=array_map('html_entity_decode', $this[$i]);
}
if (count($itemsArray)<$number) {array_push($itemsArray, $this);}
}
$theData="<dl>";
foreach ($itemsArray as $item) {
for($i=0; $i<count($tags); $i++) {
$data[$i]=$item[$i][0];
}
$title=$data[0];
$description=preg_replace("/<img(.+)><\/img>/sU",'<img$1 >',$data[1]);
$description=preg_replace("/<img(.+)\/>/sU",'<img$1 />',$description);
$link=preg_replace("/<link.+href=\"(.+)\"(.+|)\/>/sU",'$1',$data[2]);
$date=$data[3];
$theData.="
<dt><a href=\"$link\">$title</a></dt>
<dd class=\"story\">$description</dd>
<dd>Date: $date</dd>\r";
}
$theData.="</dl>";
return $theData;
}
$container='item';
$tags=array('title','description','link','pubDate');
$mtv=xml_parser("http://www.mtv.com/rss/news/latestcached.jhtml?partner=rssMozilla",$container,$tags,5);
$bbc=xml_parser("http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml",$container,$tags,10);
$cnn=xml_parser("http://rss.cnn.com/rss/cnn_topstories.rss",$container,$tags,10);
$container='entry';
$tags=array('title','content','link','published');
$flickr=xml_parser("http://api.flickr.com/services/feeds/photos_public.gne",$container,$tags,10);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>feed parser</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
* {
margin:0;
padding:0;
}
body {
background-color:white;
}
h1 {
font-size:1.5em;
}
h2 {
font-size:1.2em;
}
h1, h2 {
text-align:center;
margin:1em;
}
dt {
font-weight:bold;
margin-top:10px;
}
dd {
margin:5px;
}
dl {
width:500px;
margin:auto;
}
.story {
padding:5px;
border-top:1px solid gray;
border-bottom:1px solid gray;
}
</style>
</head>
<body>
<h1>feeds</h1>
<h2>mtv</h2>
<?php echo $mtv; ?>
<h2>bbc</h2>
<?php echo $bbc; ?>
<h2>cnn</h2>
<?php echo $cnn; ?>
<h2>flickr</h2>
<?php echo $flickr; ?>
</body>
</html>
I looked at the images in the feed - they seem to be 1px x 1px images. Strange. As for that site - I don't know how they're doing that, but you can see from the url of the image that it's from that site rather than directly from CNN. http://www.semo.net/tmp/news_img.jpg. Maybe you could ask the site owner how it's done.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.