Milos Sretin
Milos Sretin

Reputation: 1748

Cakephp display tweet

How can I display 5 tweets from my Twitter page in CakePHP element now with new Twitter API? Is there some plugin for that? Sorry if this is a stupid question but I newer worked with Twitter API before.

Upvotes: 1

Views: 738

Answers (2)

the1dv
the1dv

Reputation: 931

I have created a plugin that does this easily if you are interested: https://github.com/voycey/CakePHP-Twitter-API-v1.1-Full

Upvotes: 1

joshua.paling
joshua.paling

Reputation: 13952

I'm not aware of a CakePHP plugin that works with the new API, but there is a nice plain PHP library that's pretty easy to work with - https://github.com/jublonet/codebird-php

You need to create an app within twitter, to get your consumer key and token.

Here's some relevant snippets of code I used to get it working with a CakePHP app a while ago:

In app/Config/bootstrap.php (you'll want to cache tweets, not get them every request)

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
));

In your controller - maybe 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);
}

And then I wrote a helper to help me format them: /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;
    }

}

And then in your view you'd just have something like:

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

Upvotes: 0

Related Questions