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

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 
 * @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('' . $q);
        $r = json_decode($j, true);

        $this->queryString = $q;
        $this->json = $j;
        $this->results = $r;
        echo '
'; /** * Loop through and echo each result from the jason data to the page. */ foreach ($this->results['results'] as $tweet) { echo ''; } echo '
; }//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]

2 Responses to “Get Your Latest Tweet with PHP v2.0”

  1. Mike Thornley Says:

    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 blog post ..New Blog:

    Wardell Latham Reply:

    Thanks Mike! I hope you find this script as useful as the last.

Loading Facebook Comments ...

© Wardell Design 2015
Entries (RSS) and Comments (RSS).