Jump to content
Larry Ullman's Book Forums

Recommended Posts

I'm trying to display an rss feed on a webpage and could use some help getting the data. The format of the feed is

SimpleXMLElement Object
(
[title] => HBR.org
[id] => tag:blogs.harvardbusiness.org,2007-03-31:0.global-incremental
[link] => SimpleXMLElement Object
 (
	 [@attributes] => Array
		 (
			 [rel] => alternate
			 [type] => text/html
			 [href] => http://blogs.hbr.org/
		 )
 )
[updated] => 2012-10-22T11:10:17Z
[subtitle] => Practical insights, tools and resources from leading business thought leaders.
[logo] => http://cbimages.ed4.net/hbsp/9380_225659.gif
[entry] => Array
 (
	 [0] => SimpleXMLElement Object
		 (
			 [title] => The Sad Truth Behind Growing Clashes at the WTO
			 [id] => tag:blogs.harvardbusiness.org,2007-03-31:126.12422
			 [link] => SimpleXMLElement Object
				 (
					 [@attributes] => Array
						 (
							 [rel] => alternate
							 [type] => text/html
							 [href] => http://feeds.harvardbusiness.org/~r/harvardbusiness/~3/olpM1q4GSdM/the_sad_truth_behind_the_growi.html
						 )
				 )
			 [published] => 2012-10-22T11:00:41Z
			 [updated] => 2012-10-21T22:47:25Z
			 [summary] => Don't be fooled by the rhetoric about protectionism.

I want to get the title, href, published and summary data for the 3 most recent [entry]s. Code below is working for the most part but I dont know how to get to the href which is an attribute of link. I've tried a number of different syntaxes and foreach loops but unsuccessfully.

//get the feed and create a variable to enable parsing of the data which is returned in xml format
$feed = simplexml_load_file("http://feeds.harvardbusiness.org/harvardbusiness/");

echo '<ul id="rssFeed">';
$i=0;

//parse the feed to get the title, link, published date and summary for each entry
foreach ($feed->entry as $entry) {
$pubdate = date("jS M, Y H:i", strtotime($entry->published));
echo '<li a href="' . $href . '">' . $entry->title . '</a>
<p>' . $pubdate . '</p>
<p>' . $entry->summary . '</p>
</li>';

if ($i++ == 2) break;

} //end foreach loop

echo '</ul>'// end the ul tag

Please can you suggest how I access the href value?

 

Also I've used a counter and a break clause to stop the loop after 3 loops but I don't think this is the best practice. How would I get just the first 3 entries from the feed?

Link to comment
Share on other sites

Thanks for the reply. Now I have an array full of objects which I still don't know how to access. I'm sure there is a way to access the objects within the objects just not sure how.

 

Also is it possible to request just the 3 most recent 'entries' from the rssfeed as opposed to having to download the whole file?

Link to comment
Share on other sites

I would not convert it to an array, I would keep it as a SimpleXML object. As for the attributes, see the "Using attributes" section here: http://us3.php.net/manual/en/simplexml.examples-basic.php

 

The ability to fetch just the most recent 3 would be better, but that option would have to be set in the RSS feed itself, I believe. (Or be request-able by sending a parameter to the RSS page).

  • Upvote 1
Link to comment
Share on other sites

Thanks Larry for replying. I found the solution on stackflow but your link got me searching on the appropriate terms. Because [link] is being returned as an object, I had to convert it to a string.

$href = (string)$entry->link->attributes()->href;

Link to comment
Share on other sites

 Share

×
×
  • Create New...