Solved

Very simple PHP script to display RSS feeds: Part II

Posted on 2008-11-03
15
549 Views
Last Modified: 2013-11-18
Another poster asked about having a simple PHP script that would display RSS feeds on a page. You can find that EE thread here:

http://tinyurl.com/64uepk

That script worked perfectly, except for two additional things I need:

1. Standard URLs worked fine but when I used a URL that had attributes (i.e. http://somesite.com/page?attributes=xyz), the feed just renders blank. No errors just blank. Anyone know how to modify that original code to work with those types of URLs?

2. I am placing this script on a page that is behind browser-based challenge response password, pulling from an RSS URL behind that same password protection. (i.e. Home page where the script is is pulling from www.mysite.com/forum/rss). How can I make this script work with that constraint?

Thanks!
Jake
0
Comment
Question by:sink21
  • 8
  • 6
15 Comments
 
LVL 11

Expert Comment

by:NoiS
Comment Utility
If you have PHP >= 5


	// Is source online?

	$url = isset($_GET['url'])?$_GET['url']:'http://feeds.feedburner.com/Phpbrasil-IniciantesAoPhp';	

	list($status) = get_headers($url);

	if(strpos($status,'200')>0){ // Yes, its online

		// loading contents				

//		$xmlobj = simplexml_load_file($url); 

		//	or

		$string = file_get_contents($url);

		$xmlobj = simplexml_load_string($string);

		 

		if ( $xmlobj!=FALSE ){ // Loading sucessfull

			$count=0;

			echo "<pre>";			

			foreach($xmlobj->channel->item as $value){ // 				

				print_r($value);

				if($count++>=10) break; // Limit 10				

			}

			echo "</pre>";

		}

	}else{

		// the url is off-line

		; 

	}

Open in new window

0
 

Author Comment

by:sink21
Comment Utility
Sorry, NoiS.... I think I need some more background .... is this an update to the related post I linked to, or an all new solution?

Oh, I should mention too that I need to do two or more of these PHP scripts on one page. That's why the related link question was such a good solution!

Thanks!
Jake
0
 
LVL 11

Expert Comment

by:NoiS
Comment Utility
The code I posted replace the file attached. Its very, very simple and works fine.

I guess that your script is a too old code but I made a little change on it.

See the attached file


rss-feed-reader.txt
0
 

Author Comment

by:sink21
Comment Utility
Hmmm... I just replaced my rss-feed-reader.php file with the one you uploaded, but it's still giving me the same trouble. No errors, just not displaying the attribute filled URL. Here's the format of the URL that's borking:

http://www.mysite.com/forum/search.php?PostBackAction=Search&Type=Topics&Page=1&Feed=ATOM&FeedTitle=All+Discussions+Feed&IgnoreDefaultPage=1

(fake domain name, obviously!)

If I replace the URL above with another short, non-attribute-in-the-URL link it works fine.

The code I have in the actual index.php file is below.
<?php

include('includes/rss-feed-reader.php'); ?>
 
 

<?php 

$feed = 'http://feeds.feedburner.com/CommunityGuy';

$max_items = 5;

$rss = new mod_rss();

$rss->parser($feed, $max_items, true);
 

?>
 

<hr />
 

<?php 

$feed = 'http://www.mysite.com/forum/search.php?PostBackAction=Search&Type=Topics&Page=1&Feed=ATOM&FeedTitle=All+Discussions+Feed&IgnoreDefaultPage=1';

$max_items = 5;

$rss = new mod_rss();

$rss->parser($feed, $max_items, true);
 

?>

Open in new window

0
 
LVL 11

Expert Comment

by:NoiS
Comment Utility
but $feed is a feed target (a xml file)?  

$feed MUST contain url thar resolve to a XML file, not HTML file.

Whe you open the URL on your browser what do you see? XML or HTML?
The url doesn't redirect to another URL?
0
 

Author Comment

by:sink21
Comment Utility
yep - sure is. I've verified that it's showing as an RSS feed properly in FF, and no, no redirection.
0
 
LVL 11

Expert Comment

by:NoiS
Comment Utility
I did notice that in your URL have an attribute feed=ATOM.
Atom is not RSS, however atom is a XML too.

I made some tests with these urls

http://news.google.com.br/news?hl=pt-BR&client=firefox-a&rls=org.mozilla:pt-BR:official&hs=Tc4&q=obama&um=1&ie=UTF-8&tab=wn&nolr=1&output=rss

AND

http://news.google.com.br/news?hl=pt-BR&client=firefox-a&rls=org.mozilla:pt-BR:official&hs=Tc4&q=obama&um=1&ie=UTF-8&tab=wn&nolr=1&output=atom

Se second url brings the feed on Atom format that is different.

The script only works with rss, not atom.

I'll do some changes on the script to work with both.



0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:sink21
Comment Utility
Let me switch back to RSS (I'd switched to ATOM earlier just to see if the RSS feed had issues)

OK, changed it back to the RSS feed, and same issue. no breaking, just no appearance of content.

Actually, I wonder if there's a problem with the RSS feed.... I just tried your RSS link from google and it worked fine. Dang.

But yes, if you could modify to include ATOM that would be WONDERFUL!! Thanks!

Jake
0
 

Author Comment

by:sink21
Comment Utility
But strangely, I just used FeedValidator.org to verify the validity (obviously) and it checked out fine... hmm...
0
 

Author Comment

by:sink21
Comment Utility
One other question - I noticed that in my feed there are plus signs, but there are not in yours... does your code do any sort of "these characters are OK" checking that is missing the plus signs?
0
 

Author Comment

by:sink21
Comment Utility
<gack> so many follow-ups to myself.... sorry!

Here's the RSS code itself:
<?xml version="1.0" encoding="utf-8"?>

<rss version="2.0">

<channel>

<title>mysite Private Forum - </title>

<lastBuildDate>Mon, 03 Nov 2008 13:19:24 -0700</lastBuildDate>

<link>http://mysite.com/forum/</link>

<description></description>

<generator>

Lussumo Vanilla 1.1.5a &amp; Feed Publisher

</generator>

<item>

<title>test discussion</title>

<link>http://mysite.com/forum/comments.php?DiscussionID=1</link>

<guid isPermaLink="false">http://mysite.com/forum/comments.php?DiscussionID=1</guid>

<pubDate>Mon, 03 Nov 2008 09:10:25 -0700</pubDate>

<author>admin</author>

<description>

<![CDATA[ this is a test!<br /><br />yeah, really. <br /><br />a test. ]]>

</description>

</item>

</channel>

</rss>

Open in new window

0
 
LVL 11

Expert Comment

by:NoiS
Comment Utility
Here's my Code, need allow_url_fopen ON and PHP >= 5;

	/**

	 * Retrives Atom or Rss Feed and parse your contents 

	 *

	 * @param sring $feed The path to feed (need allow_url_fopen ON )

	 * @param int $max Max entries to be parsed

	 * @param bool $output Output the contents formatted (simple output)

	 */

	function parseRssOrAtom($url,$max=10,$output=false){	

	        // Is source online?

//        $url = isset($_GET['url'])?$_GET['url']:'http://feeds.feedburner.com/Phpbrasil-IniciantesAoPhp';        

        list($status) = get_headers($url);

        if(strpos($status,'200')>0){ // Yes, its online

                // loading contents                             

//              $xmlobj = simplexml_load_file($url); 

                //      or

                $string = file_get_contents($url);

                $xmlobj = simplexml_load_string($string);
 

                if ( $xmlobj!==FALSE ){ // Loading sucessfull

                        $count=0;

                        if(is_object($xmlobj->channel->item)){                                         

	                        foreach($xmlobj->channel->item as $value){ //                           

	                            $content[$count]["title"] = $value->title ;                             

	                            $content[$count]["link"] = $value->link ;

	                            $content[$count]["date"] = $value->pubDate;

	                            $content[$count]["description"] = $value->description;

	                            if($count++>=$max){

	                            	break; // Find the limit

	                            }

	                        }

                        }elseif( is_object($xmlobj->entry)){

                        	foreach($xmlobj->entry as $k=>$value){ //                           

	                            $content[$count]["title"] = $value->title ;                             

	                            $content[$count]["link"] = $value->link["href"] ;

	                            $content[$count]["date"] = $value->modified;

	                            $content[$count]["description"] = $value->content;

								if($count++>=$max){

	                            	break; // Find the limit

	                            }

	                            

	                        }

                        }else{

                        	trigger_error("Cannot find any channels or entries. The feed is valid!?",E_USER_WARNING);

                        	return NULL;

                        }

                        if($output==true){

							foreach($content as $k=>$v){

	                        	#-> title

								printf("<dt><b><a href='%s'>%s</a></b></dt>\n",

									trim($v["link"]),

									htmlspecialchars(trim($v["title"])));

								#-> description

								printf("<dd>%s</dd>\n",

									htmlspecialchars(trim($v["description"])));

								#-> date

								printf("<dd>%s</dd>\n",

									htmlspecialchars(trim($v["date"])));                        	

							}

                        }

                        

					return $content;                     

                }else{

                	trigger_error("Cannot load the feed!",E_USER_WARNING);

                	return null;

                }

                

        }else{

            trigger_error("Cannot read the feed!",E_USER_WARNING);

        	// the url is off-line

			return NULL; 

        }	

	}

//	$feed = 'http://www.domain.com/feed.xml';
 

	$max_items = 5;
 

	parseRssOrAtom($feed, $max_items, true);	

//	or

	print_r(parseRssOrAtom($url, $max_items));	

Open in new window

0
 

Author Comment

by:sink21
Comment Utility
Sorry, not sure what allow_url_fopen is or how to turn it to ON.

I'm also not sure if this code is meant to also replace the rss-feed-reader.php or not.

Help, please!


0
 
LVL 11

Accepted Solution

by:
NoiS earned 500 total points
Comment Utility
If the old code works the allow_url_fopen is ON.
If the PHP is greater than 5.0 the code will work fine.

Just put the function on the file and include it.
Only the caller must be changed.
All other code will be the same.
See lines 75~79.

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now