Question

I found some code that allows me to connect to Twitter using OAuth so that I could post tweets. This code is actually about changing the Avatar but I changed it to just tweet: http://net.tutsplus.com/tutorials/php/creating-a-twitter-oauth-application/

I got it working fine but the OAuth seems to be stored in a SESSION so as soon as I close the browser it disconnects... I want it to be permanently connected so I can post from a Cron Job.

How do I go about doing this?

Was it helpful?

Solution

     <?php
      // use abrahams oauth library and create your app at dev.twitter.com
     $message= 'my tweet text';

     define("CONSUMER_KEY", "xxxx");
     define("CONSUMER_SECRET", "xxxx");
     define("OAUTH_TOKEN", "xxxx");
     define("OAUTH_SECRET", "xxxx");
     $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);
     $connection->get('account/verify_credentials');
     $connection->post('statuses/update',array('status' => " $message "));

     ?>

OTHER TIPS

The easiest way would be to authenticate the application and save the oauth_token and oauth_token_secret for your account. Then when you run the script, set those values into the $_SESSION so that the library can use them.

This probably sounds a little roundabout, and it is. I would use a library that has built in support to manually set your access token. I always suggest using Abraham's Twitter OAuth Library for PHP: https://github.com/abraham/twitteroauth It has good documentation and works well.

I would suggest using your OAuth callback to store the token and secret in a database table, and then pull them from the db instead of trying to use the session var. I use Twitter in one of my apps, and have it set in a way where I only have to allow access to the Twitter app one time. Don't know your exact needs here, but I can't post Tweets to my account without having to re-authenticate when the session expires.

I second GregSchoen as well, Abraham's Twitter OAuth Library for PHP is very easy to work with.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top