Simple php function to get recent tweets, tutorial and example to get recent tweets easily, get recent tweets with cache time in php.

How To Get Recent Tweets

Using PHP cURL and Twitter API, we will get recent tweets easily and with cache time, we will use Qassim_HTTP() function.

Make Twitter App

Firstly go to twitter api and make your app, read how to make twitter app.

Qassim_HTTP() Function

We will use Qassim_HTTP() function:

/* By Qassim Hassan, wp-time.com */
function Qassim_HTTP($method, $url, $header, $data){
    if( $method == 1 ){
        $method_type = 1; // 1 = POST
    }else{
        $method_type = 0; // 0 = GET
    }

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_HEADER, 0);

    if( $header !== 0 ){
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    }

    curl_setopt($curl, CURLOPT_POST, $method_type);

    if( $data !== 0 ){
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }

    $response = curl_exec($curl);
    $json = json_decode($response, true);
    curl_close($curl);

    return $json;
}

Make Recent Tweets Function

Now using Qassim_HTTP() function, we will make new function “Qassim_Get_Recent_Tweets()” to get access token and after that we will get recent tweets.

/* By Qassim Hassan, wp-time.com */
function Qassim_Get_Recent_Tweets($consumer_key, $consumer_secret, $user_name, $count, $cache, $session_tweets, $session_cache){
    $base64 = base64_encode("$consumer_key:$consumer_secret");
    $header = array(
        "Authorization: Basic $base64",
        "Content-Type: application/x-www-form-urlencoded;charset=UTF-8",
    ); // Header array

    $data = http_build_query( array("grant_type" => "client_credentials") ); // If no data, enter 0, like this: $data = 0;
    $url = 'https://api.twitter.com/oauth2/token';
    $method = 1; // Method type: 1 = POST, 0 = GET.
    $get_access_token = Qassim_HTTP($method, $url, $header, $data);
    $access_token = $get_access_token['access_token']; // Your access token!

    $Header = array("Authorization: Bearer $access_token"); // Header with our access token.
    $Data = 0;
    $Method = 0;
    $username = $user_name;
    $tweets_count = $count;
    $Url = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=$username&count=$tweets_count";
    $result = Qassim_HTTP($Method, $Url, $Header, $Data);

    if( isset($_SESSION["$session_cache"]) and time() > $_SESSION["$session_cache"] ){
        unset($_SESSION["$session_tweets"]);
        unset($_SESSION["$session_cache"]);
    }

    if( !isset($_SESSION["$session_tweets"]) ){
        $_SESSION["$session_tweets"] = $result;
        $get_tweets = $_SESSION["$session_tweets"];
        $_SESSION["$session_cache"] = time() + $cache;
    }
    else{
        $get_tweets = $_SESSION["$session_tweets"];
    }

    return $get_tweets;
}

Get Recent Tweets

Now we will display recent tweets (read the comments next to the variables):

/* By Qassim Hassan, wp-time.com */

$consumer_key = "xxxx"; // enter your consumer key
$consumer_secret = "xxxx"; // enter your consumer secret

$username = "QQQHZ"; // enter your username without @
$count = 2; // count of tweets

$cache_time = 3600; // your recent tweets will be refreshed every 1 hour, 3600 seconds = 1 hour
$session_tweets = "my_recent_tweets"; // enter your tweets session name without space
$session_cache = "my_cache_time"; // enter your cache session name without space

if( isset($_SESSION["$session_tweets"]) and isset($_SESSION["$session_cache"]) and time() < $_SESSION["$session_cache"] ){
    $tweets = $_SESSION["$session_tweets"];
}else{
    $tweets = Qassim_Get_Recent_Tweets($consumer_key, $consumer_secret, $username, $count, $cache_time, $session_tweets, $session_cache);
}

$tweets_list = "";
foreach ( $tweets as $tweet ) {
    $tweets_list .= '<li>'.$tweet['text'].'</li>';
}

// Display Recent Tweets
echo "<ul>";
echo $tweets_list;
echo "</ul>";

Download

Download the previous example if you did not understand the tutorial.