Get Your Latest Tweet with PHP v2.0

By: , On Wednesday, March 6th, 2013

latesttweet_code

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)

<span style="font-style: italic;">&lt;?php</span>
<span style="font-style: italic;">/**</span>
<span style="font-style: italic;"> * Latest Tweet Class</span>
<span style="font-style: italic;">  * Returns search results from Twitter's search API</span>
<span style="font-style: italic;"> * @author Wardell Latham &lt;wardell@wardelldesign.com&gt;</span>
<span style="font-style: italic;"> * @version 2.0</span>
<span style="font-style: italic;"> */</span>
<span style="font-weight: bold;">class</span> <span style="font-weight: bold;">latestTweet</span> {

    <span style="font-weight: bold;">public</span> $queryString;
    <span style="font-weight: bold;">public</span> $json;
    <span style="font-weight: bold;">public</span> $results;

    <span style="font-style: italic;">/**</span>
<span style="font-style: italic;">     * Get Twitter Results</span>
<span style="font-style: italic;">     * @param string $u optional name of user to search from</span>
<span style="font-style: italic;">     * @param integer $n number of tweets to return</span>
<span style="font-style: italic;">     * @param string $q optional query string</span>
<span style="font-style: italic;">     * @param string $t type of results mixed|recent|popular</span>
<span style="font-style: italic;">     */</span>
    <span style="font-weight: bold;">function</span> getResults($u = <span style="font-style: italic;">""</span>, $n = 1, $q = <span style="font-style: italic;">""</span>, $t = <span style="font-style: italic;">"recent"</span>) {

        <span style="font-weight: bold;">if</span>($u==<span style="font-style: italic;">""</span> &amp;&amp; $q==<span style="font-style: italic;">""</span>){
            $u=<span style="font-style: italic;">'twitterapi'</span>;
        }
        <span style="font-weight: bold;">if</span>($u!=<span style="font-style: italic;">""</span>){
            $q.=<span style="font-style: italic;">"+from:"</span>.$u;
        }
        $q = urlencode($q);
        $q.=<span style="font-style: italic;">"&amp;result_type="</span>.$t;
        $q.=<span style="font-style: italic;">"&amp;rpp="</span>.$n;

        $j = file_get_contents(<span style="font-style: italic;">'http://search.twitter.com/search.json?q='</span> . $q);
        $r = json_decode($j, <span style="font-weight: bold;">true</span>);

        $this-&gt;queryString = $q;
        $this-&gt;json = $j;
        $this-&gt;results = $r;

        <span style="font-weight: bold;">echo</span> <span style="font-style: italic;">'&lt;div id="latest_tweet"&gt;'</span>;
        <span style="font-style: italic;">/**</span>
<span style="font-style: italic;">         * Loop through and echo each result from the jason data to the page.</span>
<span style="font-style: italic;">         */</span>
        <span style="font-weight: bold;">foreach</span> ($this-&gt;results[<span style="font-style: italic;">'results'</span>] <span style="font-weight: bold;">as</span> $tweet) {
            <span style="font-weight: bold;">echo</span> <span style="font-style: italic;">'&lt;div class="tweet"&gt;&lt;div class="profile_image"&gt;</span>
<span style="font-style: italic;">                &lt;a href="https://twitter.com/'</span> . $tweet[<span style="font-style: italic;">'from_user'</span>] . <span style="font-style: italic;">'"&gt;&lt;img src="'</span> . $tweet[<span style="font-style: italic;">'profile_image_url'</span>] . <span style="font-style: italic;">'" /&gt;&lt;/a&gt;</span>
<span style="font-style: italic;">                &lt;/div&gt;</span>
<span style="font-style: italic;">                &lt;a class="user_name" href="https://twitter.com/'</span> . $tweet[<span style="font-style: italic;">'from_user'</span>] . <span style="font-style: italic;">'"&gt;'</span> . $tweet[<span style="font-style: italic;">'from_user_name'</span>] . <span style="font-style: italic;">'&lt;/a&gt;</span>
<span style="font-style: italic;">                &lt;a class="user" href="https://twitter.com/'</span> . $tweet[<span style="font-style: italic;">'from_user'</span>] . <span style="font-style: italic;">'"&gt;@'</span> . $tweet[<span style="font-style: italic;">'from_user'</span>] . <span style="font-style: italic;">'&lt;/a&gt;&lt;span class="created_at"&gt;'</span> . $tweet[<span style="font-style: italic;">'created_at'</span>] .<span style="font-style: italic;">'&lt;/span&gt;</span>
<span style="font-style: italic;">                &lt;p class="tweet_text"&gt;'</span>. $tweet[<span style="font-style: italic;">'text'</span>] . <span style="font-style: italic;">'&lt;/p&gt;&lt;/div&gt;'</span>;
        }
        <span style="font-weight: bold;">echo</span> <span style="font-style: italic;">'&lt;/div&gt;'</span>;
    }<span style="font-style: italic;">//end get results method</span>
}

<span style="font-style: italic;">?&gt;</span>

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.

<span style="font-style: italic;">&lt;?php</span>
<span style="font-weight: bold;">require_once</span> <span style="font-style: italic;">'latestTweet.php'</span>;
<span style="font-style: italic;">?&gt;</span>
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;&lt;/title&gt;
        &lt;style&gt;
            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;
            }
        &lt;/style&gt;
    &lt;/head&gt;
    &lt;body&gt;

        <span style="font-style: italic;">&lt;?php</span>
        $myTweets = <span style="font-weight: bold;">new</span> latestTweet();
        $myTweets-&gt;getResults(<span style="font-style: italic;">'wardelldesign'</span>, 1);
        unset($myTweets);
        <span style="font-style: italic;">?&gt;</span>
    &lt;/body&gt;

&lt;/html&gt;

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: androidscoop.co.uk

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