4

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

Ahalya Hegde
  • 1,571
  • 2
  • 18
  • 41
  • 1
    Hey, that's a bit offtopic, but for anyone, who will want to use your code, like I just did, two things are missing. First curl_setopt($ch, CURLOPT_URL, $url); is needed. Second curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); is required where $postData should be equal to status=' . rawurlencode($status), otherwise you'll get "Couldn't authenticate you" error. – Stranger Jun 02 '15 at 01:02
  • Oh yes,thank you I have updated it now. – Ahalya Hegde Jun 02 '15 at 04:11

2 Answers2

2

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.

zeflex
  • 1,487
  • 1
  • 14
  • 29
0

You may use cron jobs.

http://www.thesitewizard.com/general/set-cron-job.shtml for explanations.

https://www.setcronjob.com/ for a free service if you don't have cron option on your hosting (usually shared hosting)

Enjoy !

zeflex
  • 1,487
  • 1
  • 14
  • 29
  • Well, I am not an expert in PHP, so this is bit confusing me. Is there a simple way to do this? It is quite easy with Facebook API. I am not getting the same with twitter. – Ahalya Hegde May 15 '15 at 11:29
  • 2
    As I seen, you cannot schedule tweet with their api like you can do with Facebook. So with php you cannot do it. You have to use cron jobs, take a look on it it's pretty simple. It's kinda windows tasks scheduler but for web servers. – zeflex May 15 '15 at 15:01
  • @SunShine so, what's are your plans ? :) – zeflex May 17 '15 at 04:33
  • hey @zeflex , I was busy with some other work. Now I am back. The link you provided is very nice. I just want to schedule so that it should work only for once. Not repeatedly. the user will give the date with year. Only on that date and year it should get posted. only "one" time. How to do it ? – Ahalya Hegde Jun 08 '15 at 08:40
  • You can schedule a cron running each minute to your script. Inside your script, you check if current time (on execution) is the same or past than the "one shot" scheduled post. If yes, do the curl and post you tweet. Then you delete the entry to avoid duplicates. – zeflex Jun 08 '15 at 16:32
  • How to delete only the particular entry from crontab ? crontab -r will delete all the entries. How to delete only those entries that ran. – Ahalya Hegde Jun 10 '15 at 07:13
  • No the delete should be done on the php side (I mean delete entry from database or whatever). I guess you don't see what I mean, the big picture in fact. Once you set the cron to run each minute for ex., you don't have to modify it. – zeflex Jun 10 '15 at 14:37
  • I will try to post a little bit later the big picture, editing my answer. – zeflex Jun 10 '15 at 14:37