Get Your Latest Tweet with PHP v2.0
Way back in 2010 I created a PHP script that retrieved the latest tweet from a given user via an XML Atom file generated by the Twitter search API. Last year in 2012 twitter dropped XML support from its API, so I though I’d revisit the issue of retrieving your latest tweet, or last few tweets using PHP.
This time around
it’s the script is OOP flavored and uses json_decode to parse through the delicious chunks of JSON goodness. Here is a link to the code in action http://www.wardelldesign.com/examples/latesttweet/
Here is the code for the class that retrieves the tweet(s)
<?php /** * Latest Tweet Class * Returns search results from Twitter's search API * @author Wardell Latham <wardell@wardelldesign.com> * @version 2.0 */ class latestTweet { public $queryString; public $json; public $results; /** * Get Twitter Results * @param string $u optional name of user to search from * @param integer $n number of tweets to return * @param string $q optional query string * @param string $t type of results mixed|recent|popular */ function getResults($u = "", $n = 1, $q = "", $t = "recent") { if($u=="" && $q==""){ $u='twitterapi'; } if($u!=""){ $q.="+from:".$u; } $q = urlencode($q); $q.="&result_type=".$t; $q.="&rpp=".$n; $j = file_get_contents('http://search.twitter.com/search.json?q=' . $q); $r = json_decode($j, true); $this->queryString = $q; $this->json = $j; $this->results = $r; echo '<div id="latest_tweet">'; /** * Loop through and echo each result from the jason data to the page. */ foreach ($this->results['results'] as $tweet) { echo '<div class="tweet"><div class="profile_image"> <a href="https://twitter.com/' . $tweet['from_user'] . '"><img src="' . $tweet['profile_image_url'] . '" /></a> </div> <a class="user_name" href="https://twitter.com/' . $tweet['from_user'] . '">' . $tweet['from_user_name'] . '</a> <a class="user" href="https://twitter.com/' . $tweet['from_user'] . '">@' . $tweet['from_user'] . '</a><span class="created_at">' . $tweet['created_at'] .'</span> <p class="tweet_text">'. $tweet['text'] . '</p></div>'; } echo '</div>'; }//end get results method } ?>
Here is an example of how the above code could be used if it were placed inside of a file called latestTweet.php and included in separate PHP file.
<?php require_once 'latestTweet.php'; ?> <html> <head> <title></title> <style> body{ background: rgb(255,255,255); /* Old browsers */ background: -moz-radial-gradient(center, ellipse cover, rgba(255,255,255,1) 0%, rgba(64,150,238,1) 100%); /* FF3.6+ */ background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(0%,rgba(255,255,255,1)), color-stop(100%,rgba(64,150,238,1))); /* Chrome,Safari4+ */ background: -webkit-radial-gradient(center, ellipse cover, rgba(255,255,255,1) 0%,rgba(64,150,238,1) 100%); /* Chrome10+,Safari5.1+ */ background: -o-radial-gradient(center, ellipse cover, rgba(255,255,255,1) 0%,rgba(64,150,238,1) 100%); /* Opera 12+ */ background: -ms-radial-gradient(center, ellipse cover, rgba(255,255,255,1) 0%,rgba(64,150,238,1) 100%); /* IE10+ */ background: radial-gradient(ellipse at center, rgba(255,255,255,1) 0%,rgba(64,150,238,1) 100%); /* W3C */ filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#4096ee',GradientType=1 ); /* IE6-9 fallback on horizontal gradient */ } .tweet{ width: 50%; margin: 15px auto; border:1px solid #000; -webkit-border-radius: 30px; -moz-border-radius: 30px; border-radius: 30px; padding: 15px; -webkit-box-shadow: 0px 10px 10px 0px #999; box-shadow: 0px 10px 10px 0px #999; background: #fff; } .tweet_text{ margin:5px 0px; } .profile_image{ float:left; margin-right: 10px; } .user_name{ color:#000; font-size:large; text-decoration: none; font-weight: bold; } .user{ color:#ccc; font-weight: bold; } .created_at{ float: right; font-size:x-small; padding-left: 10px; } </style> </head> <body> <?php $myTweets = new latestTweet(); $myTweets->getResults('wardelldesign', 1); unset($myTweets); ?> </body> </html>
Get your latest tweet with PHP v2.0 [download]








March 6th, 2013 at 9:53 pm
That’s a useful post Wardell!. I believe things will be wrapping up on the 5th API wise so I was looking for an alternative, having used your previous version.
Mike Thornley´s last [type] ..New Blog: androidscoop.co.uk
Reply
Wardell Latham Reply:
March 9th, 2013 at 3:55 pm
Thanks Mike! I hope you find this script as useful as the last.
Reply