Tweet to Twitter with PHP on Your Website

December 31, 2015

It turns out, generating a tweet is pretty simple with PHP. First, you have to login to twitter and generate application keys. Then, get the tmh0Auth project. Make sure and read the README for that project.

git clone

You should end up with 4 keys/tokens/secrets from twitter. Save them off like so.

$config['twitter_consumer_key'] = 'xxxxxx';
$config['twitter_consumer_secret'] = 'xxxxxx';
$config['twitter_token'] = 'xxxxxx';
$config['twitter_secret'] = 'xxxxxx';

Now, you can implement a tweet function that will send the tweet given raw $text.

function tweet($text)
   global $config;
   $tmhOAuth = new tmhOAuth(array(
                                  'consumer_key' => $config['twitter_consumer_key'],
                                  'consumer_secret' => $config['twitter_consumer_secret'],
                                  'token' => $config['twitter_token'],
                                  'secret' => $config['twitter_secret'],
   $response = $tmhOAuth->request('POST',
                                  array('status' => $text));
   if ($response != 200)
      die('There was an error tweeting the post.');

It's just that simple. Give it a try.

Related Posts