9,019 views

We have now come to the part where we will start the actual development of our first "single-user use" Twitter Application. Let me just recall the function of the Twitter Application I wish to build. I would like to be able to send Tweets from my Control Panel directly to Twitter without the usual signing in on Twitter method. How is that possible? If you followed the previous Twitter Tutorial How to retrieve a single access token in Twitter with tmhOAut, I've retrieved the Access Tokens for my Twitter Application there and I will use that as the authorization for my Twitter Application, It will serve as the Applications sign in pass and would allow the application to send tweets from my Control Panel to Twitter without the typical signing in on Twitter process.

By Rnel, on September 08, 2010, under Twitter Tutorials
 

We have now come to the part where we will start the actual development of our first "single-user use" Twitter Application. Let me just recall the function of the Twitter Application I wish to build.

I would like to be able to send Tweets from my Control Panel directly to Twitter without the usual signing in on Twitter method. How is that possible? If you followed the previous Twitter Tutorial How to retrieve a single access token in Twitter with tmhOAut, I've retrieved the Access Tokens for my Twitter Application there and I will use that as the authorization for my Twitter Application, It will serve as the Applications sign in pass and would allow the application to send tweets from my Control Panel to Twitter without the typical signing in on Twitter process.

I will still use tmhOAuth of Matt Harris - An OAuth 1.0A library written in PHP, specifically for use with the Twitter API. Check it out at github LINK. Follow him on twitter @themattharris

I have also created a component in HTML with jQuery that will be use for this Twitter Application. Read about One line jQuery Twitter like dynamic character counter function. That will serve as the form from my Control Panel where I will be sending Tweets from. It calculates the actual remaining characters a user can input based on the 140 limit character from Twitter.

  • The code flow is very simple.
  • - Send a Tweet to Twitter with the access tokens
  • - Twitter will check the access tokens
  • - If auhorized, Twitter will accept the Tweet and upate the status.
  • - If not auhorized, Twitter will deny the access and status will not be updated.
  • - Return to the Control Panel and display the appropriate message. Tweet is Away! if successfull. Error... if denied.

There are two important parts of this Twitter Application which I will discuss later. For those who are just excited as I am to Tweet from your website and don't wish to know about the backend side of the Twitter Application you can download the file and configure it's setting to start Tweeting Away!

NOTE!
THE DIRECTORY WHERE YOU WOULD RUN THIS TWITTER APPLICATION MUST BE PRIVATE AND PROTECTED!

 
  • Download the file: LINK

Extract the files and configure the settings.

 

Configure Settings:

Open index.php file and find this code located at the top of the file.

	    	<php
			//-- BEGIN - CONFIGURE SETTINGS
			  /**
			   * - I suggest that these comes from a secured database
			   */
				$tmhOAuth = new tmhOAuth(array(
					'consumer_key'		=> '',
					'consumer_secret'	=> '',
					'user_token'		=> '',
					'user_secret'		=> '',
				));
			//-- END
			?>
		

Enter the consumer_key, consumer_secret, user_token, and user_secret of your "single-user use" Twitter Application. You have the option to whether directly write those values on the corresponding fields above or fetch them from a datase or from any other file. I store those keys and tokens in a database and just fetch them when needed.

That's all you need to do!

Upload the files on a SECURED AND PROTECTED directory where you alone has the access and start tweeting from your website.

 

Backend:

You may want to skip this part if you don't need to learn about the backend side of this Twitter Application, but this is short and simple.

On the index.php file, it has a block of code that consists of two parts.

The first part is a PHP Snippet where it checks if the form is submitted and perform the tweet process if true. It calls the function tweet() from the PHP class $RNELdotNETtweetAway_tmhOAuth.

		<php
			if(isset($_REQUEST['send_tweet'])){ //check if "send tweet" is set
				$msg = isset($_POST['msg']) ? htmlspecialchars($_POST['msg']) : '';
				if($msg){
					$RNELdotNETtweetAway_tmhOAuth = new RNELdotNETtweetAway_tmhOAuth($msg);
					if($RNELdotNETtweetAway_tmhOAuth->tweet($tmhOAuth)){
						echo 'Tweet is Away!';
					}
					else{
						echo 'Error...';
					}
				}
			}
		?>
		

 

The second part is the HTML form where a user enter the messages to tweet. To know more about this code, read One line jQuery Twitter like dynamic character counter function.

			
Enter your message140

 

tweet() function

This function from RNELdotNETtweetAway_tmhOAuth PHP class is also very simple.

		/**
		 * tweet function - send status update request to twitter using tmhOAuth PHP library 
		 * @param - class - tmhOAuth library
		 */
		public function tweet($tmhOAuth){
			$tmhOAuth->request('POST', $tmhOAuth->url('statuses/update'), array(
				'status' => $this->status,
			));
	
			if($tmhOAuth->response['code'] == 200){
			  return true;
			}
			else{
			  return false;
			}
		}
		

It uses tmhOAuth PHP library to send a status update request to Twitter with the access tokens. Returns true if successful,false if not.

 

Conclusion:

It's easy and fun to build a Twitter Application and achieve your needed scripting requirements whether it's for personal convenience use like what I develop or for any public service usage like "Sign in with Twitter" Applications. All you need to do is enjoy while developing your Twitter Application and have some basic to advanced HTML, javascript, jQuery, and PHP knoweldge and of course on top of it all with the help of an oAuth library like the tmhOAuth PHP library.