They say you learn something new every day.

Twitter API (21/04/2012)

I’m building a little web application, and for part of it, I want to use the Twitter API, but by God the documentation is complex.

Luckily, I’ve found Matt Harris’s Twitter API Functions. I’ve been playing with them all day, and finally discovered, thanks to Stack Overflow (as ever) that there’s a slight tweak you need to make because of a change in December 2011.

In the code below I’ve got a little function to post tweets. The line in bold is the one that was missing and was causing me to pull my hair out:

 <?php

$tweet_text = ‘Hello Twitter’;
print “Posting…\n”;
$result = post_tweet($tweet_text);
print “Response code: ” . $result . “\n”;

FUNCTION post_tweet($tweet_text) {

require_once(‘tmhOAuth.php’);

$connection = new tmhOAuth(array(
  ‘consumer_key’ => ‘xxx’,
  ‘consumer_secret’ => ‘xxx’,
  ‘user_token’ => ‘xxx’,
  ‘user_secret’ => ‘xxx’,
‘curl_ssl_verifypeer’ => false
));

$connection->request(‘POST’, $connection->url(‘1/statuses/update’), array(‘status’ => $tweet_text));
return $connection->response[‘code’];

}

?> 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Tag Cloud

%d bloggers like this: