This post explains how to create RSS feed for web projects with PHP. It's simple code just you have to include the basic RSS stucture in while loop.
Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts
Displaying RSS Feed with PHP
This article explains to displaying RSS(XML format) feed like popurls.com (popular urls in one place) using simplexml_load_file() a PHP function. It's very useful to display your blog feeds as like Recent articles(headlines) list.
RSS- Really Simple Syndication.
Reading XML data and presenting with HTML.
Download Script Live Demo
Index.php RSS display page:
File contains HTML tags and PHP included rssclass.php. You have to change the RSS feed URL.
<div> <?php include('rssclass.php'); $feedlist = new rss('http://feeds2.feedburner.com/9lesson'); echo $feedlist->display(9,"9lessons"); $feedlist = new rss('http://feeds.feedburner.com/nettuts'); echo $feedlist->display(9,"Nettuts"); $feedlist = new rss('http://feeds.labnol.org/labnol'); echo $feedlist->display(9,"Labnol"); ?> </div>
rssclass.php
A beautiful PHP function simplexml_load_file() to load and read XML file. simplexml_load_string() XML string reader.
<?php class rss { var $feed; function rss($feed) { $this->feed = $feed; } function parse() { $rss = simplexml_load_file($this->feed);
$rss_split = array(); foreach ($rss->channel->item as $item) {
$title = (string) $item->title; // Title $link = (string) $item->link; // Url Link $description = (string) $item->description; //Description $rss_split[] = '<div> <a href="'.$link.'" target="_blank" title="" > '.$title.' </a> <hr> </div> '; } return $rss_split; } function display($numrows,$head) { $rss_split = $this->parse(); $i = 0; $rss_data = '<div class="vas"> <div class="title-head"> '.$head.' </div> <div class="feeds-links">'; while ( $i < $numrows ) { $rss_data .= $rss_split[$i]; $i++; } $trim = str_replace('', '',$this->feed); $user = str_replace('&lang=en-us&format=rss_200','',$trim); $rss_data.='</div></div>'; return $rss_data; } } ?>
CSS code :
Style just view the Live Demo
.vas{ float:left; width:270px; padding:10px; } .title-head { font-size:18px; font-weight:bold; text-align:left; background-color:#006699; color:#FFFFFF; padding:5px;} .feeds-links { text-align:left; padding:5px; border:1px solid #dedede; }
Web.xml Deployment Descriptor
Web.xml mapping servlet names improve web app's flexibility and security..The deployment descriptor (DD), provides a "declarative" mechanism for customizing your web applications without touching source code!
Tomcat Directory Structure
Login.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Login extends HttpServlet{
public void doGet(HttpServletRequest request,
HttpServletResponse response)throws IOException{
PrintWriter out=response.getWriter();
java.util.Date tody=new java.util.Date();
out.println("out put html tags");
}
}
<servlet>Maps internal name to fully-qualified class name.
<servlet-mapping>Maps internal name to public URL name.
web.xml
<servlet-mapping> which servlet should i invoke for this requested URL?
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd" version="2.4">
<servlet>
<servlet-name>Servlet_login</servlet-name>
<servlet-class>Login</servlet-class>
</servlet>
<servlet>
<servlet-name>Servlet_inbox</servlet-name>
<servlet-class>inbox</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet_login</servlet-name>
<url-pattern>/signin.do</usr-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Servlet_inbox</servlet-name>
<url-pattern>/view.do</usr-pattern>
</servlet-mapping>
</web-app>
signin.do file mapping to Login.class. So the client or users to get to the servlet.. but it's a made-up name that is NOT the name of the actual servlet class.
Tomcat Directory Structure
Login.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Login extends HttpServlet{
public void doGet(HttpServletRequest request,
HttpServletResponse response)throws IOException{
PrintWriter out=response.getWriter();
java.util.Date tody=new java.util.Date();
out.println("out put html tags");
}
}
<servlet>Maps internal name to fully-qualified class name.
<servlet-mapping>Maps internal name to public URL name.
web.xml
<servlet-mapping> which servlet should i invoke for this requested URL?
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd" version="2.4">
<servlet>
<servlet-name>Servlet_login</servlet-name>
<servlet-class>Login</servlet-class>
</servlet>
<servlet>
<servlet-name>Servlet_inbox</servlet-name>
<servlet-class>inbox</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet_login</servlet-name>
<url-pattern>/signin.do</usr-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Servlet_inbox</servlet-name>
<url-pattern>/view.do</usr-pattern>
</servlet-mapping>
</web-app>
signin.do file mapping to Login.class. So the client or users to get to the servlet.. but it's a made-up name that is NOT the name of the actual servlet class.