【问题标题】:Cakephp display tweetCakephp 显示推文
【发布时间】:2014-02-25 17:53:06
【问题描述】:

现在如何使用新的 Twitter API 在 CakePHP 元素中显示来自我的 Twitter 页面的 5 条推文? 有什么插件吗?抱歉,如果这是一个愚蠢的问题,但我之前使用过 Twitter API。

【问题讨论】:

    标签: cakephp twitter


    【解决方案1】:

    如果您有兴趣,我已经创建了一个可以轻松完成此操作的插件:https://github.com/voycey/CakePHP-Twitter-API-v1.1-Full

    【讨论】:

      【解决方案2】:

      我不知道有一个可与新 API 配合使用的 CakePHP 插件,但有一个非常易于使用的简单 PHP 库 - https://github.com/jublonet/codebird-php

      您需要在 twitter 中创建一个应用程序,以获取您的使用者密钥和令牌。

      这里有一些相关的 sn-ps 代码,我不久前用来让它与 CakePHP 应用程序一起工作:

      app/Config/bootstrap.php 中(您需要缓存推文,而不是每次请求都获取它们)

      Cache::config('three_hour_config', array(
          'engine' => 'File', //[required]
          'duration'=> '+3 hours', //[optional]
          'probability'=> 100, //[optional]
          'mask' => 0777, // [optional] permission mask to use when creating cache files
      ));
      

      在您的控制器中 - 可能是 AppController.php:

      private function __setLatestTweets() {
          $rawTweets = Cache::read('rawTweets', 'three_hour_config' );
          if (empty($rawTweets)) {
      
              // from https://github.com/jublonet/codebird-php
              require_once (APP . 'Vendor' . DS . 'jublonet' . DS . 'codebird-php' . DS . 'src' . DS . 'codebird.php');
              \Codebird\Codebird::setConsumerKey('********', '*********');
              $cb = \Codebird\Codebird::getInstance();
              $cb->setToken('*****-**********', '*********');
      
              $rawTweets = (array)$cb->statuses_userTimeline(array('count' => 1));
      
              // remove the last key, which is not numeric.
              // If we don't remove it, it's treated like a normal tweet and creates errors
              unset($rawTweets['httpstatus']);
      
              Cache::write('rawTweets', $rawTweets, 'three_hours');
          }
          $this->set('rawTweets',$rawTweets);
      }
      

      然后我写了一个助手来帮助我格式化它们:/app/View/Helper/TwitterHelper.php

      <?php
      /* /app/View/Helper/TwitterHelper.php */
      App::uses('AppHelper', 'View/Helper');
      
      class TwitterHelper extends AppHelper {
      
          public $helpers = array('Time');
      
          public function formatTweets($tweets) {
              $latestTweets = '';
      
              foreach ($tweets as $tweet) {
                  if ($tweet->retweeted) {
                      $tweetText = $tweet->retweeted_status->text;
                  } else {
                      $tweetText = $tweet->text;
                  }
                  // Convert twitter Usernames and links to Hyperlinks
                  $tweetcontent = '<div class="tweet_content">' . $this->linkify($tweetText) . '</div>';
                  $tweetDate = strtotime($tweet->created_at);
                  $singleTweet = $tweetcontent . '<div class="tweet_footer">' .
                              $this->Time->timeAgoInWords( $tweetDate ) .
                              ' &sdot; ' .
                              '<a href="https://twitter.com/intent/tweet?in_reply_to=' . number_format($tweet->id, 0, '', '') . '" class="twtr_reply" target="_blank">reply</a>' .
                              ' &sdot; ' .
                              '<a href="https://twitter.com/intent/retweet?tweet_id=' . number_format($tweet->id, 0, '', '') . '" class="twtr-rt" target="_blank">retweet</a>' .
                              ' &sdot; ' .
                              '<a href="https://twitter.com/intent/favorite?tweet_id=' . number_format($tweet->id, 0, '', '') . '" class="twtr-fav" target="_blank">favorite</a>' .
                              '</div>';
                  $latestTweets .= "<div class=\"single_tweet\"> $singleTweet </div>";
              }
      
              return $latestTweets;
          }
      
      /**
       * Credit Jeremy Parrish http://rrish.org/
       */
          public function linkify($statusText) {
              // linkify URLs
              $statusText = preg_replace(
                  '/(https?:\/\/\S+)/',
                  '<a target="_blank" href="\1">\1</a>',
                  $statusText
              );
      
              // linkify twitter users
              $statusText = preg_replace(
                  '/(^|\s)@(\w+)/',
                  '\1@<a target="_blank" href="http://twitter.com/\2">\2</a>',
                  $statusText
              );
      
              // linkify tags
              $statusText = preg_replace(
                  '/(^|\s)#(\w+)/',
                  '\1#<a target="_blank" href="http://search.twitter.com/search?q=%23\2">\2</a>',
                  $statusText
              );
      
              return $statusText;
          }
      
      }
      

      然后在你看来,你只会有类似的东西:

      <h4>Latest from Twitter</h4>
      <div class="tweets_container">
          <?php echo $this->Twitter->formatTweets($rawTweets); ?>
      </div>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-22
        • 2012-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多