(07) 3040 6690

Get Your Latest Tweet with PHP v2.0

By: , On Wednesday, March 6th, 2013


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)

 * 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==""){
        $q = urlencode($q);

        $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>
                <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.

require_once 'latestTweet.php';
                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 */

                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;
                margin:5px 0px;
                margin-right: 10px;
                text-decoration: none;
                font-weight: bold;
                font-weight: bold;
                float: right;
                padding-left: 10px;

        $myTweets = new latestTweet();
        $myTweets->getResults('wardelldesign', 1);


Get your latest tweet with PHP v2.0 [download]

Fatal error: Call to undefined function adsensem_ad() in /home/content/14/4543214/html/wordpress/wp-content/themes/wardelldesign/single.php on line 129