

If you have any questions regarding the creating of this layout, feel free to ask in the comment box at the end of this article.īefore going into the HTML for the site, this is how you should organize the file structure for the finished website: All it takes is fifteen minutes in Photoshop, using gradients and a bokeh effect for the header design. The design is simple and sleek, nothing too fancy. This is how the finished website will look. The idea is to write two PHP functions that will spit out different content one is for the display of the titles from the feed, and the other is for the content that will appear in a list form like those of a blog archive. We’ll be creating a simple site with just two pages home and news.
#Php rss reader tutorial code#
I won’t go much into the XHTML/CSS code in this tutorial. However, I will go into more details when we start writing the PHP code. In this tutorial, we will take a look at how the PHP parser creates an array in which each element will contain the data found in the RSS feed items. Wikipedia defines a parser as a part of a compiler, or an interpreter, which checks for proper syntax and builds a data structure based on that syntax.
#Php rss reader tutorial how to#
In today’s article, I will demonstrate how to write a simple function in PHP that allows you to display content from an RSS feed. Cases 2 and 3 are just to show you how it can be done for more complex tags.Recently, I was building a website for a telecommunications startup and because they didn’t have a lot of content for their website, I suggested they use the content from a Yahoo Finance RSS feed.

If you know the structure of the XML and it has lots of unique tags, I find that using the first case is the easy. I find the easiest way to use XMLReader is to know which tags I want the information out of and use them. As such, my answer assumes you want only specific information and you know where it is located. Most of my XML parsing life is spent extracting nuggets of useful information out of truckloads of XML (Amazon MWS). Remember that the more code you write, the higher the possibility of you introducing bugs or introducing performance regressions. Stay as far away from XMLReader as possible. My advice: write a prototype with SimpleXML, see if it works for you. Not as complicated and awkward as XMLReader, but light years away from working with SimpleXML. It's halfway between XMLReader and SimpleXML. I wish it was possible to use simplexml_import_dom() but it doesn't seem to work in that caseĬons: DOM is annoying to work with. Pros: uses about as much memory as SimpleXML, and XMLReader::expand() is faster than creating a new SimpleXMLElement. Even a modest machine would be able to process a thousand nodes per second, though. You really have to benchmark it to understand whether it's a problem for you. Pros: doesn't use much memory (only the memory needed to process one node) and SimpleXML is, as the name implies, really easy to use.Ĭons: creating a SimpleXMLElement object for each node is not very fast.

Plus, it leaves you with more lines of code to maintain Userland code is slow and prone to error. Quick overview of pros and cons of different approaches:Ĭons: excessively hard to write and debug, requires lots of userland code to do anything useful. now you can use $node without going insane about parsing $node = simplexml_import_dom($doc->importNode($z->expand(), true)) $node = new SimpleXMLElement($z->readOuterXML()) now that we're at the right depth, hop to the next until the end of the tree This way, you keep the memory usage low because you're treating one node at a time and you still leverage SimpleXML's ease of use. It all depends on how big the unit of work, but I guess you're trying to treat each nodes in succession.įor that, the simplest way would be to use XMLReader to get to each node, then use SimpleXML to access them.
