Learn How to Post on Twitter Using PHP and Oauth

I request many reader to write tutorial about, how to status update from php to twitter, So i post a tutorial for beginner over here. In this tutorial you will learn about , how to update status on twitter with php ?

Twitter is Great Social media site, it has world’s largest community and markets. Twitter has release Oauth Api for access twitter with your website or application. you can post on twitter without login in twitter, also access twitter with his Consumer Secret key, Access Token, Consumer Key and Secret key. I will use Oautho api for this tutorial. Download Here

Final Layout what we want to make.
Post on Twitter Using PHP

First go to https://apps.twitter.com/app/new Fill all details, agree the twitter policy and then click create button.

Create Twitter Application

After create application make sure application permission must “read and write” if it’s read only update it because we want to post on twitter.

Check Application Permission

After completing this all next step is, Go to Keys and Access Tokens tab and generate access tokens, consumers key, api key, screat token.

Generate Access Tokens

Now basic details is completed. Now we start about php code for posting. First download the Oautho.php here.

Lest come, First we learn basic code for better understanding. Very basic code is over here. Create a file named basictwitter.php and run it. And check your twitter You got notice that post is successfully updated.

<?php
require_once 'twitteroauth.php';
define("CONSUMER_KEY", "yourconsumerkey");
define("CONSUMER_SECRET","yourconsumerscretkey");
define("OAUTH_TOKEN", "oauthtoken");
define("OAUTH_SECRET", "oauthsecret");
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);
$content = $connection->get('account/verify_credentials');
$connection->post('statuses/update', array('status' => 'twit with http://designspicy.com - say hello')); ?>

Now go ahead and post twit with php and validation. Download Fully beautiful code with css and validation here. In this form if textarea is empty you will get error message and also post success notification.

<style>
.btn {
  background: #3498db;
  background-image: -webkit-linear-gradient(top, #3498db, #2980b9);
  background-image: -moz-linear-gradient(top, #3498db, #2980b9);
  background-image: -ms-linear-gradient(top, #3498db, #2980b9);
  background-image: -o-linear-gradient(top, #3498db, #2980b9);
  background-image: linear-gradient(to bottom, #3498db, #2980b9);
  -webkit-border-radius: 12;
  -moz-border-radius: 12;
  border-radius: 12px;
  font-family: Arial;
  color: #ffffff;
  font-size: 12px;
  padding: 6px 20px 7px 20px;
  text-decoration: none;
}

.btn:hover {
  background: #3cb0fd;
  background-image: -webkit-linear-gradient(top, #3cb0fd, #3498db);
  background-image: -moz-linear-gradient(top, #3cb0fd, #3498db);
  background-image: -ms-linear-gradient(top, #3cb0fd, #3498db);
  background-image: -o-linear-gradient(top, #3cb0fd, #3498db);
  background-image: linear-gradient(to bottom, #3cb0fd, #3498db);
  text-decoration: none;
}
.error {color: #FF0000;}
</style>

HTML form code

<form method="post" id="formcomment"><fieldset>
	<div id="errAll"></div>
	<p><label for="name">Name</label><br/>
	<input type="text" name="name" id="name" value="" size="35" />
	</p>
	<p><label for="email">Email</label><br/>
	<input type="text" name="email" id="email" value="" size="35" />
	</p>
	<p><label for="url">URL</label><br/>
	<input type="text" name="url" id="url" value="" size="35" />
	</p>
	<p><label for="message">Message</label><br/>
	<textarea cols="54" rows="8" name="message" id="message"></textarea>
	</p>
	<p><input type="submit" name="submitter" value="Submit Comment" /></p>
	<input type="hidden" name="task" id="task" value="comment" />
</fieldset></form>

PHP code for sending on twitter with validation.

<?php
require_once 'twitteroauth.php';
$twtmsg = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") 
{
if(empty($_POST["twtmsg"])) 
{
   $message = "Write Msg";
    } 
    else 
    {
     $twtmsg = $_POST['twtmsg']; 	
	define("CONSUMER_KEY", "yourconsumerkey");
	define("CONSUMER_SECRET","yourconsumerscretkey");
	define("OAUTH_TOKEN", "oauthtoken");
	define("OAUTH_SECRET", "oauthsecretkey");
	$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);
	$content = $connection->get('account/verify_credentials');
	$connection->post('statuses/update', array('status' => $twtmsg));
	 $message = "UPDATE Sucessfully";
    }
}
?>

Finaly full index.php’s code download here .
At last : Hope this help you. if you found any bug or error just comment, I will reply as soon as possible.

Savan Paun

I am Savan Paun Admin of this blog. I like to write about Wordpress, Design, Development, Plugin, SEO, Blogging and Making Money online. I am a professional blogger based in Jamnagar. I'm always available for beginner who wants to learn.

Comments

Leave a Reply