ALthough at the time of writing this, vendors have been deprecated in cakephp, I’m going to write this article because I want to start doing more useful tutorials instead of just school work.
This tutorial will only work for cakephp version 1.3.
Today I did some work. This work was basically “Help my twitter feed has been broken for months”. Never fear, Jimmy is here. To save the day.
This problem happened because Twitter decided to remove old functionality with their Twitter feeds like RSS and atom feeds. This is good because “Bring on the future!” but also bad because a lot of javascript libraries used these old methods and now they are broken. Like the one I was using on this website.
To fix the issue I had to comb the interwebsites to finds me some tutorialses. One in particular was pretty useful and showed how to grab the twitter feed, which is all I needed to do. The process is as follows:
- Log into https://dev.twitter.com/apps/
- Create a new app
- Create an access token
- Leave this page open so you can copy about 4 damn codes along to your program
- Search for the twitteroauth code on github and download it.
- In the zip, grab the twitteroauth folder with twitteroauth.php and oauth.php
- Paste this folder in cakephp’s app/vendors folder
MEANWHILE, on your web server (or localhost), you’re editing a cakephp CONTROLLER file. In this case, because the twitter feed was on every page, I was editing the app_controller.php.
In your controller, you need the following:
// Load latest Twitter data from cache $tweets = $this->Session->read('Twitter.latest'); if(empty($tweets)) { // Path to twitteroauth library App::import('vendor', 'twitteroauth/twitteroauth'); // vars for use later $twitteruser = 'username'; $notweets = 3; $consumerkey = 'xvsdfsdfsd'; $consumersecret = 'sdfsadfsadf'; $accesstoken = 'sdfsadf'; $accesstokensecret = 'sdfsdaf'; // function creates new connection function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) { $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret); return $connection; } // get connection using the above function $connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret); // request tweets json $tweets = $connection->get('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=' . $twitteruser . '&count=' . $notweets); // add into session cache $this->Session->write('Twitter.latest', $tweets); } $this->set('tweets', $tweets);
And there. Look through the code. The comments tell you what it is doing. Basically make sure you paste the codes and usernames into the fields, The data there is example data.
The reason why I’ve added that session stuff is because the twitter call from the website is slow as balls and if I had to wait 5 – 10 seconds on every page load I’d probably shoot myself. This basically stores the tweet data in the session because it’s quicker that way. The user will now only experience an initial slow page load but no slow-down after that.
Then, in your view, you need something like:
$twitter_output = ''; if(!empty($tweets)) { $username = 'twitter_username'; $html->tag('h4', 'Latest Tweets'); for($i = 0; $i < 3; $i++) { $twitter_output .= $html->tag('p', $html->link($username, 'http://twitter.com/' . $username, array('target' => 'blank')) . ': ' . $tweets[$i]->text); } }
The above may or may not need some filtering on that text, for example: links won’t appear as links but just HTML. Twitter converts these on its own.
In new versions of cakephp, it is recommended that you create a plugin or a type of package for this sort of thing rather than vendors. Vendors are lazy and… I don’t know. But if you need a quick fix for a client, then this is your best option.
There you have it, Twitter API 1.1 on cakephp 1.3.
Leave comments if you have any better ways of doing things.