Recently, Twitter made some changes and dropped support for RSS (as well as imposing plenty of other measures and limitations along the way). You can read about it here – http://mashable.com/2012/09/05/twitter-api-rss/
A while back, I shared some code on How to get a Twitter feed into your WordPress theme. These new changes to Twitter’s API mean that this method no longer works. So, I’ve updated it.
I’ve also moved it into a Class. This needs updating already, as the JSON API actually gives us access to all sorts of new things, but this should be a good starting point.
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
// Twitter Class // Usage: // $twitter = new Twitter; // $twitter->twitter_messages(); class Twitter{ function __construct() { $this->Twitter(); } function Twitter(){ define('MAGPIE_CACHE_ON', 1); //2.7 Cache Bug define('MAGPIE_CACHE_AGE', 1800); define('MAGPIE_INPUT_ENCODING', 'UTF-8'); define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); } public function twitter_messages($username = 'douglasradburn', $amount = 5, $error = 'Unable to retrieve tweets at this time.') { try { $feedDetails = $this->curlGet('https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=' . $username . '&&count=' . $amount); $feed = json_decode($feedDetails); $i=0; foreach($feed as $item){ printf('<li> %s <span><a href="https://twitter.com/' . $username . '/status/%s">%s</a></span> </li>',$this->twitterify(strip_tags(str_replace($username.': ', '', $item->text))), $item->id, contextualTime(strtotime($item->created_at))); $i++; } } catch (Exception $exception) { echo $error; } } private function twitterify($ret) { # thanks to http://www.snipe.net/2009/09/php-twitter-clickable-links/#axzz0psFrJPHB $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2", $ret); $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2", $ret); $ret = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $ret); $ret = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $ret); return $ret; } private function curlGet($url) { // is cURL installed yet? if (!function_exists('curl_init')){ echo 'Sorry cURL is not installed!'; return; } $ch = curl_init(); // the target curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return the page curl_setopt($ch, CURLOPT_URL, $url); $data = curl_exec($ch); curl_close($ch); return $data; } } |
Image Credit: Ian Sane
Pingback: Scion Players | A Wordpress Twitter stream, without the RSS… | Web Development …