読者です 読者をやめる 読者になる 読者になる

作業ログ(2010/09/24分)

自宅で作業

<?php
// oauth componentをincludeする
App::import('Vendor', 'oauth', array('file' => 'OAuth' . DS . 'oauth_consumer.php'));
class MainController extends AppController {
        var $name = 'Main';
        var $uses = null;
        var $helpers = array('Form');

        function index() {}

        public function twitter() {
                $consumer = $this->createConsumer();
                $requestToken = $consumer->getRequestToken('http://twitter.com/oauth/request_token', 'oauth認証後に戻ってくるcallbackurlを書く');
                $this->Session->write('twitter_request_token', $requestToken);
                $this->redirect('http://twitter.com/oauth/authorize?oauth_token=' . $requestToken->key);
        }

        public function twitter_callback() {
                $requestToken = $this->Session->read('twitter_request_token');
                $consumer = $this->createConsumer();
                $accessToken = $consumer->getAccessToken('http://twitter.com/oauth/access_token', $requestToken);

                $myposts = $consumer->get($accessToken->key,
                                           $accessToken->secret,
                                           'http://api.twitter.com/1/statuses/user_timeline.json',
                                           array('count' => 10));

                // 出力確認用
                foreach( json_decode($myposts) as $post ) {
                        echo $post->text . '<br />';
                        echo strftime('%Y/%m/%d %H:%M:%S', strtotime( (string)$post->created_at )) . '<br />';
                }
        }

        Private function createConsumer() {
                return new OAuth_Consumer('Consumer key', 'Consumer secret');
        }
}