|
|
Want to know more about Packt's Article Network? Interested in contributing your article ideas? Please visit our FAQ for more information. See More BROWSE
All Titles WordPress Web Services SOA BPEL Web Graphics & Video Web Development RAW Portugues, Espanol, Italiano, French PHP/MySQL Oracle Open Source Networking & Telephony Moodle Microsoft & .NET Linux Servers jQuery Joomla! JBoss Java e-Learning e-Commerce Dynamics Drupal CRM Cookbook Content Management Beginner Guides Architecture and Analysis AJAX Future Titles Recently Published Titles This is a 5 part mini series by Roshan Bhattarai, covering basics of Widget, development of Wiki seek Widget, Pop-up Image Widget, RSS Web Widget, and Delicious Tagometer Widget. The web is becoming more flexible and dynamic from day to day. The service and functionality provided by a particular website is not limited to itself. We can extend it to other websites by placing a few lines of code in their web pages called Widget. In this article we will explore the technologies that go behind making a Widget and understand its working. See More |
RSS Web Widget
What is an RSS Feed?First of all, let us understand what a web feed is. Basically, it is a data format that provides frequently updated content to users. Content distributors syndicate the web feed, allowing users to subscribe, by using feed aggregator. RSS feeds contain data in an XML format. RSS is the term used for describing Really Simple Syndication, RDF Site Summary, or Rich Site Summary, depending upon the different versions. RDF (Resource Description Framework), a family of W3C specification, is a data model format for modelling various information such as title, author, modified date, content etc through variety of syntax format. RDF is basically designed to be read by computers for exchanging information. Since, RSS is an XML format for data representation, different authorities defined different formats of RSS across different versions like 0.90, 0.91, 0.92, 0.93, 0.94, 1.0 and 2.0. The following table shows when and by whom were the different RSS versions proposed.
Meanwhile, Harvard Law school is responsible for the further development of the RSS specification. There had been a competition like scenario for developing the different versions of RSS between UserLand, Netscape and O'Reilly before the official RSS 2.0 specification was released. For a detailed history of these different versions of RSS you can check http://www.rss-specifications.com/history-rss.htm The current version RSS is 2.0 and it is the common format for publishing RSS feeds these days. Like RSS, there is another format that uses the XML language for publishing web feeds. It is known as ATOM feed, and is most commonly used in Wiki and blogging software. Please refer to http://en.wikipedia.org/wiki/ATOM for detail. The following is the RSS icon that denotes links with RSS feeds.
If you're using Mozilla's Firefox web browser then you're likely to see the above image in the address bar of the browser for subscribing to an RSS feed link available in any given page. Web browsers like Firefox and Safari discover available RSS feeds in web pages by looking at the Internet media type application/rss+xml. The following tag specifies that this web page is linked with the RSS feed URL: http://www.example.com/rss.xml Example of RSS 2.0 formatFirst of all, let’s look at a simple example of the RSS format. <?xml version="1.0" encoding="UTF-8" ?> The first line is the XML declaration that indicates its version is 1.0. The character encoding is UTF-8. UTF-8 characters support many European and Asian characters so it is widely used as character encoding in web. The next line is the rss declaration, which declares that it is a RSS document of version 2.0 The next line contains the <channel> element which is used for describing the detail of the RSS feed. The <channel> element must have three required elements <title>, <link> and <description>. The title tag contains the title of that particular feed. Similarly, the link element contains the hyperlink of the channel and the description tag describes or carries the main information of the channel. This tag usually contains the information in detail. Furthermore, each <channel> element may have one or more <item> elements which contain the story of the feed. Each <item> element must have the three elements <title>, <link> and <description> whose use is similar to those of channel elements, but they describe the details of each individual items. Finally, the last two lines are the closing tags for the <channel> and <rss> elements. Creating RSS Web WidgetThe RSS widget we're going to build is a simple one which displays the headlines from the RSS feed, along with the title of the RSS feed. This is another widget which uses some JavaScript, PHP CSS and HTML. The content of the widget is displayed within an Iframe so when you set up the widget, you've to adjust the height and width.
To parse the RSS feed in XML format, I've used the popular PHP RSS Parser – Magpie RSS. The homepage of Magpie RSS is located at http://magpierss.sourceforge.net/. Introduction to Magpie RSSBefore writing the code, let's understand what the benefits of using the Magpie framework are, and how it works.
Now, let's quickly understand how Magpie RSS is used to parse the RSS feed. I'm going to pick the example from their homepage for demonstration. require_once 'rss_fetch.inc'; If you're more interested in trying other PHP RSS parsers then you might like to check out SimplePie RSS Parser (http://simplepie.org/) and LastRSS (http://lastrss.oslab.net/). You can see in the first line how the rss_fetch.inc file is included in the working file. After that, the URL of the RSS feed from getacoder.com is assigned to the $url variable. The fetch_rss() function of Magpie is used for fetching data and converting this data into RSS Objects. In the next line, the title of RSS feed is displayed using the code $rss->channel['title']. The other lines are used for displaying each of the RSS feed's items. Each feed item is stored within an $rss->items array, and the foreach() loop is used to loop through each element of the array. Writing Code for our RSS WidgetAs I've already discussed, this widget is going to use Iframe for displaying the content of the widget, so let's look at the JavaScript code for embedding Iframe within the HTML code. var widget_string = '<iframe In the above code, the widget string variable contains the string for displaying the widget. The source of Iframe is assigned to rss_parse_handler.php. The URL of the RSS feed, and the headlines from the feed are passed to rss_parse_handler.php via the GET method, using rss_url and maxlinks parameters respectively. The values to these parameters are assigned from the Javascript variables rss_widget_url and rss_widget_max_links. The width and height of the Iframe are also assigned from JavaScript variables, namely rss_widget_width and rss_widget_height. The red border on the widget is displayed by assigning 1px solid #FF0000 to the border attribute using the inline styling of CSS. Since, Inline CSS is used, the frameborder property is set to 0 (i.e. the border of the frame is zero). Displaying borders from the CSS has some benefit over employing the frameborder property. While using CSS code, 1px dashed #FF0000 (border-width border-style border-color) means you can display a dashed border (you can't using frameborder), and you can use the border-right, border-left, border-top, border-bottom attributes of CSS to display borders at specified positions of the object. The scrolling property is set to no here, which means that the scroll bar will not be displayed in the widget if the widget content overflows. If you want to show a scroll bar, then you can set this property to yes. The values of JavaScript variables like rss_widget_url, rss_widget_max_links etc come from the page where we'll be using this widget. You'll see how the values of these variables will be assigned from the section at the end where we'll look at how to use this RSS widget. Object-Oriented JavaScript
PHP CodeWe have looked at the JavaScript code, now let's look at the code of rss_parse_handler.php. First of all, a style sheet called rss_widget.css, is attached to this page. <link rel="stylesheet" type="text/css" href="rss_widget.css" /> Now, we define a user defined error handler function, to handle any error warning that may occur while executing the PHP script. function custom_error_handler($errno, $errstr) The set_error_handler() function allows you to define custom means of handling errors at runtime which allow you to display a warning message, in a stylish way, at runtime. You might know that the error_reporting() function of PHP can be used to set different levels for displaying errors. We can completely turn it off by using the error_reporting(0); statement. But the error_reporting() settings will have no effect on your custom error handler function made from set_error_handler() and it will be called regardless of error_reporting(). You might have noticed that the error handler function custom_error_handler() contains two parameters, so let's look at them here:
Let's look at the remaining code of rss_parse_handler.php $widget_string=''; The $widget_string contains the html string for displaying the title and headlines. It is initialized as an empty string at the beginning. The $max_feed_links variable contains the value passed, via the parameter maxlinks, and the value is converted into an integer, (which was actually the string when passed via the URL, using the intval() function of PHP). The $feed_url variable contains the value of the parameter rss_url and is passed via the GET method in a URL decoded format using the urldecode() function of PHP. This is encoded using the encodeURIComponent() function of JavaScript. Now, look at the remaining PHP code for parsing and displaying the items of RSS feed. require_once 'rss_fetch.inc'; Most of the coding of this part is already discussed above while describing Magpie RSS in detail. The @ symbol in front of fetch_rss() function is known as the error control operator in PHP. When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored. It should be noticed in the above code that the whole content of the widget is contained within the division rss_widget_box. In turn, the title of the feed is inside the division with the class rss_title, and the links to the RSS headlines are within the division with the class rss_links. Finally, the content of the $widget_string is printed in the browser using an echo statement. Style definition in CSS fileLet's now look at the style defined in CSS file. body The first style is defined for the body tag. The property assigned to this tag will be assigned to the whole document. Subsequently, the class is defined with the name phperror, you might have noticed that this class is used within the span (inside the custom error handler function custom_error_handler() of PHP). # is the id selector for CSS and used here for styling the division with id rss_widget_box. The difference between ".rss_links" and ".rss_links a" is that the styling of the rss_links class will be applied to the element which uses the class rss_links. While ".rss_links a" styling will be applied to all the anchor element(s)(<a></a> tag) inside the elements using the class rss_links. Putting the code all togetherSince we've dissected the code in sections, let's compile them into single files. Create a rss_widgert.js file and add the following code to it. var widget_string = '<iframe Now, create a rss_parse_handler.php file and add the following code. <html> After that, create a rss_widget.css file and add the following code to it. body Using RSS widgetTo use this RSS widget we need to follow these steps:
<script type="text/javascript" language="javascript"> Remember that the values of the variables rss_widget_url, rss_widget_max_links, rss_widget_width and rss_widget_height are customizable and you can change them according to your need. SummaryRSS feeds are XML data formats for providing real time website updates. These RSS feeds can be read in a single place by using an RSS reader or Aggregator. In this article, we've learnt how to create the widget which parses the RSS feed of a provided URL, and subsequently displays the title and headline with hyperlinks. AJAX and PHP: Building Responsive Web Applications
About the AuthorRoshan Bhattarai is an experienced web developer. His core proficiency lies in JAVASCRIPT, CSS, XHTML, PHP and MySQL. Having done his masters in information technology, he has many active websites to his credit. He has worked in various companies as a Technical Lead before moving on as a freelancer. He primarily resolves problems faced by programmers, liaise with the clients to have a better understanding of the projects' requirement and does research on new technologies relating to the project. He also excels in analysis and design of the database and work flow of a web project. He also excels in analysis and design of the database and work flow of a web project. He can be contacted on his blog http://roshanbh.com.np. Books from Packt
If you have read this article you may be interested to view :
|
TOP TITLES ![]()
|
| ||||||||