How To Custom Twitter Widget Using PHP

How To Custom Twitter Widget Using PHP

How To Custom Twitter Widget Using PHP

0 Sales


Twitter Embedded Timelines offers an easy way to display the latest tweets from the website's Twitter account. By copying and pasting the embed code from the creation section of Twitter widgets, you can add the Twitter feed widget to the web page. But there is a limitation to the website design for styling the Twitter feed list. Generally, the website layout does not match the embedded timelines widget theme. You need to configure it if you want to list the twitter feeds as per your website user interface.


Custom Twitter feed lists the latest Twitter account tweets and customizes the feed style according to your website user interface. You need to call the Twitter API to create a custom Twitter feed. In this tutorial, we'll show you how to get Twitter tweets and create a custom Twitter feed widget using Twitter API and PHP.


For create custom Twitter feeds using PHP, the following features will be implemented.


  • Use the TwitterOAuth PHP library to authenticate with Twitter.

  • Retrieve app history Twitter feeds.

  • Display the website's tweets or feed.

Create Twitter App 


You need to create a Twitter App and generate the API key & password that you need to define on the API call to access the Twitter API. If you haven't created a Twitter app before, follow this step-by-step guide to generate access tokens and API keys


Go to the Keys and Access Tokens page once you have built your Twitter App. Such information – User Key (API Key), Consumer Secret (API Secret), Access Token, and Access Token Secret – will be available on this Application Settings page. Note the credentials in the script for future use.


TwitterOAuth PHP Library 


Using PHP, the TwitterOAuth library is used to authenticate with the Twitter API and get tweets from the account of the client.

Custom Twitter Feed using PHP

We will collect the latest tweets from the Twitter account with user information and view tweets in the custom feed widget in the example software.


  • User Data Retrieve Twitter Feed: 

  • Include the class directory of the TwitterOAuth library.

  • Specify the Twitter App Settings with the User Password, Consumer Code, Access Token, and Access Token Secret.

  • Specify the username of the Twitter account.

  • Specify the number of tweets from the API that you want to fetch.

  • Initialize the class of TwitterOAuth.

  • Use the user timeline API to get the Twitter feeds.



// Path to TwitterOAuth library



 * Twitter App Settings

 * Set access tokens and API keys


$consumerKey       = "YourTwitterAppConsumerKey";

$consumerSecret    = "YourTwitterAppConsumerSecret";

$accessToken       = "YourTwitterAppAccessToken";

$accessTokenSecret = "YourTwitterAppAccessTokenSecret";

// Twitter account username

$twitterID = 'codexworldblog';

// Number of tweets

$tweetNum = 10;


// Authenticate with twitter

$twitterConnection = new TwitterOAuth(






// Get the user timeline feeds

$feedData = $twitterConnection->get(



        'screen_name'     => $twitterID,

        'count'           => $tweetNum,

        'exclude_replies' => false





Display User Information:


You will get all the information about the tweet author using user object of $feedData.



    // Get user info

    $profilePic = str_replace("normal", "400x400", $feedData[0]->user->profile_image_url_https);

    $userName = $feedData[0]->user->name;

    $userScreenName = $feedData[0]->user->screen_name;

    $tweetsNum = $feedData[0]->user->statuses_count;

    $followerNum = $feedData[0]->user->followers_count;


<div class="user-info">

    <img src="<?php echo $profilePic; ?>" class="img-thumbnail" />

    <h2><?php echo $userName; ?></h2>

    <a href="<?php echo $userScreenName; ?>" target="_blank">@<?php echo $userScreenName; ?></a>


<div class="tweet-info">

    <div class="fnum"><div>Tweets</div><div class="badge"><?php echo $tweetsNum; ?></div></div>

    <div class="fnum"><div>Followers</div><div class="badge"><?php echo $followerNum; ?></div></div>


Display Tweets / Feeds / Posts:

The following code list the latest tweets data with custom HTML element in the web page.

<div class="tweet-box">

    <h2>Latest Tweets</h2>

    <div class="tweets-widget">            

        <ul class="tweet-list">


        foreach($feedData as $tweet){

            $latestTweet = $tweet->text;

            $latestTweet = preg_replace('/https:\/\/([a-z0-9_\.\-\+\&\!\#\~\/\,]+)/i', '<a href="https://$1" target="_blank">https://$1</a>', $latestTweet);

            $latestTweet = preg_replace('/@([a-z0-9_]+)/i', '<a class="tweet-author" href="$1" target="_blank">@$1</a>', $latestTweet);

            $tweetTime = date("D M d H:i:s",strtotime($tweet->created_at));


            <li class="tweet-wrapper">

                <div class="tweet-thumb">

                    <span><a href="<?php echo $tweet->user->url; ?>" title="<?php echo $tweet->user->name; ?>"><img alt="" src="<?php echo $tweet->user->profile_image_url; ?>"></a></span>


                <div class="tweet-content">

                    <h3 class="title" title="<?php echo $tweet->text; ?>"><?php echo $latestTweet; ?></h3>

                    <span class="meta"><?php echo $tweetTime; ?> - <?php echo $tweet->favorite_count; ?> Favorite</span>



        <?php } ?>





Information Returned by Twitter API

After the autentication, the Twitter API returned various information about the user and tweets. The following informations are some of the commonly used in Twitter Feed.

  • User Information (user)

    • User ID (id)

    • User Name (name)

    • User Screen Name (screen_name)

    • User Location (location)

    • Followers Count (followers_count)

    • Friends Count (friends_count)

    • Tweets Count (statuses_count)

    • Profile Background Image (profile_background_image_url | profile_background_image_url_https)

    • Profile Image (profile_image_url | profile_image_url_https)

    • Profile Banner (profile_banner_url)

  • Tweet Post Date (created_at)

  • Tweet ID (id)

  • Tweet Text (text)

  • URLs (urls)

  • Geo Data (geo)

  • Retweet Count (retweet_count)

  • Favorite Count (favorite_count)


You can use it for personal or commercial projects. You can't resell it partially or in this form.


Create Date : May 14, 2020

Updated Date : May 14, 2020


Comments : 0

Downloads : 0