Let’s write our fist Twittertools app. This will be a really simple “Hello world” style application, just to show you how to start.
As we talked about on the First Steps page, this app is a robot-like application, a single user app. So, we will need a valid pair of application keys (consumer_key and consumer_secret) and a valid pair of user keys for this app (access_token and access_token_secret). Both are available after registering the application on the Twitter Developers area.
1. Registering the app
Name and Description: The name and description of your application. The name will appear on the “client” information available on every tweet posted from this app.
Obs:You must NOT include “Twitter” on the app name, otherwise an error will occur.
Website and Callback URL: The website of the application, and the callback url – the script that shall receive and deal with the user access tokens after he authorizes your application. We will not use this now, since we are writing a single-user app, with no need of authentication. But remember, when writing an app that needs user authentication, you need to fill this with a valid url ready to receive the auth data coming from Twitter. This is why you can’t use your local web server to host applications.
After registering, you will get a screen like this:
There are some things to note on this screen:
Chossing the Acces Level – by default, all applications are Read-only right after registration. You need to edit the app settings (the second tab on top) to change this. There are 3 models: R (read-only), RW (read and write – you need this model to post updates or make any change on the account like follow or unfollow somebody) and RWD, that gives you access to the user direct messages. Most often, you will need the RW permission model, like now. Go to the Settings tab to change this:
Choose the “Read and Write” option. On this screen you can also upload an icon for your application, and set the Organization name and website. This is optional now, but is strongly recommended when creating multi-user applications, which need user authorization.
Getting the Keys – On the first screen (the “Details” tab) you will get your consumer_key and consumer_key_secret. But we also need the access token, and we can get it on this same screen, at the bottom. You will see this:
So, click on the “Create my access token” button to get your access_token and access_token_secret .
2. Including the libs
Twittertools needs 2 libs to work (both included in the package): OAuth and TwitterOAuth. So, we have 3 libs to include in our project.
<?php require_once("OAuth.php"); require_once("TwitterOAuth.php"); require_once("Twittertools.php"); ?>
3. Setting the keys and creating the object
Remember the keys? We will use them now.
/* consumer key & consumer secret*/ $consumer_key = "app_consumer_key"; $consumer_secret = "app_consumer_secret"; /* access token & access token secret from your twitter user */ $access_token = "your_access_token"; $access_token_secret = "your_access_token_secret"; $tw = new TwitterTools($consumer_key,$consumer_secret,$access_token,$access_token_secret);
4. Let’s go to the action
With the twittertools object already instantiated, lets post a “Hello Twitter” tweet.
$tw->update("Hello, Twitter! Testing Twittertools lib");
Piece of cake, huh?
Now, for make things a little more interesting, lets add a greeting to your followers, according to current time.
The complete code bellow:
<?php require_once("OAuth.php"); require_once("TwitterOAuth.php"); require_once("TwitterTools.php"); /* consumer key & consumer secret - don't forget to fill it with your app keys */ $consumer_key = "app_consumer_key"; $consumer_secret = "app_consumer_secret"; /* access token & access token secret from your twitter user - don't forget to fill it with your access keys */ $access_token = "your_access_token"; $access_token_secret = "your_access_token_secret"; $tw = new TwitterTools($consumer_key,$consumer_secret,$access_token,$access_token_secret); $time = new DateTime("now"); $hour = $time->format("H"); if($hour >= 5 and $hour < 12) $greeting = "Good Morning"; if($hour < 18 and $hour >= 12) $greeting = "Good Afternoon"; else $greeting = "Good Night"; $ret = $tw->update("$greeting, Twitter! Testing Twittertools php lib."); echo "<pre>"; print_r($ret); echo "</pre>"; ?>
This is what the update method returns:
Array ( [retweeted] => [in_reply_to_status_id_str] => [in_reply_to_screen_name] => [id_str] => 215565884620681220 [contributors] => [in_reply_to_user_id_str] => [user] => Array ( [is_translator] => [id] => 339390974 [notifications] => [time_zone] => [profile_background_image_url] => http://a0.twimg.com/images/themes/theme1/bg.png [default_profile_image] => [profile_link_color] => 0084B4 [url] => http://twittertools.in [following] => [favourites_count] => 0 [created_at] => Thu Jul 21 01:26:23 +0000 2011 [contributors_enabled] => [utc_offset] => [geo_enabled] => [name] => TTools PHP Lib [profile_background_image_url_https] => https://si0.twimg.com/images/themes/theme1/bg.png [profile_use_background_image] => 1 [profile_text_color] => 333333 [protected] => [id_str] => 339390974 [show_all_inline_media] => [profile_image_url] => http://a0.twimg.com/profile_images/1452380051/advancedsettings_normal.png [followers_count] => 20 [profile_image_url_https] => https://si0.twimg.com/profile_images/1452380051/advancedsettings_normal.png [location] => World [profile_sidebar_border_color] => C0DEED [default_profile] => 1 [description] => TwitterTools open source PHP lib [profile_background_tile] => [profile_sidebar_fill_color] => DDEEF6 [statuses_count] => 1 [lang] => en [screen_name] => TToolslib [listed_count] => 0 [friends_count] => 4 [follow_request_sent] => [verified] => [profile_background_color] => C0DEED ) [retweet_count] => 0 [favorited] => [in_reply_to_status_id] => [source] => HelloTW Demo App [created_at] => Wed Jun 20 22:04:46 +0000 2012 [place] => [coordinates] => [geo] => [id] => 215565884620681220 [in_reply_to_user_id] => [truncated] => [text] => Good Night, Twitter! Testing Twittertools php lib. )
As you can see, you receive back a json object (this is de default format for all Twittertools methods) containing a user object and a tweet object (the update you just made). At this point, you can check your Twitter profile to see the new update there.
Note the “source” item on the tweet object. Its the name of your application. Cool, right?
Now that you already now how things happen, its time to move forward for some better challenge. Let’s make our first multi-user app! [ under construction ]