2014-01-29 2 views
1

새로운 트위터 API로 CakePHP 요소의 트위터 페이지에서 5 개의 트윗을 어떻게 표시 할 수 있습니까? 플러그인이 있습니까? 죄송합니다.이 바보 같은 질문이 있지만 전에 트위터 API와 함께 일한 적이 있습니다.Cakephp 디스플레이 짹짹

답변

0

나는 새로운 API와 함께 작동하는 CakePHP의 플러그인을 인식 아니지만, 작업 꽤 쉽게 멋진 일반 PHP 라이브러리가 - 당신은 트위터에서 응용 프로그램을 만들 필요가 https://github.com/jublonet/codebird-php

는에, 귀하의 소비자 키와 토큰을 받으십시오.

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 
)); 
(당신이 그 (것)들에게 모든 요청을하지, 트윗을 캐시 할 수 있습니다) : CakePHP의 얼마 전에 앱

여기에 내가 작업 얻을하는 데 사용되는 코드의 몇 가지 관련 조각의 컨트롤러에서

- 어쩌면 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+)/', 
      '\[email protected]<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>