插件58:接收tweet消息

<?php // Plug-in 58: Get Tweets
/*
 * 插件说明:
 * 插件从一个公开的Twitter用户中读取最后20条Tweet消息。
 * 接受一个Twitter账户的用户名,如果它是公开的,则返回它最近的Tweet消息。
 * 访问成功,则返回一个两元素的数组,其中第一个元素表示Tweet消息的个数,第二个元素包含一个数组,保存每个Tweet消息。
 * 访问失败,则返回只有一个元素的数组,这个元素的值为FALSE。它需要以下参数:
 * $user Twitter用户名。
 */
// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link

$user   = 'eminem';
$result = PIPHP_GetTweets($user);

echo "Viewing '$user':<br /><ul>";

if (!$result[0]) echo 'Failed';
else
   for ($j = 0 ; $j < $result[0] ; ++$j)
      echo "<li>" . $result[1][$j] . "</li>";

function PIPHP_GetTweets($user)
{
   // Plug-in 58: Get Tweets
   //
   // This plug-in returns the most recent 20 tweets of a Twitter
   // user. The argument required is:
   //
   //    $user: Twitter username
   //
   // Upon success the plug-in returns a two element array, the
   // first of which contains the number of tweets returned, and
   // the second is an array of the tweets. On failure a single
   // element array is returned with the value FALSE.

   $url  = "http://twitter.com/statuses/user_timeline/$user.xml";
   $file = @file_get_contents($url);
   if (!strlen($file)) return array(FALSE);
   
   $xml  = @simplexml_load_string($file);
   if ($xml == FALSE) return array(FALSE);
   
   $tweets = array();

   foreach ($xml->status as $tweet)
   {
      $timestamp = strtotime($tweet->created_at);
      $tweets[] = "(" . date("M jS, g:ia", $timestamp) . ") " .
         $tweet->text;
   }

   return array(count($tweets), $tweets);
}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值