Sometimes it can be hard to keep track of all your conversations on twitter. To track whats been said to who you need to look through your mentions/replies and also look through your own status timeline. Here is a basic introduction on how to set up a simple threaded conversation script in php so you can see a conversation on one page.
- Server or PC running Php 5.2+
- You will also need to get the twitter library from github – twitter.lib.php. It was created by Justin Poliey and uses a simple interface to easily connect to all of twitters api methods. Take a look through the documentation provided to see the various methods that are supported.
First, lets include the twitter.lib.php file into our script:
include("includes/twitter.lib.php");
Next, we need to create a new instance of the Twitter class, passing in our Twitter username and password:
$username = "twitter_username"; $password = "twitter_password"; $twitter = new Twitter($username, $password);
Now that we have created a twitter object we can now use some methods to get data out of the twitter api. To build our threaded conversation script we will need to get any occurrences where someone has mentioned us.
// This array is passed into getMentions, for this example we will pass a blank array // Options can include: page, count, since_id, max_id $arrOptions = Array(); // Get statuses where you are mentioned $mentions = $twitter->getMentions($arrOptions); // Convert the xml document into an array $mentions = simplexml_load_string($mentions);
The above code will return the 20 most recent mentions (status containing @username) for the authenticating user. To see all the options that can be passed into the statuses/mentions api call view the Twitter Status/Mentions API page.
In the following chunk of code we will loop through the $mentions array, check if the users screen name matches the friend that we wish to see a mention history of and if it does pop it into an array called $statuses. You will notice that the status array is indexed by status id ($item->id), this is so we can later sort the index in sequence of when the status occurred.
$user_to = "friends_username";
foreach ($mentions as $item) {
if ($item->user->screen_name == $user_to) {
//Use status id as index so we can get the sequence correct
$index = (string) $item->id;
$statuses[$index]["screen_name"] = (string) $item->user->screen_name;
$statuses[$index]["url"] = (string) $item->user->url;
$statuses[$index]["profile_image_url"] = (string) $item->user->profile_image_url;
$statuses[$index]["text"] = (string) $item->text;
}
}
$item array is cast as a string – (string). This is because each item is an xml object and if we don’t store it as a string it can cause us grief later.Now we need to get the data from our own timeline and find all the responses we made.
// Passed into getUserTimeline $arrOptions = Array(); // Get your status timeline $timeline = $twitter->getUserTimeline($arrOptions); // Convert the xml document into an array $timeline = simplexml_load_string($timeline);
Again we need to loop through each item in the array and filter any responses to the user we are interested in. You will notice I use in_reply_to_screen_name, this is a quick way to see who the selected status was directed to.
foreach ($timeline as $item) {
if ($item->in_reply_to_screen_name == $user_to) {
$index = (string) $item->id;
$statuses[$index]["screen_name"] = (string) $item->user->screen_name;
$statuses[$index]["url"] = (string) $item->user->url;
$statuses[$index]["profile_image_url"] = (string) $item->user->profile_image_url;
$statuses[$index]["text"] = (string) $item->text;
}
}
So now lets sort the $statuses array by key using $ksort:
//Sort array by key ksort($statuses);
This php sort function is perfect for this application as it will sort by key (low to high) and maintains key association. For a list of the various php array sorts, take a look at Sorting Arrays.
Then its just a case of looping though our $statuses array and display the information in a nice format:
foreach ($statuses as $item) {
echo "<div style='padding:10px;width:500px;border-bottom:1px dotted #000;overflow:hidden;'>";
echo "<img src='".$item["profile_image_url"]."' height='48px' width='48px' align='left'> ".$item["screen_name"].": ".$item["text"];
echo "</div>";
}
And thats it. A very simple threaded conversation script that could easily be developed further into a full application.
