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,
    <?php
    //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);
    mysql_select_db($dbName)
    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,
    <?php
    //file name : initRSS.php
    //author : littleflow3r
    require_once 'simplepie.inc';
    //initialize simplePie
    $feed = new SimplePie('http://localhost/websmsmasking/feed/rss');
    $feed->set_cache_location('../rss/cache/');
    $feed->set_feed_url('http://localhost/websmsmasking/feed/rss');
    $feed->init();
    $feed->handle_content_type();
    ?>
  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,
    <?php
    //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);
    endforeach;
    ?>
  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.
    Result
  3. Check the database, see that tb_rss already filled with rss content from your rss’s address.
    Tb_RSS

Okay, Done. Happy Trying! Happy Coding :)
Hal yang juga menarik:

Hak Cipta

Semua skrip dan teknik dalam artikel di itx.web.id boleh digunakan sebagaimana kehendakmu tanpa perlu mencantumkan sumber. Kamu tidak boleh mengkopi seluruh artikel, dalam Bahasa Indonesia ataupun diterjemahkan ke dalam bahasa lain.


Related Articles:

Artikel terkait:

: PHP

About the author

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 lo

1 Comment

Tinggalkan Balasan

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

x( X-( B-) ;-) :wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :D :?: :-| :-x :-o :-P :-D :-? :-)) :-) :-( :-& :) :( :!: 8-O 8-) 8) (Русский) (yahoo) (worship) (woot) (wave) (unsure) (tongue) (thinking) (tears) (taser) (smileydance) (sleeping) (sick) (scenic) (rofl) (rock) (party) (panic) (okok) (nottalking) (ninja) (music) (muscle) (muhaha) (money) (mmm) (lonely) (lol) (lmao) (idiot) (hungry) (highfive) (heart_beat) (heart) (headspin) (hassle) (haha) (gym) (griltongue) (goodluck) (girlkiss) (funkydance) (fish_hit) (eyeroll) (evilsmirk) (evil_grin) (drinking) (doh) (devil) (dance) (cry) (cozy) (coffee) (brokenheart) (bringit) (blush) (bigeyes) (beer) (banana_rock) (banana_ninja) (banana_cool) (applause) (annoyed) (angry) (K) (: