php Twitter小工具

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Twitter小工具相关的知识,希望对你有一定的参考价值。

<?php
  require_once (TEMPLATEPATH."/inc/twitteroauth/twitteroauth.php");
  require_once (TEMPLATEPATH."/inc/twitteroauth/OAuth.php");

  /* Create a TwitterOauth object with consumer/user tokens. */
  $connection = new TwitterOAuth('0sEgwod5t1o6u2KpidvaodQwO', 'hlHUFNBsVcgxB5skHIxX8nD44a6oroQTbH1okEtI6yOt8ui08Q', '127297630-OvTWe2SGCM6DhwXPfjehwm3y2jA4XMf4xhAorMoW', 'EGtanzwDLiAbRrsQw41RtL9Gi5pkTPtmHLmiLo6NH8Rse');

  $latest_tweet = $connection->get('statuses/user_timeline', array('count' => 1, 'screen_name' => 'alley_kat_beer'));
  $latest_tweet = $latest_tweet[0];

  $auth_name = $latest_tweet->user->name;
  $auth_user = "@".$latest_tweet->user->screen_name;
  $auth_user = preg_replace("/@([A-Za-z0-9\/.]*)/", "<a href=\"http://www.twitter.com/$1\">@$1</a>", $auth_user);
  $time_stamp = $latest_tweet->created_at;

  $image = $latest_tweet->user->profile_image_url;

  $image = str_replace("normal", "bigger", $image);

  $text = $latest_tweet->text;

  $tweet_arr = (array) $latest_tweet;

  parse_message($tweet_arr);

  function parse_message( &$tweet ) {
    if ( !empty($tweet['entities']) ) {
      $replace_index = array();
      $append = array();
      $text = $tweet['text'];
      foreach ($tweet['entities'] as $area => $items) {
        $prefix = false;
        $display = false;
        switch ( $area ) {
          case 'hashtags':
            $find   = 'text';
            $prefix = '#';
            $url    = 'https://twitter.com/search/?src=hash&q=%23';
            break;
          case 'user_mentions':
            $find   = 'screen_name';
            $prefix = '@';
            $url    = 'https://twitter.com/';
            break;
          case 'media':
            $display = 'media_url_https';
            $href    = 'media_url_https';
            $size    = 'small';
            break;
          case 'urls':
            $find    = 'url';
            $display = 'display_url';
            $url     = "expanded_url";
            break;
          default: break;
        }
        foreach ($items as $item) {
          if ( $area == 'media' ) {
            // We can display images at the end of the tweet but sizing needs to added all the way to the top.
            // $append[$item->$display] = "<img src=\"{$item->$href}:$size\" />";
          }else{
            $msg     = $display ? $prefix.$item->$display : $prefix.$item->$find;
            $replace = $prefix.$item->$find;
            $href    = isset($item->$url) ? $item->$url : $url;
            if (!(strpos($href, 'http') === 0)) $href = "http://".$href;
            if ( $prefix ) $href .= $item->$find;
            $with = "<a target=\"_blank\" href=\"$href\">$msg</a>";
            $replace_index[$replace] = $with;
          }
        }
      }
      foreach ($replace_index as $replace => $with) $tweet['text'] = str_replace($replace,$with,$tweet['text']);
      foreach ($append as $add) $tweet['text'] .= $add;
    }
  }

  $text = $tweet_arr['text'];

  // Convert timestamp to nice date - 2 hours ago, etc.
  function nicetime($date)
  {
      if(empty($date)) {
          return "No date provided";
      }
   
      $periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
      $lengths         = array("60","60","24","7","4.35","12","10");
   
      $now             = time();
      $unix_date         = strtotime($date);
   
         // check validity of date
      if(empty($unix_date)) {    
          return "Bad date";
      }
   
      // is it future date or past date
      if($now > $unix_date) {    
          $difference     = $now - $unix_date;
          $tense         = "ago";
   
      } else {
          $difference     = $unix_date - $now;
          $tense         = "from now";
      }
   
      for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
          $difference /= $lengths[$j];
      }
   
      $difference = round($difference);
   
      if($difference != 1) {
          $periods[$j].= "s";
      }
   
      return "$difference $periods[$j] {$tense}";
  }
   
  $result = nicetime($time_stamp); // 2 days ago
   
?>
<div class="recent-tweet">
  <div class="tweet-header">
    <span class="banner">Latest Tweet</span>
    <br>
    <i class="fa fa-2x fa-twitter"></i>
  </div>
  <p class="tweet-content">
    <?php echo $text; ?>
  </p>
  <p class="tweet-footer">
    Posted <?php echo $result; ?> // <a class="read-more" href="//twitter.com/alley_kat_beer">Follow Us</a>
  </p>
</div>

以上是关于php Twitter小工具的主要内容,如果未能解决你的问题,请参考以下文章

Twitter好友小工具v2.0-jQuery插件

PHP - Twitter API - 使用空间时请求损坏

Twitter Bootstrap Twipsy - 悬停在工具提示上方

我在Twitter API上做错了什么?

如何在 php 和 javascript 中创建小部件?

用于动态小部件更新的 Twitter 小部件方法