There are so many popular sites providing services these days that you're bound to want to integrate with them to display feeds of your own or others content on them. In this article we take a look at how you can achieve this in PHP to consume feeds from Flickr, Twitter, and Vimeo.
Although Twitter have a widget available to be displayed on your site that doesn't make it the best option - if Twitter is down or is responding slowly then it may slow your site's response time down too. It's also not that flexible, so the solution is to use the Twitter API to create your own Twitter widget for displaying tweets from yourself, friends, or anyone.
Twitter has received a lot of media attention lately and a lot of people are wanting to put feeds from Twitter on their site. The most obvious way to do this is to use the widget that they provide, but it's not that customisable. Here we'll look at the various types of feed you can have, and how to cache the responses to minimise access bandwidth usage.
When deciding to add a Twitter feed to my own site I decided to only have my own tweets and to not include responses as you have no control over what would appear then - people could use @replies to deface your site or to put adverts on your site even when they're not in your friends list (though there are ways round this). So now lets look at interacting with the Twitter API.
Retrieving a User Timeline
It doesn't necessarily have to be your own timeline that you display, but I imagine you'll want it to be! To retrieve the timeline I used the following function:
define('TWITTER_URL', 'http://twitter.com/');
function _getUserTimeline ($username = '', $limit = 20)
{
if ($username == '') {
$username = $this->username
}
$url = TWITTER_URL . 'statuses/user_timeline/' . urlencode($username)
. '.' . $this->output_type . '?count=' . intval($count);
return $this->_getResponse($url);
}
This code is not complete by itself - the function belongs inside of a class, and the class also requires a _getResponse function. The _getResponse function will retrieve XML outputted by the URL, and then uses simplexml_load_string to turn this into an object. The function defined above is very basic and will use whatever username the class has stored if one has not been passed into it, and the output type will depend on what is defined in the class. The default is XML, but a JSON object is also possible. In the object returned there will be two items: @attribute, and status. The @attributes item is likely to be useless to you so we shall discard it and use just the status information. To parse the XML we can add the following member function to the class.
function _getResponse ($url, $postargs = '')
{
if (function_exists('curl_init')) {
$ch = curl_init($url);
if ($postargs != '') {
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postargs);
}
// Send auth details if required
if ($this->username !== false && $this->password !== false) {
curl_setopt($ch, CURLOPT_USERPWD, $this->username.':'.$this->password);
}
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$this->responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if (intval($this->responseCode) == 200) {
$response = simplexml_load_string($response);
$statuses = array();
if (!empty($response->status)) {
foreach ($response->status as $status) {
$statuses[] = $status;
}
}
$this->response = $statuses;
}
}
return false;
}
These two functions are the main bulk of handling a feed from Twitter where the first one will set up the URL for the API request the second function actually carries it out and requests data from Twitter's API and then processes the response. The resultant HTTP code is stored in the member variable $responseCode and the response in $response after having been parsed from XML into an object by SimpleXML.
Before this will work this needs putting into a class with a constructor and the member variables that are required by these two functions. In my case I called the class TwitterFeed and I added variables for username, password, output_type, responseCode and response. At this point you will now be ready to use the class file and to output a feed of the status message responses. All you need to do now is to iterate the contents of $feed->response to produce HTML to your liking. One thing I also like to do at this point is to turn URLs into links and mentions of other Twitter users into links to their profile pages.













