Get the Other Site’s Feeds with SimplePie RSS Reader

Case Study : Get the other’s site feed with RSS Reader SimplePie, show it, and save it to database.
Requirements : SimplePie library, download here. Webserver Packages, already installed.

RSS (Really Simple Syndication) is a xml based format that used to sharing and delivering web content that usually updated regularly. While the term “output” refers to the outputs of RSS which can be read with RSS Feed Reader. SimplePie is a RSS Feed Reader/Parser that quite famous (and my favorite of course), the way RSS Feed Parser working is to read a XML format contained in the RSS Feed which we set ourselves.

Okay, lets give it a try.

Step 1 : Prepare the Database

  1. Create a new database named db_tutorial
  2. Create table tb_rss, with structure like the picture below.
    Table Structure
  3. Done with the database!

Step 2 : Prepare Working Folder

  1. Create a new folder named rss in your document root
  2. Create new folder named cache in the folder rss (…/rss/cache)
  3. Copy file SimplePie.inc (on simplepie zip file) in to folder rss.

Step 3 : Create a Connection to Database

  1. Create a script to connect to database, type the following script,
    //file name : connect.php
    //author : littleflow3r
    $host = "localhost";
    $user = "root"; //sesuaikan dengan setting mysql
    $pass = ""; //
    sesuaikan dengan setting mysql
    $dbName = "db_tutorial";
    mysql_connect($host, $user, $pass);
    or die ("Connect Failed !! : ".mysql_error());
  2. Save in the folder rss with name connect.php

Step 4 : Inisialisasi SimplePie

  1. Type the following script,
    //file name : initRSS.php
    //author : littleflow3r
    require_once 'simplepie.inc';
    //initialize simplePie
    $feed = new SimplePie('http://localhost/websmsmasking/feed/rss');
  2. save in the folder rss and named it initRSS.php
  3. Explanation : The way of SimplePie RSS Parser working is to read the XML format contained adalah dengan membaca format XML contained in the RSS Feed. The Code above is used to initializes SimplePie, by entering the address of our rss [baris 06 dan baris 08]. For example, i use the sample rss address is localhost/websmsmasking/feed/rss –> feed from my local application. You can change this to address that you want, but remember that to gets the rss feed from online sites, you have to be connected to internet, too (this is because simplepie will directly access to that sites.)

Step 5 : Show the Feed

  1. Type the following script,
    //file name : rss.php
    //author : littleflow3r
    include 'initRSS.php';
    include 'connect.php';
    //get the feed and show it
    echo "<h1> RSS News Update </h1>";
    foreach ($feed->get_items(0,4) as $item):
    $permalink = $item->get_permalink();
    $title = $item->get_title();
    $desc = $item->get_description();
    echo '<a href="'.$permalink.' ">'; echo $title; echo '</a>';
    echo "<br>";
    echo $desc;
    echo "<br>"; echo "<br>";
    //insert to database
    $q = "INSERT into tb_rss (link, title, description) VALUES('$permalink','$title','$desc')";
    $result = mysql_query($q);
  2. Save in the folder rss, named it rss.php
  3. Explanation: Baris 08 show looping with foreach according to the number of feed obtained from the address of the set RSS’s address, by the get_items(0,4)function, the intention is to take 4 feed items starting from the 0 record.
    Baris 09 save the permalink from each itemsin to variabel $permalink
    Baris 10 get the feed title by the get_title function and save it to $title.
    Baris 11 get the feed description with the get_desc function and save it to $desc.

Step 6 : Testing Code

  1. Go to brwoser, run http://localhost/rss/rss.php
  2. You will see the feed from your rss’s address like image below.
  3. Check the database, see that tb_rss already filled with rss content from your rss’s address.

Okay, Done. Happy Trying! Happy Coding 🙂

Oleh littleflow3r

obviously, a girl. with sweet smile, off course. turning her 21 years trapped on Informatics departement and just started to having some crush with it lately. she uses wordpress. she loves php. she loves to satisfied her shopping hobbies just via looking around in online shop. oh yeah, she got an A for her english subject^^

1 reply on “Get the Other Site’s Feeds with SimplePie RSS Reader”

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

This site uses Akismet to reduce spam. Learn how your comment data is processed.