
When we’re done, it should look like this demo Flickr module. In this tutorial, we’ll build a PHP class to load and parse a Flickr RSS feed, isolate the pieces of information we need, and output formatted XHTML to the browser.
#Rss reader php class how to#
We simply need to grab the content from our account, figure out how to read it right into our website, and then display it in such a way that it will mesh with the rest of our site’s design. This way, we don’t have to create any content at all. One solution is to use RSS feeds to grab the content off of our various social media sites (in this example, we’ll use Flickr). So, what we’re hoping for is a way to get the best of both worlds: how can we utilize the power of Flickr on our own website without needing to dedicate a great deal of time to solving the problem? The Solution Besides, doesn’t it seem a little silly to reinvent the wheel? The learning experience of building such tools is great, but we don’t always have hundreds of hours to dedicate to rebuilding Flickr’s robust photo management. On the flipside, you could reverse engineer all of these tools and create your own versions of them for use on your personal website, creating the same immersive user experience you get from using social tools, but that’s a good deal of time and effort. This is great, but it limits your creativity, and it also requires your readers to remember multiple separate URLs.
#Rss reader php class for free#
With sites like Myspace, Flickr, YouTube, and Twitter, you can essentially create a full-on interactive web experience for free and with no knowledge of web design. Cases 2 and 3 are just to show you how it can be done for more complex tags.The Internet seems to be getting easier everyday. 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).
#Rss reader php class code#
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.
