Send Tweets to your Twitter Account via PHP
Posting status messages to a Twitter account via PHP is quite useful in many scenarios. Like automatically tweeting your blog’s latest content, tweeting list of facts, tips or quotes at regular intervals, tweeting daily news, etc. This article’s intention is to show how you can send a tweet to your Twitter account via PHP programming.
Required APIs, Tokens and Files
We will require the following things in order to tweet via PHP.
- An app created via your Twitter account with Read/Write permission.
- Consumer Key, Consumer Secret, Access Token, Access Token Secret
- OAuth API (i.e. a file called OAuth.php)
- PHP Library to support OAuth for Twitter’s REST API (i.e. a file called twitteroauth.php)
Let’s get started by following the below steps carefully.
Step 1: Go to the Create New App screen and login with your Twitter account.
Step 2: On the Create an application screen, enter the required details like Name, Description, and Website for your new Twitter app. Callback URL isn’t necessary and you may leave it blank. Accept the Developer Rules of the Road agreement. Enter the captcha and click Create your Twitter application.
Step 3: If your Twitter app is created successfully, you will see the app screen. On this screen under the OAuth Settings section, copy+paste the Consumer Key and Consumer Secret somewhere for future reference.
Clicking the Create my access token will be of no use at the moment, since the Access Level is still Read-Only. We need it to be with Read-Write permissions.
Step 4: Go to the Settings tab. Under the Application Type section, select Read and Write or Read, Write and Access direct messages if your application requires access to Direct Messages too. Click the Update this Twitter application’s settings button.
Step 5: Now, go back to the Details tab. You should see Access Level changed to Read and Write under the OAuth Settings section.
Under the Your access token section, click Create my access token button. If successful, you will see newly generated Access Token and Access Token Secret. Copy both the keys for future reference.
Step 7: Create a new file called index.php and add the following code to it.
// Include twitteroauth
// Set keys
$consumerKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
$consumerSecret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
$accessToken = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
$accessTokenSecret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
// Create object
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
// Set status message
$tweetMessage = 'This is a tweet to my Twitter account via PHP.';
// Check for 140 characters
if(strlen($tweetMessage) <= 140)
// Post the status message
$tweet->post('statuses/update', array('status' => $tweetMessage));
What the code does: This code includes the twitteroauth.php file which already includes the OAuth.php file. Define variables for Consumer Key, Consumer Secret, Access Token, Access Token Secret and copy the respective keys. Create an object of the class TwitterOAuth by passing the keys as parameters. Define a variable which holds the actual status message to be tweeted to your Twitter account. Before posting, it’s safer to check the length of the status message to be below 140 characters – else, the code may show an error. Call the post function as shown with your status message.
Place index.php too in the same directory, i.e. domainname.com/mytwitterbot.
Now, open the URL in your browser. In our case, it would be like http://domainname.com/mytwitterbot. In few seconds, you should see a tweet update in your Twitter account with the set status message.
The status message can be populated from an array, XML file, plain text file, database, or from another API.
Automate Tweets in PHP via CRON
To schedule an auto-tweeting functionality at regular intervals, you can add the above created index.php file to your CRON jobs. This way, the tweet will automatically go to your Twitter account at set intervals.
Did you face any problem while implementing the above method? Please share your experience in the comments below.