How to Auto Post on Twitter with PHP

Manually post to social networks is probably better than to use an automated method. But, if you post many new topics every day, automated post is practically inevitable.

Twitter, as almost any social network, brings an Application Programming Interface, Twitter API, in order to communicate with it programmatically. You cannot use Twitter API directly (using your username and password), but you have to create a Twitter App. Creating a Twitter App is easy (see below). After you create your app, you get ConsumerKey, ConsumerSecret, AccessToken, AccessTokenSecret and you have to use them as credentials to communicate using Twitter API.

Additionally, you need a library for your programming language (PHP in my case) to easily use Twitter API. There are many libraries available. In this tutorial I use Codebird-php.

How to Create a Twitter app

Step 1 – Go to https://dev.twitter.com/apps and sign in with your account

Step 2 – Press ‘Create a new application’ button

Step 3 – Complete the required fields and press ‘Create your Twitter application’ (in the bottom of the screen)

Step 4 – Go to ‘Settings’ TAB and set Application Type to ‘Read and Write’. Then press ‘Update this Twitter application’s settings’

Step 5 – Go to Details TAB and press ‘Create my access token’ button

Step 6 – Go to oAuth tool TAB and get your access tokens

Which PHP library for Twitter to select?

You may use any library available (in PHP section) of twitter libraries.

I tested many of these libraries. The are all working well. I finally selected Codebird-php, because

  • it is well written and documented
  • you can easily post an image with this library

Get Codebird-php from Github or Sourceforge. There is a DEV branch (2.5.0) on Github, which works well (at least with my tests). But, I preferred to use the stable version 2.4.1 (MASTER branch).

Actually, you need two files

  • codebird.php (the main class)
  • cacert.pem (Validate Twitter SSL certificate)

Get and put them in the same directory.

Codebird Requirements

Post single tweet using php

Here is an example to post a single tweet

// require codebird
require_once('/path/to/codebird_php_v2.4.1/codebird.php');

\Codebird\Codebird::setConsumerKey("your_ConsumerKey", "your_ConsumerSecret");
$cb = \Codebird\Codebird::getInstance();
$cb->setToken("your_AccessToken", "your_AccessTokenSecret");

$params = array(
        'status' => 'Auto Post on Twitter with PHP http://goo.gl/OZHaQD #php #twitter'
);
$reply = $cb->statuses_update($params);

Post tweet with image using PHP

Here is an example to post a tweet with image

// require codebird
require_once('/path/to/codebird_php_v2.4.1/codebird.php');

\Codebird\Codebird::setConsumerKey("your_ConsumerKey", "your_ConsumerSecret");
$cb = \Codebird\Codebird::getInstance();
$cb->setToken("your_AccessToken", "your_AccessTokenSecret");

$params = array(
        'status' => 'Auto Post on Twitter with PHP http://goo.gl/OZHaQD #php #twitter',
        'media[]' => '/path/to/picture.png'
);
$reply = $cb->statuses_updateWithMedia($params);

Twitter 140 characters restriction

Although the total length is more than 140 chars, you can successfully tweet DIRECTLY, something like this

Automatically Post on Twitter with PHP using Codebird - Step by step tutorial http://www.pontikis.net/blog/auto_post_on_twitter_with_php #php #twitter

because, Twitter will shorten the URL for you, so the total lenght will become less than 140.

But, you cannot do it using the above code. So, keep your automatic tweets length less than 140 characters (use fewer words or shorten URL)

Automatically Post on Twitter with PHP using Codebird http://goo.gl/OZHaQD #php #twitter

Putting them all together

The following PHP script can be invoked from command line (or Cron) or from your browser.

Remember to put this script in a protected directory of your web server (without public access).

Here is a short description of the script.

  • I use MySQL in this example, but, of course, any database could be used. In my database, the “topics” table has some columns to support post automation to Twitter, as:
    • twitter_post varchar(140) – the text to be posted
    • twitter_image varchar(400) – the path of the image to be uploaded (if any)
    • twitter_pubstatus int – “0” means that this topic is pending to be posted, “1” means topic has been posted to twitter
  • First, an array is created ($share_topics), which contains the topics to be posted to twitter. Obviously, you have to customize the SQL query, according to your database structure.
  • Then, a connection is established with Twitter using my Twitter app tokens and codebird-php
  • Each array element is posted to Twitter. I check the response using https://dev.twitter.com/docs/error-codes-responses. If response is 200 (SUCCESS), this topic twitter_pubstatus is set to 1 (so, it will not be tweeted once more, in case script will run again)
  • The script produces a meaningful report and keeps logs.

    Take care for logfile rotation, for example:

                            nano /etc/logrotate.d/auto_share
                    

    Add the following replace /path/to/… with your path)

                    /path/to/auto_share.log {
                            weekly
                            missingok
                            rotate 4
                            notifempty
                            create
                    }
                    

Here is the code:

<?php
// determine script invocation (CLI or Web Server)
if(php_sapi_name() == 'cli') {
        $line_break = PHP_EOL;
} else {
        $line_break = '<br>';
}

// require codebird
require_once('/path/to/codebird_php_v2.4.1/codebird.php'); // configure /path/to/... appropriately

// get current time - configure appropriately, depending to how you store dates in your database
$now =  date("YmdHis");

// initialize
$share_topics = array();

// connect to database
$conn = new mysqli("db_server", "db_user", "db_passwd", "db_name");

// check connection
if ($conn->connect_error) {
        trigger_error('Database connection failed: '  . $conn->connect_error, E_USER_ERROR);
}

// create array with topics to be posted on Twitter
$sql = 'SELECT id as topic_id, twitter_post, twitter_image, twitter_pubstatus FROM topics ' .
        'WHERE date_published IS NOT NULL AND date_published <= ' . "'" . $now . "' " .
        'AND twitter_pubstatus = 0 ' .
        'ORDER BY date_published ASC';

$rs = $conn->query($sql);
if($rs === false) {
        $user_error = 'Wrong SQL: ' . $sql . '<br>' . 'Error: ' . $conn->errno . ' ' . $conn->error;
        trigger_error($user_error, E_USER_ERROR);
}
$rs->data_seek(0);
while($res = $rs->fetch_assoc()) {
        $a_topic = array(
                "topic_id" => $res["topic_id"],
                "twitter_post" => $res["twitter_post"],
                "twitter_image" => $res["twitter_image"],
                "twitter_pubstatus" => $res["twitter_pubstatus"]
        );
        array_push($share_topics, $a_topic);
}
$rs->free();

// initialize Codebird (using your access tokens) - establish connection with Twitter
\Codebird\Codebird::setConsumerKey("your_ConsumerKey", "your_ConsumerSecret");
$cb = \Codebird\Codebird::getInstance();
$cb->setToken("your_AccessToken", "your_AccessTokenSecret");

// AUTOMATIC TWEET EACH TOPIC
foreach($share_topics as $share_topic) {

        if($share_topic['twitter_status'] == 0) {

                if($share_topic['twitter_image']) {
                        $params = array(
                                'status' => $share_topic['twitter_post'],
                                'media[]' => $share_topic['twitter_image']
                        );
                        $reply = $cb->statuses_updateWithMedia($params);
                } else {
                        $params = array(
                                'status' => $share_topic['twitter_post']
                        );
                        $reply = $cb->statuses_update($params);
                }

            // check if tweet successfully posted
                $status = $reply->httpstatus;
                if($status == 200) {

                        // mark topic as tweeted (ensure that it will be tweeted only once)
                        $sql = 'UPDATE topics SET twitter_pubstatus = 1 WHERE id = ' . $share_topic['topic_id'];
                        if($conn->query($sql) === false) {
                                trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
                        }
                        $result .= $share_topic['topic_id'] . ' ' . $share_topic['twitter_post'] . ' success (' . $status . ')' . $line_break;

                } else {
                        $result .= $share_topic['topic_id'] . ' ' . $share_topic['twitter_post'] . ' FAILED... (' . $status . ')' . $line_break;
                }

                sleep(3);
        }

}

if(php_sapi_name() == 'cli') {
        // keep log
        file_put_contents('/path/to/auto_share.log', $result . str_repeat('=', 80) . PHP_EOL, FILE_APPEND);

        echo $result;

} else {
        $html = '<html><head>';
                $html .= '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
                $html .= '</head>';
        $html .= '<body>';
        $html .= $result;
        $html .= '</body>';
        $html .= '</html>';
        echo $html;
}

?>

Cron automation

Here is an example to run every day on 07:30 (your server time) the auto share php script, using Cron. A mail will be sent with the results.

As root

crontab -e

add the following line

30 7 * * * /usr/bin/php /path/to/auto_share.php | mail -s "Auto share results" you@your-email.com