Twitter Feeds widget

//USAGE - set the keys and token here and then just include this file into your code. Change the <li> to whatever needed. 

if(!isset($tweetAuthen) && $tweetAuthen != "awre4rwrwfe") //random string to prevent this file being run directly
{
    echo "<li>Unauthorized use of widget</li>!";
}
else
{
        //Files needed for the Twitter authentification
    //Check if TwitterOAuth doesn't already existe
    if( ! class_exists( 'TwitterOAuth' )){
    
        require_once 'twitteroauth/twitteroauth.php';
    
    }
    
    
    
   function twitter_feeds(){

    $return_value = false;
    
    $twitter_oauth_var = array('consumer_key'=>"YOUR CONSUMER KEY", 'consumer_secret'=>"YOUR CONSUMER SECRET", "token_key"=>"YOUR TOKEN KEY", 'token_secret'=>"YOUR TOKEN SECRET");
    //Check if wee use the authentification methode. We need to have all the key and secret.
    if( is_array( $twitter_oauth_var ) && count($twitter_oauth_var) == 4 ){
       
        $connection = new TwitterOAuth($twitter_oauth_var['consumer_key'], $twitter_oauth_var['consumer_secret'], $twitter_oauth_var['token_key'],$twitter_oauth_var['token_secret']);
        $last_tweet = $connection->get('https://api.twitter.com/1.1/statuses/user_timeline.json', array("count"=>"5") );  // change 5 to the number of tweets to retrieve
                
        $return_value = $last_tweet;
        
        foreach ($last_tweet as $tweet) 
        {

            $id = $tweet->id_str;

            //$options['id'] = $id;
            //$last_tweet_html = $connection->get('https://api.twitter.com/1.1/statuses/oembed.json', $options);
            
            //$tweet_id = $id;
            //$tweet_html .= $last_tweet_html->html;
            
            $temp =  $tweet->text;
            if(strlen($tweet->entities->urls[0]->url))
            {
                $temp = str_ireplace($tweet->entities->urls[0]->url,"<a href='".$tweet->entities->urls[0]->url."' target='_blank'>".$tweet->entities->urls[0]->url."</a>",$temp);
                $tweet_html .= "<li>".$temp."</li>";
            }
            else
                $tweet_html .= "<li>".$temp."</li>";
        }

    }
        return $tweet_html;
    }
    
    echo twitter_feeds();
}

Need to include twitteroauth library. Can be downloaded from here https://github.com/abraham/twitteroauth

The whole code including the twitteroauth library can be downloaded from here.

Leave a Reply