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 https://github.com/themattharris/tmhOAuth.git
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.
require_once(dirname(__FILE__).'/lib/tmhOAuth/tmhOAuth.php'); 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', $tmhOAuth->url('1.1/statuses/update'), array('status' => $text)); if ($response != 200) { die('There was an error tweeting the post.'); } }
It's just that simple. Give it a try.