Web Developer Class: Building a Twitter Feed with PHP
Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js
Now Let's Analyze
As you can see from the code above, we are really creating just a simple class with two public methods - which will simply get the Twitter feed according to a Twitter ID. Let's look at this in more detail:
Line 19: We set the Twitter ID as a public property of the class for general use.
Line 24-36: The twitter_status method: This function simply takes the Twitter ID supplied and grabs the Twitter feed directly from twitter. In general you can use the file_get_contents($url) here too, depending on your server setup. This method simply returns the contents of the RSS feed.
Line 51: Returns an object of class SimpleXMLElement with properties containing the data held within the xml document.
Line 54-104: Returns an object of class SimpleXMLElement with properties containing the data held within the xml document. We take this object, loop through it and grab whatever we need to grab. Below we show you a simple printout of one iteration of the feed:
As you can see, we now have a lot of information that we could use to build our little Twitter feed from scratch, and although I have just grabbed one or two things in the get_tweets method, we can literally use anything there to make up our nice little feed. We have the user's profile colors, profile image, tweet info, and literally anything else about them that twitter can give us without breaking the user's privacy.
And how to use it
It's quite simple: I run a cronjob from a separate page by instantiating the Twitter Object and calling the get_tweets method as below:
Now that we have seen how simple it is to actually read an RSS Feed, we can also see that PHP is a very powerful application with nice built in functions like simplexml_load_string. With this power, we can easily break up our feed into usable chucks that we can get working rather simply. Obviously the full power of this application comes into play when you save the info in a database and manipulate it accordingly.
Until next time.