When parsing RSS feed, wrong date is displayed - php

Here is my code:
<?php
$RSSFEEDS = array(
0 => "http://samnabi.posterous.com/rss.xml",
);
function FormatRow($date, $title, $link, $description) {
return <<<HTML
<p class="blogdate">$date</p><h2 class="blogtitle">$title</h2>
<div class="clearer"> </div>
$description
HTML;
}
ob_start();
if (!isset($feedid)) $feedid = 0;
$rss_url = $RSSFEEDS[$feedid];
$rss_feed = file_get_contents($rss_url);
$rss_feed = str_replace("<![CDATA[", "", $rss_feed);
$rss_feed = str_replace("]]>", "", $rss_feed);
$rss_feed = str_replace("\n", "", $rss_feed);
$rss_feed = preg_replace('#<image>(.*?)</image>#', '', $rss_feed, 1 );
preg_match_all('#<pubDate>(.*?)</pubDate>#', $rss_feed, $date, PREG_SET_ORDER);
preg_match_all('#<title>(.*?)</title>#', $rss_feed, $title, PREG_SET_ORDER);
preg_match_all('#<link>(.*?)</link>#', $rss_feed, $link, PREG_SET_ORDER);
preg_match_all('#<description>(.*?)</description>#', $rss_feed, $description, PREG_SET_ORDER);
if(count($title) <= 1) {
echo "No new blog posts. Check back soon!";
}
else {
for ($counter = 1; $counter <= 3; $counter++ ) {
if(!empty($title[$counter][1])) {
$title[$counter][1] = str_replace("&", "&", $title[$counter][1]);
$title[$counter][1] = str_replace("&apos;", "'", $title[$counter][1]);
$row = FormatRow($date[$counter][1],$title[$counter][1],$link[$counter][1],$description[$counter][1]);
echo $row;
}
}
}
ob_end_flush();
?>
When this script is run, the first item displays the second item's pubDate. The second item displays the third item's pubDate, and so on. So the dates that are shown are not the dates that you see in the original XML file. How do I fix this?
Bonus question: how do I strip characters off the beginning and end of the pubDate tag, so that I end up with "15 May 2010" instead of "Sat, 15 May 2010 03:28:00 -0700" ?

I've said it before, so I'll say it again: Use Magpie RSS to parse your RSS feeds. It takes care of all this stuff for you, and will be much more reliable.

Magpie RSS works great. Here's the code I used to replace what was in my original question:
<?php
define('MAGPIE_INPUT_ENCODING', 'UTF-8');
define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
//Tell it to use the fetch script to grab the RSS feed
require_once('magpie/rss_fetch.inc');
//Now it knows how to fetch RSS, tell it which one to fetch
$rss = fetch_rss('http://samnabi.posterous.com/rss.xml');
//In this case, we only want to display the first 3 items
$items = array_slice($rss->items,0,3);
//Now we tell Magpie how to format our output
foreach ($items as $item) {
$title = $item['title'];
$date = date('d M Y', strtotime($item['pubdate']));
$link = $item['link'];
$description = $item['description'];
//And now we want to put it all together.
echo "<p>$date</p><h2>$title</h2><p>$description</p>";
}
?>

Related

Convert a RSS feed from php to xml

The code below is a PHP generator that generate a simple web page in PHP with just one feed from my RSS URL. This URL has around 60 feeds.
I would like to generate a XML RSS with just one feed in random mode. Is this possible?
<?php
function load_xml_feed($feed){
global $RanVal;
$i= 1;
$FeedXml = simplexml_load_file($feed);
foreach ($FeedXml->channel->item as $topic) {
$title[$i] = (string)$topic->title;
$link[$i] = (string)$topic->link;
$description[$i] = (string)$topic->description;
$i++;
}
$randtopic = rand(2, $i);
$link = trim($link[$randtopic]);
$title = trim($title[$randtopic]);
$description = trim($description[$randtopic]);
$RanVal = array($title,$link,$description);
return $RanVal;
}
$rss = "http://syncds.boxip.com.br/feed?code=24C165DB04";
load_xml_feed($rss);
$link = $RanVal[1];
$title = $RanVal[0];
$description = $RanVal[2];
echo "<h1>".$title."</h1><h2>".$link."</h2><p>".$description."</p>";
?>
Thank you guys.

RSS feed limit items

How can i limit the items from my RSS-feed? (Example: 5 items). I hope someone can help me with this code.
<?php
$url = "LINK TO RSS";
$rss = simplexml_load_file($url);
if($rss)
{
$items = $rss->channel->item;
foreach($items as $item)
{
$title = $item->title;
$link = $item->link;
$published_on = $item->pubDate;
$description = $item->description;
echo '<p class="rss-feed">'.$title.'</p>';
echo '<p>'.$description.'</p>';
}
}
?>
If you want to limit the number of items displayed you can use the array_slice function to store a subset of the retrieved values in $items, e.g. change:
$items = $rss->channel->item;
to:
$items = array_slice($rss->channel->item, 0, 5);
If you want to change the number of items fetched from the URL then you need to check whether/how this URL supports this behaviour.

Changing XML source on click using PHP

I have a simple newsfeed which is pulling from an xml file which works great. The news feed is broken down into separate XML files for each year. I want the page to show only one year at a time with links to other years above. Ideally when a new year is clicked I would like the content below to update without going to a new page.
I am trying to avoid repeating the PHP code which is parsing the XML for each year and instead would like to simply update the $dom_object->load source when the user clicks a different year.
I am a PHP newbie so some help would be appreciated!
<?php
$dom_object = new DOMDocument();
$dom_object->load("http://EXAMPLE.com/XML-Feed-10100524539?year=2013");
$item = $dom_object->getElementsByTagName("item");
foreach( $item as $value )
{
$titles = $value->getElementsByTagName("title");
$title = $titles->item(0)->nodeValue;
$pubDates = $value->getElementsByTagName("pubDate");
$pubDate = $pubDates->item(0)->nodeValue;
$pdf_urls = $value->getElementsByTagName("pdf_url");
$pdf_url = $pdf_urls->item(0)->nodeValue;
//Trims after last space - REMOVES EST
$pubDate=substr($pubDate, 0, strrpos($pubDate, ' '));
//Trims after remaining space - REMOVES TIME
$pubDate=substr($pubDate, 0, strrpos($pubDate, ' '));
$pubDater = str_replace('/', '-', $pubDate);
$newDate = DateTime::createFromFormat("m/d/Y", $pubDate);
$newDate = $newDate->format('F d, Y');
echo "<div style=\"width:33%; float:left; display:inline-block; height:150px;\"><div style=\"padding:10px;\"><h4>$newDate</h4><p>$title</p></div></div>";
}
?>
You would need to use ajax by using $.get or $.post so you could do something like:
$('#yourElement').click(function(){
var path = $('#path'); // for example
$.post("your/path/getXML.php", { path: path },
function (dat) {
console.log(dat);
}
);
});
Where getXML would be a file similar to this one:
$dom_object = new DOMDocument();
$dom_object->load($_POST["path"]);
$item = $dom_object->getElementsByTagName("item");
foreach( $item as $value )
{
$titles = $value->getElementsByTagName("title");
$title = $titles->item(0)->nodeValue;
$pubDates = $value->getElementsByTagName("pubDate");
$pubDate = $pubDates->item(0)->nodeValue;
$pdf_urls = $value->getElementsByTagName("pdf_url");
$pdf_url = $pdf_urls->item(0)->nodeValue;
//Trims after last space - REMOVES EST
$pubDate=substr($pubDate, 0, strrpos($pubDate, ' '));
//Trims after remaining space - REMOVES TIME
$pubDate=substr($pubDate, 0, strrpos($pubDate, ' '));
$pubDater = str_replace('/', '-', $pubDate);
$newDate = DateTime::createFromFormat("m/d/Y", $pubDate);
$newDate = $newDate->format('F d, Y');
echo "<div style=\"width:33%; float:left; display:inline-block; height:150px;\"><div style=\"padding:10px;\"><h4>$newDate</h4><p>$title</p></div></div>";

Getting random post from rss feed

I have an rss feed, created by Yahoo Pipes and I need to get random post from it. How is it possible to realize this on php?
Read the feed using XML Parser and put it in an array. then, use array_rand to pick a random item from the array.
<?
function load_xml_feed($feed)
{
global $RanVal;
$i= 1;
$FeedXml = simplexml_load_file($feed);
foreach ($FeedXml->channel->item as $topic) {
$title[$i] = (string)$topic->title;
$link[$i] = (string)$topic->link;
$description[$i] = (string)$topic->description;
$i++;
}
$randtopic = rand(2, $i);
$link = trim($link[$randtopic]);
$title = trim($title[$randtopic]);
$description = trim($description[$randtopic]);
$RanVal = array($title,$link,$description);
return $RanVal;
}
$rss = "http://www.sabaharabi.com/rss/rss.xml";
load_xml_feed($rss);
$link = $RanVal[1];
$title = $RanVal[0];
$description = $RanVal[2];
echo "<h1>".$title."</h1><h2>".$link."</h2><p>".$description."</p>";

Removing wrapping HTML elements inside a RSS XML node

I have a fetch function that injects rss content into a page for me. This returns an xml which contains the usual RSS elements like title, link, description but the problem is the returned description is a table with two tds which one contains an image the other the text. I am not sure how I can remove the table, img and the tds and be left only with the text using php and not javascript.
Any help is much appreciated.
<?php
require_once('rss_fetch.inc');
$url = 'http://www.domain.com/rss.aspx?typeid=0&imagesize=120&topcount=20';
if ( $url ) {
$rss = fetch_rss( $url );
//echo "Channel: " . $rss->channel['title'] . "<p>";
echo "<ul>";
foreach ($rss->items as $item) {
$href = $item['link'];
$title = $item['title'];
$description = $item['description'];
$pubdate = date('F dS, Y', strtotime($item['pubdate']));
echo "<li><h3>$title<em>$pubdate</em></h3>$description <p><a href='$href' target='_blank'>ادامه مطلب</a></p><br/></li>";
}
echo "</ul>";
}
?>
strip_tags() will do the job..

Categories