They say you learn something new every day.

Posts tagged ‘API’

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

Tag Cloud