After a lot of puzzling with a connection to twitter to put a post on my wall, I thought of writing it down so that I never have to puzzle with this again!
After making a Twitter account and an application on http://twitter.com/apps/.
You need 2 actions in you Controller. The first one that you call asks for allowing the connection:
The second action is the callback, that reads the token that you get when you allow the twitter access. This token we will save in a database for further usage.
In my example I have an IndexController with 2 actions named: indexAction() and callbackAction().
The url of the page is “my_url.com” and my callback url is “my_url.com/index/callback”.
You need to specify this in your twitter app settings. You go to your app that you can find on http://twitter.com/apps/ and change the settings with your url and your callback url.
Now we can start in our Zend Framework.
First we make our indexAction():
What you need is your consumerKey and your consumerSecret that you find in your twitter application details
public function indexAction()
{
session_start();
$config = array(
'callbackUrl' => 'http://my_url.com/index/callback',
'siteUrl' => 'http://api.twitter.com/oauth',
'consumerKey' => 'CONSUMER_KEY',
'consumerSecret' => 'CONSUMER_SECRET'
);
$consumer = new Zend_Oauth_Consumer($config);
$token = $consumer->getRequestToken();
$_SESSION['BLUB'] = serialize($token);
$consumer->redirect();
}
What will happen is that you will be redirected to the allow twitter connection page and then he will go to your second action the callbackAction.
callbackAction():
public function doneAction(){
session_start();
$config = array(
'callbackUrl' => 'http://my_url.com/index/callback',
'siteUrl' => 'http://api.twitter.com/oauth',
'consumerKey' => 'CONSUMER_KEY',
'consumerSecret' => 'CONSUMER_SECRET'
);
$consumer = new Zend_Oauth_Consumer($config);
if (!empty($_GET) && isset($_SESSION['BLUB'])) { $token = $consumer->getAccessToken($_GET,unserialize($_SESSION['BLUB']));
$_SESSION['TWITTER_ACCESS_TOKEN'] = serialize($token);
//SAVE TOKEN TO DB $table = new Default_Model_DbTable_Twitter(); //TABLE CLASS $data = array(
'token' => $_SESSION['TWITTER_ACCESS_TOKEN']
);
$where = $table->getAdapter()->quoteInto('id = ?', 1);
$table->update($data, $where);
// Now that we have an Access Token, we can discard the Request Token
$_SESSION['BLUB'] = null;
}
}
Now we have the token and put for examples tweets on your twitter by the following code:
//Get token from DB
$twitterMap = new Default_Model_TwitterMapper();
$token = unserialize($twitterMap->getToken());
$twitter = new Zend_Service_Twitter(array(
'username' => 'MY_TWITTER_ACOUNT',
'accessToken' => $token
));
// verify user's credentials with Twitter
$response = $twitter->status->update('Hello World');
And that’s is it to make a connection with your twitter using the Zend Framework by using the twitter service and the Oauth.
I have also implemented this. Do you know if there is a way to bypass the Twitter allow connection page, or to automatically allow your app, without having to login into twitter and click allow?
You can’t bypass this, but you only have to do this once. Once you have your token saved in your database, you can re-use it. So you only have to allow Twitter the first time.
Hey Lisa,
thanks for your post about connecting with zend_oauth.
But I still don’t understand something. How does a user get his access_token back from the database? What does your function getToken() do? It looks like your script is just for one account, isn’t it? Is saving the access_token at the database only possible for one account?
Lets say, a user logs in with Oauth, he confirms the twitterpage, comes back to my site and the new access_token will be saved in my database (e.g. table: twitter, rows: id, access_token). The user can use my twitterservice now.
But how will the user get his access_token back, if he comes again (after a day)? thats the point, I don’t understand. I hope you can understand me and help