![]() |
![]() |
|||||||||||||||
|
||||||||||||||||
|
![]() |
Simple PHP RSS loader
A little while ago I wrote a simple RSS parser and loader in PHP for a client. The purpose for me was to read an RSS feed and copy the contents all so the clients blogspot blog can also be presented on their own website. Anyway here is the main guts of the script, it uses cURL and XML/DOM. <?php$ch = curl_init(); $feedurl = "http://yourfeedurlhere"; curl_setopt($ch, CURLOPT_URL, $feedurl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $blogfeed = curl_exec($ch); curl_close($ch); $blog = DOMDocument::loadXML($blogfeed); $entries = $blog->getElementsByTagName("item"); foreach($entries as $entry){ $pubdatenode = $entry->getElementsByTagName("pubDate"); $pubdate = $pubdatenode -> item(0)->nodeValue; $adate = date("Y-m-d H:i:s",strtotime($pubdate)); $titlenode = $entry->getElementsByTagName("title"); $title = $titlenode -> item(0)->nodeValue; $descriptionnode = $entry->getElementsByTagName("description"); $description = $descriptionnode -> item(0)->nodeValue; } ?> So inside the foreach loop there are three variables parsed out of each item in the feed. One for the title (title), the posting date (adate) and the description or content field (description). I then take that date and optionally update a database with the information but you could do whatever you need to at that point. I haven't extracted the link or comment fields because I either already know that information or don't even really want it, again you could extract that as well if you wished, the same applies for other channel entities. At any rate there are other, more complex libraries, out there that will turn the feed into a list of objects for you but for my needs a simple solution works well so it might help someone else as well. Tags Categories Comments |
||||||||||||||
|
Home | About | Blog | Stuff | Contact | Privacy Policy | |||||||||||||||
| © 2008 Max Stocker | ||||||||||||||||