The Twitter API is a fantastic means of distributing real-time content from your website without any need to manually stay on-top of content updates.

How to Send Tweets Automatically with PHP

A while back we put together an article on implementing the Twitter API  on your website to display your latest tweets, and in conjunction with that we've put together a downloadable PHP script that allows you to automatically send tweets to your Twitter account from your website.

What is required?

To setup the automatic tweeting from your website, you'll need:

  • A Twitter account, developer app and a generated OAuth token (not sure if you've got them, check this article for instructions to create all the keys you'll need)
  • The data you want to tweet about and a method to query your data (in this example, we will use PDO)
  • Ability to setup a cron job
  • The TwitterOAuth PHP client library or J7mbo's twitter-api-php PHP client library (view on GitHub)

Creating the Twitter Connection

To send out tweets, we need to grant the account permission and then setup an instance of the TwitterOAuth or twitter-api-php class that we will be using.

Define the constants

# Define constants

TwitterOAuth Connection

# Create the connection
# Migrate over to SSL/TLS
$twitter->ssl_verifypeer = true;

Twitter-api-php Connection

$twitter = new TwitterAPIExchange(array(
    'oauth_access_token' => ACCESS_TOKEN,
    'oauth_access_token_secret' => ACCESS_TOKEN_SECRET,
    'consumer_key' => CONSUMER_KEY,
    'consumer_secret' => CONSUMER_SECRET

To send a tweet using the API, we call the "/statuses/update" endpoint with the POST parameter 'status'. Similarly to our previous article, we'll create an instance of the Twitter class, load our database connection and then post the request over to the Twitter API.

Using TwitterOAuth

$twitter->post('statuses/update', array('status' => $message));

Using twitter-api-php

$url = '';
$requestMethod = 'POST';
$postData = array('status' => 'Hi how are you');
echo $twitter->buildOauth($url, $requestMethod)

Once we've made the call, we can check everything went through without any problems by checking the response:

	id: 243145735212777472,
	id_str: "243145735212777472"

You can find a full response output on the Twitter API documentation.

Wrapping Up a Helper Class

To make the code more reusable throughout your project, we can create a helper library class that can easily be instantiated and called from multiple places in your project.

Using the helper class, we can easily call the following methods:

# Sends a tweet that we pass in (140 characters)
# Sends a random Tweet from our data source

Automated Posting to Twitter

We can now automate the entire process by creating a standalone file 'autoTweet.php' that we will run a cron job on.

# Send a random Tweet for us

That's all we need to get going, now we can setup a cron job using the following:

0 18 * * * /usr/bin/php /path/to/autoTweet.php > /dev/null 2>&1

This will send out a new Tweet every day at 18:00 (server time). You can use this cron job generator to set your own times.

Download PHP Auto-Tweeter Class