Create Custom Twitter Widget with Pagination Through Tweets

A block of code that allows you to create a twitter feed in a PHP environment. Enjoy!

You’ll first want to create a Twitter App to get your access token, access token secret, consumer key, and consumer secret for authentication later.

You’ll also want to download TwitterAPIExchange.php as you’ll need to ‘require()’ that later.

Create a file named initialize.php and put on your server. Include the following functions in it as you’ll need to use them for the feed/widget later.

<?php
require_once( 'initialize.php'); // include the functions you need
require_once( 'TwitterAPIExchange.php'); // include the twitter api
// validate the widget with you credentials
$settings = array(
    'oauth_access_token' => "xxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxx",
    'oauth_access_token_secret' => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    'consumer_key' => "xxxxxxxxxxxxxxxxxxxxxx",
    'consumer_secret' => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
);
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
// here we are scrubbing the url for the parameters but if none
// exist we are setting a search, a screen name, and a tweet
// count (per page)
$getfield = $_SERVER['QUERY_STRING'] ? '?'.$_SERVER['QUERY_STRING'] : '?q=kubashi&screen_name=kubashi&count=2';
$twitter = new TwitterAPIExchange($settings);
$store = $twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest();
$result = json_decode($store);
$multi_array = objectToArray($result);
?>
<ul class="tweets">
    <?php
    // Here's where the magic happens. We will loop through
    // the array given in '$multi_array' and set our tweet layout
    foreach($multi_array['statuses'] as $key => $value ){ ?>
        <?php
        $user = $value['user'];
        $entities = $value['entities'];
        $tweet = links($value["text"]); //converts text links to clickable links
        ?>
        <li class="tweet">
            <div class="text">
                <?php echo $tweet; ?>
            </div>
            <?php if($entities['media']){ ?>
                <?php foreach($entities['media'] as $offset => $entity){ ?>
                    <?php if($entity['media_url'] < 2){ ?>
                        <div class="media lurch">
                        </div>
                    <?php } ?>
                <?php } ?>
            <?php } ?>
            <div class="time"><?php echo twitter_time($value['created_at']); ?></div>
            <div class="action">
                <a href="https://twitter.com/intent/tweet?in_reply_to=<?php echo $value['id_str']; ?>">Reply</a>
                <a href="https://twitter.com/intent/retweet?tweet_id=<?php echo $value['id_str']; ?>">Retweet</a>
            </div>
        </li>
    <?php } ?>
</ul>
<!-- this simple but sexy button allows you to paginate through your tweets -->
<a href="<?php echo $multi_array['search_metadata']['next_results']; ?>">Next</a>

With some cool ajax requests you could do one of those nifty infinite loops. Hopefully this gives you all a base for some really cool implementations. Cheers!

Wicks Digital

Student Wolfpack Club

Sterling Whiteside Brand Presence

Dr. Anika Goodwin

South Harlow Interiors

South Harlow Home

DIASPORA Logo for X, The Moonshot Factory

Arden Key – Optimus Key

Zealografy

Rogers Solutions Group

UNCG Admissions Website

Helen Davis Design

KBS “Bless Up”

DA/CA 5

Infinitely Cool

Solar Cafe

Quick out the Blocks

Virtus Therapy

RSP Logo Concept

“皇族 熊猫” Royal Panda

Kingdom by Kubashi

Confidential Confessions

“Warbird” Concept

Weston Farms

Walter Latham Entertainment

2012 NC State Football

Eller Wedding Mock Movie Poster

Braxton Cosby

Kubashi

Triangle Futbol Club Alliance

Cam Bennerman “13” Brand Mark

NC State Football Media Guide

Boom Town Talk