How to get the last tweet?

0

I need to implement a function in my project to get the last tweet, however all unsuccessful attempts, could someone give me a function to display the last tweet?

    
asked by anonymous 12.02.2014 / 16:12

1 answer

7

Use any PHP library that supports the Tweeter API:

link

Here's an example using the Abraham Williams TwitterOAuth :

# Carregar a classe do Twitter API
require_once('TwitterOAuth.php');

# Definir constantes
define('TWEET_LIMIT', 5);
define('TWITTER_USERNAME', 'YOUR_TWITTER');
define('CONSUMER_KEY', 'YOUR_KEY');
define('CONSUMER_SECRET', 'YOUR_SECRET_KEY');
define('ACCESS_TOKEN', 'YOUR_TOKEN');
define('ACCESS_TOKEN_SECRET', 'YOUR_TOKEN_SECRET');

# Criar conexão
$twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);

# Usar SSL/TLS
$twitter->ssl_verifypeer = true;

# Carregar os Tweets
$tweets = $twitter->get('statuses/user_timeline', array('screen_name' => TWITTER_USERNAME, 'exclude_replies' => 'true', 'include_rts' => 'false', 'count' => TWEET_LIMIT));

# Exemplo de saída
if(!empty($tweets)) {
    foreach($tweets as $tweet) {

        # Acessando como um objeto
        $tweetText = $tweet->text;

        # Tornando links ativos
        $tweetText = preg_replace("/(http://|(www.))(([^s<]{4,68})[^s<]*)/", '<a href="http://$2$3" target="_blank">$1$2$4</a>', $tweetText);

        # "Linkificar" menções a usuários
        $tweetText = preg_replace("/@(w+)/", '<a href="http://www.twitter.com/$1" target="_blank">@$1</a>', $tweetText);

        # "Linkificar" tags
        $tweetText = preg_replace("/#(w+)/", '<a href="http://search.twitter.com/search?q=$1" target="_blank">#$1</a>', $tweetText);

        # Enviar saída
        echo $tweetText;

    }
}

Source: link

To use this library, you need to register an application on Twitter for the access key and token: link

    
12.02.2014 / 16:36