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 $this->load->view('view_feed',$data);
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.
<ul> <?php 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>'; } ?> </ul>
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 http://url.com/rss.xml 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