Get Latest Tweet PHP Code
Just under the title heading of this blog is a line displaying my latest tweet, however it doesn’t quite do what I want. I got the code (displayed below) from Joost de Valk’s blog; it will convert mentions into links, but not hashtags and URL links – something I would really like it to do.
Sadly I don’t have enough knowledge of regular expressions, or PHP, to modify this code to add that functionality, so if you’re able to help I would greatly appreciate it!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | < ?php require_once(ABSPATH . 'wp-includes/class-snoopy.php'); $tweet = get_option("lasttweet"); $url = "http://twitter.com/statuses/user_timeline/jamesbmarshall.json?count=20"; if ($tweet['lastcheck'] < ( mktime() - 60 ) ) { $snoopy = new Snoopy; $result = $snoopy->fetch($url); if ($result) { $twitterdata = json_decode($snoopy->results,true); $i = 0; while ($twitterdata[$i]['in_reply_to_user_id'] != '') { $i++; } $pattern = '/@([a-zA-Z]+)/'; $replace = '<a href="http://twitter.com/'.strtolower('1').'">@1'; $output = preg_replace($pattern,$replace,$twitterdata[$i]["text"]); $tweet['lastcheck'] = mktime(); $tweet['data'] = $output; $tweet['rawdata'] = $twitterdata; $tweet['followers'] = $twitterdata[0]['user']['followers_count']; update_option('lasttweet',$tweet); } else { echo "Twitter API not responding."; } } else { $output = $tweet['data']; } echo "Latest Tweet: ".""".$output."""; ?> |
Ideally, instead of just saying “Latest Tweet:” I’d kinda like that to be a link back to the tweet itself, too!
Update: This is the current version of the code. Thanks to Chris Alexander and Nick Telford it now links correctly, automatically converting mentions, hashtags and URLs into clickable links!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | < ?php $username = "jamesbmarshall"; require_once(ABSPATH . 'wp-includes/class-snoopy.php'); $tweet = get_option("lasttweet"); $url = "http://twitter.com/statuses/user_timeline/" . $username . ".json?count=20"; if ($tweet['lastcheck'] < ( mktime() - 60 ) ) { $snoopy = new Snoopy; $result = $snoopy->fetch($url); if ($result) { $twitterdata = json_decode($snoopy->results,true); $i = 0; while ($twitterdata[$i]['in_reply_to_user_id'] != '') { $i++; } // Hashtag, URL and Username linking - added by Chris Alexander, chris@chris-alexander.co.uk $output = $twitterdata[$i]['text']; //Replace URLs $output = preg_replace('@(https?://([-w.]+)+(:d+)?(/([w/_.]*(?S+)?)?)?)@i', '<a href="$1" target="_blank">$1</a>', $output); //Replace @mentions $output = preg_replace('/([^a-zA-Z0-9-_&])@([0-9a-zA-Z_]+)/','$1<a href="http://twitter.com/$2" target="_blank">@$2</a>',$output); //Replace #hashtags $output = preg_replace('/([^a-zA-Z0-9-_&])#([0-9a-zA-Z_]+)/',"$1<a href="http://twitter.com/search?q=%23$2" target="_blank">#$2</a>",$output); $prefix = '<a href="http://twitter.com/' . $username . '/status/' . $twitterdata[$i]['id'] . '" target="_blank">Latest Tweet</a>: '; $tweet['prefix'] = $prefix; $tweet['lastcheck'] = mktime(); $tweet['data'] = $output; $tweet['rawdata'] = $twitterdata; $tweet['followers'] = $twitterdata[0]['user']['followers_count']; update_option('lasttweet',$tweet); } else { echo "Twitter API not responding."; } } else { $output = $tweet['data']; $prefix = $tweet['prefix']; } echo $prefix . $output; ?> |
This is completely untested but you may like to try this out
<?php
$username = "jamesbmarshall";
require_once(ABSPATH . 'wp-includes/class-snoopy.php');
$tweet = get_option("lasttweet");
$url = "http://twitter.com/statuses/user_timeline/" . $username . ".json?count=20";
if ($tweet['lastcheck'] fetch($url);
if ($result) {
$twitterdata = json_decode($snoopy->results,true);
$i = 0;
while ($twitterdata[$i]['in_reply_to_user_id'] != ”) {
$i++;
}
// Hashtag, URL and Username linking – added by Chris Alexander
$output = $twitterdata[$i]['text'];
$output = preg_replace(‘/([^a-zA-Z0-9-_&])@([0-9a-zA-Z_]+)/’,”$1@$2“,$output);
$output = preg_replace(‘/([^a-zA-Z0-9-_&])#([0-9a-zA-Z_]+)/’,”$1#$2“,$output);
$output = preg_replace(‘@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@’, ‘$1‘, $output);
$prefix = ‘Latest Tweet: ‘;
$tweet['prefix'] = $prefix;
$tweet['lastcheck'] = mktime();
$tweet['data'] = $output;
$tweet['rawdata'] = $twitterdata;
$tweet['followers'] = $twitterdata[0]['user']['followers_count'];
update_option(‘lasttweet’,$tweet);
} else {
echo “Twitter API not responding.”;
}
} else {
$output = $tweet['data'];
$prefix = $tweet['prefix'];
}
echo $prefix . $output;
?>
[Reply]
This is what I tried:
< ?php
$username = "jamesbmarshall";
require_once(ABSPATH . 'wp-includes/class-snoopy.php');
$tweet = get_option("lasttweet");
$url = "http://twitter.com/statuses/user_timeline/" . $username . ".json?count=20";
if ($tweet['lastcheck'] < ( mktime() - 60 ) ) {
$snoopy = new Snoopy;
$result = $snoopy->fetch($url);
if ($result) {
$twitterdata = json_decode($snoopy->results,true);
$i = 0;
while ($twitterdata[$i]['in_reply_to_user_id'] != ”) {
$i++;
}
// Hashtag, URL and Username linking – added by Chris Alexander
$output = $twitterdata[$i]['text'];
$output = preg_replace(‘/([^a-zA-Z0-9-_&])@([0-9a-zA-Z_]+)/’,”$1@$2″, $output);
$output = preg_replace(‘/([^a-zA-Z0-9-_&])#([0-9a-zA-Z_]+)/’,”$1#$2″, $output);
$output = preg_replace(‘@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@’, ‘$1′, $output);
$prefix = ‘Latest Tweet: ‘;
$tweet['prefix'] = $prefix;
$tweet['lastcheck'] = mktime();
$tweet['data'] = $output;
$tweet['rawdata'] = $twitterdata;
$tweet['followers'] = $twitterdata[0]['user']['followers_count'];
update_option(‘lasttweet’,$tweet);
} else {
echo “Twitter API not responding.”;
}
} else {
$output = $tweet['data'];
$prefix = $tweet['prefix'];
}
echo $prefix . $output;
?>
[Reply]
Hi there, do you now of a way to display your latest tweet with a specific hashtag? My blog is about a running, but my tweets can be just about anything. So I’m hoping to display my latest tweets that have the hashtag #running somewhere on my blog. I’ve tried a few plugins (which I prefer not to use anyway), but haven’t found anything yet that works yet.
-Cheers
[Reply]
The code above does’t work?
[Reply]
[...] (August 2011)*: this is another fantastic implementation of a Twitter PHP script. I highly recommend [...]