How do I get the last tweet?

Asked

Viewed 611 times

0

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

  • You are not using any API?

  • no, I was trying with this function: Function Ultimotweet($user){ $url = "http://twitter.com/statuses/user_timeline/$usuario.xml? Count=1"; $xml = simplexml_load_file($url) or die("Error connecting."); foreach($xml->status as $status){ $ultimo = $status->text; } echo $ultimo; }

  • 1

    If you want there’s a link that can help you http://www.webdevdoor.com/javascript-ajax/custom-twitter-feed-integration-jquery/ but it’s in English.

  • Thanks Paulo Roberto, I’ll be here.

1 answer

7


Use any PHP library that supports the Tweeter API:

https://dev.twitter.com/docs/twitter-libraries#php

Below is an example using the Abraham Williams' Twitteroauth class:

# 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: http://www.aljtmedia.com/blog/displaying-latest-tweets-using-the-twitter-api-v11-in-php

To use this library, you need to register an application on Twitter to get the key and access token: https://apps.twitter.com/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.