PHP: visualizzare i tweet preferiti da Twitter
Possiamo utilizzare una nostra classe PHP per visualizzare i nostri tweet preferiti presi da Twitter.
PHP ci permette di estrarre con facilità molte informazioni da Twitter, come ad esempio i nostri tweet preferiti. In questo articolo vedremo come svolgere questo compito utilizzando una classe PHP creata per l’occasione.
La classe è la seguente:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
class FavoriteTweets
{
private $username;
public function __construct($username)
{
$this->username = $username;
}
private function _getTweets()
{
$tweets = array();
$tweets = json_decode($this->_fetchFeed());
if(!$tweets){
$tweets = array();
}
return $tweets;
}
public function generate ($limit=10, $className = 'tweet-fav-list')
{
echo "<ul class='$className'>";
$tweets = array_slice($this->_getTweets(),0,$limit);
foreach($tweets as $t){
$id = $t->id_str;
$text = self::_formatTweet($t->text);
$time = self::_relativeTime($t->created_at);
$username = $t->user->screen_name;
$retweets = $t->retweet_count;
?>
<li>
<p><?php echo $text ?></p>
<div class="info">
<a href="http://twitter.com/<?php echo $username ?>" class="user"
title="Go to <?php echo $username?>'s twitter page"><?php echo $username ?></a>
<?php if($retweets > 0):?>
<span class="retweet" title="Retweet Count"><?php echo $retweets ?></span>
<?php endif;?>
<a href="http://twitter.com/<?php echo $username,'/status/',$id?>"
class="date" title="Shared <?php echo $time?>"><?php echo $time?></a>
</div>
</li>
<?php
}
echo "</ul>";
}
private function _fetchFeed()
{
return file_get_contents("http://api.twitter.com/1/favorites/{$this->username}.json");
}
private static function _relativeTime($time)
{
$divisions = array(1,60,60,24,7,4.34,12);
$names = array('second','minute','hour','day','week','month','year');
$time = time() - strtotime($time);
$name = "";
if($time < 10){
return "just now";
}
for($i=0; $i<count($divisions); $i++){
if($time < $divisions[$i]) break;
$time = $time/$divisions[$i];
$name = $names[$i];
}
$time = round($time);
if($time != 1){
$name.= 's';
}
return "$time $name ago";
}
private static function _formatTweet($str)
{
$str = preg_replace(
'/((ftp|https?):\/\/([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?)/i',
'<a class="link" href="$1" target="_blank">$1</a>',
$str
);
$str = preg_replace(
'/(\s|^)@([\w\-]+)/',
'$1<a class="mention" href="http://twitter.com/#!/$2" target="_blank">@$2</a>',
$str
);
$str = preg_replace(
'/(\s|^)#([\w\-]+)/',
'$1<a class="hash" href="http://twitter.com/search?q=%23$2" target="_blank">#$2</a>',
$str
);
return $str;
}
} |
La classe estrae i dati dal feed JSON di Twitter e restituisce un output in HTML. È il metodo pubblico FavoriteTweets::generate($limit, $className) ad essere usato nella pagina di destinazione:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php
require_once('inc/FavoriteTweets.php');
$favoriteTweets = new FavoriteTweets('gabromanato');
?>
<!DOCTYPE html>
<!-- marcatura -->
<body>
<div id="favorite-tweets">
<?php $favoriteTweets->generate(5);?>
</div>
</body>
</html> |
Potete visionare l’esempio finale di seguito.
Demo
Visualizzare i tweet preferiti da Twitter

Nessun commento. Aggiungine uno!
I commenti sono chiusi.