PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: Rss
Rss - phponwebsites.com
Showing posts with label Rss. Show all posts

20 Aug 2014

Get/Read value from RSS Feed using PHP

                                You can generate RSS Feed using PHP and MySQL. Then you've have a doubt. How can retrieve data from RSS Feed using PHP? In this post, You are going to know get data from RSS Feed using PHP.

                                 You can get value from RSS Feed using PHP in following two ways.

1. Get data from RSS Feed using simple_load_file() in PHP
2. Get data from RSS Feed using file_get_contents() and SimpleXMLElement() in PHP

Lets see below examples.

Get data from RSS Feed using simple_load_file() in PHP


                                   Simple_load_file() in PHP is used to convert XML document into an object. The below example explain retrieve data from RSS Feed.


<?php
$xml=simplexml_load_file('http://www.phponwebsites.com/rss.xml');
//print_R($xml);
foreach($xml->channel->item as $val)
 {
  echo $val->title.' '. $val->link.'<br>';
 }
?>


Get data from RSS Feed using file_get_contents and SimpleXMLElement in PHP


                                  File_get_contents() is used to read the contents of a file into string. SimpleXMLElement is used to represents elements of XML document. The below example explain retrieve data from RSS Feed.

<?php
$url=file_get_contents('http://www.phponwebsites.com/rss.xml');
$xml=new SimpleXMLElement($url);
//print_R($xml);
foreach($xml->channel->item as $val)
 {
  echo $val->title.' '. $val->link.'<br>';
 }
?>


 When you give print_R() in above PHP file, you can see the output on screen like below.


  SimpleXMLElement Object (
   [@attributes] => Array ( [version] => 2.0 )
   [channel] => SimpleXMLElement Object (
         [title] => Phponwebsites
[link] => http://www.phponwebsites.com
[description] => Phponwebsites about php related topics like .htaccess, robots.txt, mysql, jquery, ajax, js and also php for php developers
         [language] => en-us
[item] => Array (
          [0] => SimpleXMLElement Object (
            [title] => PHP
  [link] => http://www.phponwebsites.com/p/php.html
               [description] => PHP doubts clarified at Phponwebsites
                )
         [1] => SimpleXMLElement Object (
            [title] => MySQL
           [link] => http://www.phponwebsites.com/p/mysql.html
               [description] => MySQL doubts clarified at Phponwebsites
               )
           [2] => SimpleXMLElement Object (
           [title] => htaccess
          [link] => http://www.phponwebsites.com/p/htaccess.html
              [description] => htaccess doubts clarified at Phponwebsites
               )
          )
    )
)

Now you can retrieve data from RSS Feed.

Related Post:
Create Sitemap dynamically using PHP and MySQL
Create RSS Feed dynamically using PHP and MySQL
PHP : Create json file
Create XML file using PHP and MySQL
PHP : Parse data from XML Sitemap using simplexml_load_file and file_get_elements
Read data from JSON file using PHP
Parse data from XML file using PHP

19 Aug 2014

Create RSS Feed dynamically using PHP and MySQL

                                    You can create RSS feed dynamically using PHP like how you create sitemap dynamically using PHP and MySQL. Before you are going to know create RSS Feed, you should know what is RSS Feed? and why you need to add RSS Feed to your websites?

What is RSS Feed?


                                   RSS expands Really Simple Syndication. It is developed in XML like Sitemap. It is like content delivery vehicle. That means, you created a product, Ii distributes it to all places. Due to this feed, the viewers of your websites can easily find the latest pages of your websites. So there is no need to spend more time to search their desired topics on your web contents. This is the short description about RSS Feed. 

For more details, visit
    RSS Feed in W3schools
    RSS Feed at rss.softwaregarden
    what is RSS Feed
    Definition of RSS Feed at press-feed

Structure of RSS Feed

             
                                   The structure of RSS Feed is described below.


<?xml version='1.0' encoding='UTF-8'?>
<rss version='2.0'>
<channel>
    <title>Phponwebsites</title>
    <link>http://www.phponwebsites.com</link>
    <description>Phponwebsites about php related topics like .htaccess, robots.txt, mysql, jquery, ajax, js and also php for php developers</description>
    <language>en-us</language>
    <item>
      <title>PHP</title>
      <link>http://www.phponwebsites.com/p/php.html</link>
      <description>PHP doubts clarified at Phponwebsites</description>
   </item>
  <item>
      <title>MySQL</title>
      <link>http://www.phponwebsites.com/p/mysql.html</link>
      <description>MySQL doubts clarified at Phponwebsites</description>
   </item>
  <item>
      <title>htaccess</title>
      <link>http://www.phponwebsites.com/p/htaccess.html</link>
      <description>Htaccess doubts clarified at Phponwebsites</description>
   </item>
</channel>
</rss>

Generate RSS Feed dynamically using PHP and MySQL


                 Let see how to create RSS Feed dynamically using PHP and MySQL. Follow the below steps to create feed.


1. Create database and table


                  First you create database for store your link details on it. The below MySQL query is used for create database.

                Create database new

 where new is database name

                Use new

The above query is used to use database "new".
                  The MySQL query for create table is,

                Create table links (id int not null auto_increment primary key, name varchar(200), link varchar(200), description varchar(250))

                 Now the table "links" is created. Then store values into table using below query

                insert into table links values('','PHP','http://www.phponwebsites.com/p/php.html','PHP doubts are clarified at Phponwebsites')

                After insert values into table, your table look likes below

insert values into MySQL table for RSS Feed


2. Add header type RSS Feed


                            First you need to add content type of RSS feed. It is represented below.

header("Content-Type: application/rss+xml; charset=ISO-8859-1");


3. Add content to RSS Feed

                         
                            First of all you need to store all links in your database. Then on;y you can retrieve it using PHP. The below example we Retrieve data from MySQL database using mysql_fetch_array in PHP.
After added header, you have to add some name spaces for RSS Feed. Finally add contents to RSS Feed.


<?php
header("Content-Type: application/rss+xml; charset=ISO-8859-1");
echo '<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?>
<?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?>
<rss
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
    xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0"
version="2.0">
<channel>
<title>Phponwebsites</title>
<link>http://www.phponwebsites.com</link>
<description>Phponwebsites about php related topics like .htaccess, robots.txt, mysql, jquery, ajax, js and also php for php developers</description>';
       $connection = @mysql_connect('localhost', 'root', '') or die(mysql_error());
       mysql_select_db('new')  or die (mysql_error());
       $query = "SELECT * FROM links order by id desc";
       $rss = mysql_query($query) or die (mysql_error());
       $ImgPath='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwiEMDL1tvD_oaHIph6KkufjJK5hc5m7kwdJoVgkjFJuf4oqWWbSQXNDkuq1-ukdOhgudTe6gou7Al4oAbvJ-oC-ejuZEhre-ue99v02aajGkKGacaDkZPfk09v7DvUzZlJbIoaxNajoMu/s1600/logo5.png';
       while($row=mysql_fetch_array($rss)){
       echo '
<item>
<title>'.$row['name'].'</title>
<link>'.$row['link'].'</link>
<content:encoded>
      <![CDATA[<p align="left">
          <a href="'.$row['link'].'"><img style="background-image: none; " border="0" src="'.$ImgPath.'" />    </a></p>]]>
       </content:encoded>
       <description>'.$row['description'].'</description></item>';
}
      echo '</channel>
</rss>';
?>

               When you run this file on firfox, you will get output like below

Create RSS Feed dynamicall using PHP and MySQL


4. Htaccess


                         Then you have to add below lines to your htaccess file.


                    RewriteRule ^rss.xml$ rss.php [L]

When you type rss.xml in your browser address bar, it redirects to rss.php.

             Finally you got dynamic RSS Feed.

Related Post:
Read data from RSS Feed using PHP
Create json file using PHP and MySQL
Create XML file using PHP and MySQL
PHP : Parse data from XML Sitemap using simplexml_load_file and file_get_elements
Read data from JSON file using PHP
Parse data from XML file using PHP