Auto tweet with OAuth using PHP and Cron job

Recent I have developed an auto tweet application using twitter OAuth and PHP and running this application in a particular interval of time using Cpanel Cron job.Here I will explain you how to make your auto tweet application.

Auto tweet with OAuth using PHP and Cron job

First register your new application in twitter. Click here to register a new twitter application and follow the steps. After successful registration you will get permission to access your twitter account using OAuth. Same time your OAuth key and consumer key etc.. will be provided by twitter.

Download File

Now you are ready to develop the application. Download the example file and extract it. Open the index.php with your editor. It will be look like below

$consumerKey    = 'Consumer-Key';
$consumerSecret = 'Consumer-Secret';
$oAuthToken     = 'OAuthToken';
$oAuthSecret    = 'OAuth Secret';
//Find above details in Twitter Application


$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $oAuthToken, $oAuthSecret);

$sql=mysql_query("select url,title from article order by rand()");

$tweet->post('statuses/update', array('status' => ''.$showfetch['title'].''.$showfetch['url'].''));

Here you have to change the Consumer Key, Consumer Secret, OAuth Token and OAuth Secret. You can find these values in twitter application.

Connect to database and get the title and URL from database and pass it to the tweet function. Run the script by uploading to server and check the functionality.

Cron Job:

Now we are going to create a new cron job to run this application in every minute (In my case). Login to Cpanel and click the cron jobs. Create a new cron job and write the below command,

wget -O /dev/null -q

Here wget: Get an HTTP resource to run the PHP,-O /dev/null: Don’t write any logs while running cron job,-q: Turn off all visual output (quiet).And hit the page to run PHP script with mentioned URL!

DONE!! Your auto tweet application is running now..Check the twitter account for PHP posting the tweet from your database.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>