How to parse RSS feed from other website on PHP CodeIgniter

Assalamu’alaikum warahmatullah… Today I’ll share about something simplicity but sometimes it’s make newbies slowdown the project. If we work with multiple website where its have some corelation, sometimes we need to parse news feed from another site. We have RSS and Atom. RSS read an xml document and today I’ll not show you how to build some RSS for CodeIgniter website, but I’ll tell you how to read an xml document from another web in our project using SimpleXMLElement who its available in PHP language, so its on CodeIgniter.

$get_feeds = new SimpleXMLElement('[URL]', null, true);

Thats the main code that we need in PHP to use SimpleXMLElement. Actualy, there is many option menu for using this features, but you need to search more.

The $get_feeds variable is array who save xml data from URL we need. In CodeIgniter, we have Controller and View to implement it. You can place the main code to your controller, and save it on variable that you load on your view. For example, look at the code above.

$data['xml'] = new SimpleXMLElement('URL', null, true);

//and you need to load the view with the data include

And then in your view, you need to loop your array in you can modify with your style. In this case, I’ll show my loop.

  for($i=0; $i<6; $i++)
   $url = $xml->channel->item[$i]->link;
   $title = $xml->channel->item[$i]->title;
   echo '<li><a href="'.$url.'" target="_blank">'.$title.'</a></li>';

Title and Link is variable name that i got from xml, and I loop it. You can also loop many things, such us description, or other. For optional varible, you can look at your file, and you feel find that.

Look at the

for($i=0; $i<6; $i++)

You can limit the query, because its so far if you want to load all data from that site. You can limit by the looping condition, I use 6 for looping maximum.

I think enough, if you need more you can search another resource or comment in bottom area.

Wa’alaikumussalam warahmatullah

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments

Would love your thoughts, please comment.x