Today's web is about sharing. My facebook API / movie experiment is up and running. It needs some tweaking but bit by bit..  What I did find important was a live stream. Of course you can go the old-school route of mail updates, but today updates are more efficient via facebook and twitter.

As I played already with the facebook API, today it was time to do some research on Twitter's API. Purpose: push new movie reviews to twitter! This way people can follow sharemovies and they get new movies automatically pushed into their stream. (Updates on the app are however posted to facebook via the activity stream)

There are some tutorials that suggest this method:

include '../../twitter.php';
$message = 'New movie ...';
$url = '';
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message");
curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
$buffer = curl_exec($curl_handle);
if (empty($buffer)) {
 echo "error!";
} else {
 echo 'success!';

.. but this method does not work anymore because twitter is now using Oauth.

So I followed this helpful article:

1. Register an app: This is quite common; Facebook asks the same.
You need the read & write permissions to post to your twitter account

2. You get 4 API keys. Download the Abraham Twitter OAuth class to connect to twitter using OAuth and fill in the keys.

3. including “OAuth.php”  and “twitteroauth.php” the rest is easy

File tweet.php

$consumerKey    = '<insert your consumer key';
$consumerSecret = '<insert your consumer secret>';
$oAuthToken     = '<insert your access token>';
$oAuthSecret    = '<insert your token secret>';
// twitteroauth.php points to OAuth.php
// all files are in the same dir
// create a new instance
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $oAuthToken, $oAuthSecret);
This makes the twitter instance which you can use to post updates to your stream (the user account that registers the app, is used to post the updates to!) :
// post to twitter
include 'tweet.php';  // the file where the tweet instance is created
$statusMessage = 'Movie added: '.$title. ' -> ' . $message;
$tweet->post('statuses/update', array('status' => $statusMessage));

So when adding this bit of code at the point where somebody adds a movie, it posts the new title + URL to the @sharemovies twitter account. Following this user and you get updated about new movies in your twitter stream :)

[caption id="attachment_622" align="alignnone" width="480" caption="Create your Movie stream with twitter API"]Movie stream[/caption]

I probably deploy the same with Facebook over time, but it is always good to play with various technologies.

Bob Belderbos

Software Developer, Pythonista, Data Geek, Student of Life. About me