Design Spicy
  • Home
  • Make Money Online
  • Blogging Tips
  • Hosting
  • SEO
  • WordPress
  • PHP
  • Technology
You are here: Home / PHP / Learn How to Post on Twitter Using PHP and Oauth

Learn How to Post on Twitter Using PHP and Oauth

October 13, 2014 by Savan Paun 8 Comments

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.

print

  • Twitter
  • Facebook
  • G-plus
  • Linkedin
  • Reddit
  • Pintrest

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

  1. Pardeep Kumar says

    November 21, 2014 at 1:09 am

    Dear, I think this code only let me to tweet on my twitter wall, but I want everyone who wants to tweet from my website

    Reply
  2. jahanzaib says

    December 13, 2015 at 5:10 pm

    This code isn’t working for, there is no error or anything else.

    Reply
    • Savan Paun says

      February 22, 2016 at 11:41 pm

      Which type of error ? Did u enter Consumer Key and Secret key Correctly

      Reply
  3. ramesh says

    January 28, 2016 at 3:03 pm

    Please send twitteroauth.php source/link

    Reply
    • Savan Paun says

      October 25, 2016 at 12:21 am

      Hello Ramesh, You can get all files from download link. Check the Tutorial. If You found any error let me know. Thank You keep visiting.

      Reply
  4. Vipul Hadiya says

    May 11, 2016 at 3:58 am

    Can you tell me from where to get Oautho.php and twitteroauth.php ? You din’t explained anything about it.

    Reply
    • Savan Paun says

      October 25, 2016 at 12:17 am

      Hello Vipul, You can Download Full Source code from Download link.

      Reply
  5. priyanka patodia (@pranks1978) says

    November 19, 2016 at 11:43 am

    download link is not working properly… inspite of subscribing its repeatedly asking to subscirbe.. Hence I am unable to download

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Twitter login using PHP and Oauth
  • Popular Tips to Increase Your Adsense CPC and CTR
  • How to Increase Alexa Rank Of Your Blog or Website
  • Why Bluehost is The Best Blog Host for Bloggers
  • Basic SEO Tips For Bloggers

Categories

  • Blogging Tips
  • Hosting
  • Make Money Online
  • PHP
  • SEO
  • Technology
  • WordPress

Page

  • Contact Us
  • Privacy

Alexa Rank

About Me

image

Welcome to Design Spicy,

Copyright ©2018 Design Spicy