17

Jun

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.

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.

1

2

3

4

<?php

/* Wrap the following in pre tags */

print_r($sXML);

?>

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:

1

2

3

4

5

6

7

8

9

<?php

$screen_name=$sXML->screen_name;

$location=$sXML->location;

$description=$sXML->description;

$followers_count=$sXML->followers_count;

$favourites_count=$sXML->favourites_count;

$friends_count=$sXML->friends_count;

$text=$sXML->status->text;

?>

… 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

1

2

3

4

5

6

7

8

9

10

11

12

13

<?php

functiontweetcount($url){

$twitterjson="http://urls.api.twitter.com/1/urls/count.json?url=%s";

$fileData=file_get_contents(sprintf($twitterjson,$url));

$json=json_decode($fileData,true);// true will be converted into associative array

Support

About Marty

Marty is a passionate web developer from Sydney, Australia. He owns about 600 websites and makes a healthy living from working the web. As a day job, he works as a pilot for an international airline. Follow Marty on Twitter or Google+.

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.

Search Internoetics

Find us on Facebook

Internoetics on Twitter

Sponsored Link

Internoetics on Google+

Internoetics

31st July 2015: A server upset is causing us to restore certain parts of this (and other) websites over the next week; this includes a few articles that may include outdated shortcodes. If you encounter any errors, please let us know.