Search code examples
phpcurltwittercroncron-task

Scheduled posting to twitter in PHP cURL


I am currently able to post tweets to my Twitter account using Twitter API. I am using cURL. But now I want to post the tweets on scheduled date/time. I know that it is possible but no idea how ?

code :

<?php
  $twitter_consumer_key = 'xxx';
  $twitter_consumer_secret = 'xxx';
  $twitter_access_token = 'xxx';
  $twitter_access_token_secret = 'xxx';
  $twitter_version = '1.0';
  $sign_method = 'HMAC-SHA1';
  $status="HelloWorld";
  $url = 'https://api.twitter.com/1.1/statuses/update.json';
  $param_string = 'oauth_consumer_key=' . $this->twitter_consumer_key .
        '&oauth_nonce=' . time() .
        '&oauth_signature_method=' . $this->sign_method .
        '&oauth_timestamp=' . time() .
        '&oauth_token=' . $this->twitter_access_token .
        '&oauth_version=' . $this->twitter_version .
        '&status=' . rawurlencode($status);

  //Generate a signature base string for POST
  $base_string = 'POST&' . rawurlencode($url) . '&' . rawurlencode($param_string);
  $sign_key = rawurlencode($this->twitter_consumer_secret) . '&' . rawurlencode($this->twitter_access_token_secret);

  //Generate a unique signature
  $signature = base64_encode(hash_hmac('sha1', $base_string, $sign_key, true));
  $curl_header = 'OAuth oauth_consumer_key=' . rawurlencode($this->twitter_consumer_key) . ',' .
      'oauth_nonce=' . rawurlencode(time()) . ',' .
      'oauth_signature=' . rawurlencode($signature) . ',' .
      'oauth_signature_method=' . $this->sign_method . ',' .
      'oauth_timestamp=' . rawurlencode(time()) . ',' .
      'oauth_token=' . rawurlencode($this->twitter_access_token) . ',' .         'oauth_version=' . $this->twitter_version;

  $ch = curl_init();
  //Twitter post
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: ' . $curl_header));
  curl_setopt($ch, CURLOPT_VERBOSE, 1);
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, 'status=' . rawurlencode($status));

  $twitter_post = json_decode(curl_exec($ch));
  curl_close($ch);
  print_r($twitter_post);
?>

What changes do I need to make so as to post a tweet on scheduled date/time


Solution

  • Here is the big picture of what you can do to perform your scheduled tweets:

    1 - Set a cron task to run each minute to your page named post_tweets.php (for ex.)

    2 - When a user wanna schedule a tweet, save the tweet, including the date/time the tweet should be posted into database. Let's say the tweet should be posted at 2015-06-11 18:00:00

    3 - Each time your page post_tweets.php will be called by your cron, this page should check if you have an entry into your database older than the post_tweets.php time at execution time (if the page is called at 2015-06-11 18:00:35 or over , your page will see you have an entry set at 2015-06-11 18:00:00 >> go go , you have to post the tweet !)

    4 - If yes, post the tweet. Then delete the tweet from your db

    5 - If no, nothing happens

    This method will require a SELECT query runned each minute to check if there is something to post.

    I hope you'll understand better my logic now.