twitter-data-website

Include Twitter Data on your Website

Every application I build now is littered with various types of functionality that links them into different social network. If you’re reading this post it’s unlikely I’ll have to educate you on the merits of plugging your website into the social lives of your readers.

What follows is some code that will help you integrate twitter data into your website in a more meaningful way than simply including just a retweet button.

Twitter Data on your Website

Note: Unauthenticated requests to the Twitter API are no longer permitted. This code will no longer work. We’ll post something again in the future.

Twitter make an XML file available for each visible Twitter profile with various pieces of useful data… have a look at my profile data here (link is no longer valid). Since it’s an XML file, we can easily parse it and extract the information for use in our own applications.

I’m in the process of building a large number of websites that syndicates podcasts and I wanted to include a small Twitter profile box attached to every author’s page. Although very much in development, this is an image of what I’m rendering on a profile page for each podcast.

It’s basically a simple box with some information extracted from the XML data referenced above. Of course, I could use more data and add a follow button – and I may end up doing just that – but it serves as a basic example of what you’ll accomplish with the very simple code below.

To use the data, you’ll have to extract it into a more meaningful format.

SimpleXMLElement returns an object representing data.

Once we extract the elements of the XML data, we can print the object array simply so you have a feel for data that’s returned.

The variable $sXML is now an object with all the XML tags being attributes. To retrieve the any element you’ll simply access it in PHP’s object form. For example, here’s what you’ll use to extract certain elements:

… and so on.

Simple!

In my example above, I’ve parsed usernames, hashtags and URL’s in the most recent tweet ($sXML->status->text) so they become clickable links. You can do that with the following.

Of course, you’ll want to store the XML values into a database or flat file for a defined period of time to avoided repeated requests to Twitter.

A better way of determining the number of times your URL has been retweeted

A couple of weeks ago, I showed you how to find the number of times your URL had been tweeted to Twitter using the TweetMeme API. Since I’ve been using it, I’ve found that it’s relatively unreliable and not nearly as accurate as obtaining the same data from Twitter (obviously). I had been using Twitter data and TweetMeme data on the same site meaning that there was a clear discrepancy when both sources happened to be rendered on the same page.

As an example, a link to one of my podcasts indicates 58 tweets via TweetMeme and 73 via Twitter. Not good.

Using Twitter’s API (via the JSON API), information retrieved is far more accurate.

From Wikipedia, JSON (an acronym for JavaScript Object Notation) is a lightweight text-based open standard designed for human-readable data interchange. It is derived from the JavaScript scripting language for representing simple data structures and associative arrays, called objects. Despite its relationship to JavaScript, it is language-independent, with parsers available for most languages.

The code

PHP’s json_decode function is what’s used to covert the encoded string. When set to TRUE (as in the above example), returned objects will be converted into associative arrays.

Support

If you need any help, please get in touch with us.

6 comments

  1. l0garithm

    but how can you get a list of all your tweets?
    i see that in the status->text variable it holds the last submitted tweet only.

    • Marty
      Author

      Good question — and I assume you mean tweet specific lat/long coordinates or “posted from” details? I don’t think so… but I’ll have to check. I’ve used it before but I can’t recall where I got the info. I’ll get to it in a few days. If you find your answer before I do, please let me know.

  2. Pingback: Count the Number of Shares to Facebook, Twitter, LinkedIn & Google Plus in a WordPress post (with shortcode)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">