Download PHP Code to Post tweet on Twitter with Live Demo

Here we will show to post tweet on twitter using twitter oAuth API and PHP. Twitter is a microblogging website where you can show yourself within the character limit of 140 characters. Twitter is the easiest way to see what’s going on in the world in real time and has become very popular. Here we are going to tell you how you can add facility of post tweet on twitter from your website.

Firstly you have to login through twitter API by entering your username and password after that write post or message what you want will be updated on your twitter timeline. You do not need to go on twitter to update tweet by just clicking on loginbutton and write post or message into text area you can achieve that thing.

Here we are telling you step by step that how you can do this thing as follows:

1.    Login from your twitter account and go to https://apps.twitter.com/app/new to create new app. Fill all details like app name, redirect url and website url. App name will show when user will login through app so give name what you want to show there. In my case app name is discussdesk_twitter.

2.    After submitting form you will have following screen as shown below. Here you are seeing consumer key and secret key will use in your project further.

3.    Create file config.php with following code as shown below as follows. Here you have to put api and secret key of application in line no. 2 and 3 respectively. Redirect url will put in line no. 4 in define method.

<?php
define('CONSUMER_KEY', 'API Key');
define('CONSUMER_SECRET', 'Secret Key');
define('OAUTH_CALLBACK', 'CALLBACK_URL');
?>

4.    Create another file index.php with following code as shown below. it is used to render login button like by clicking user will go on twitter site after that taking authentication will come back to index.php page and show textarea and button to update tweet on twitter.

<?php
error_reporting(0);
session_start();
?>
<h2>How to post tweet on Twitter with PHP.</h2>
<?php
require_once('oauth/twitteroauth.php');
require_once('config.php');
if(isset($_POST["status"]))
{
    $status = $_POST["status"];
if(strlen($status)>=130)
    {
            $status = substr($status,0,130);
    }
    $access_token = $_SESSION['access_token'];
    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);

    $connection->post('statuses/update', array('status' => "$status"));
    $message = "Tweeted Sucessfully!!";
}
if(isset($_GET["redirect"]))
{
    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);

    $request_token = $connection->getRequestToken(OAUTH_CALLBACK);

    $_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
    $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];

switch ($connection->http_code) {
case 200:
        $url = $connection->getAuthorizeURL($token);
header('Location: ' . $url); 
break;
default:
echo 'Could not connect to Twitter. Refresh the page or try again later.';
    }
exit;
}
if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) {

echo '<a href="./index.php?redirect=true"><imgsrc="./images/twitter.png" alt="Sign in with Twitter"/></a>';
}
else
{
echo "<a href='logout.php'>Logout</a><br>";
echo '<br>'.$message.'<br>
<form action="index.php" method="post">
<input type="text" name="status" id="status" placeholder="Write a comment...."><input type="submit" value="Post My Twitt!" style="padding: 5px;">
</form>';
}
?>

5.    Create another file process.php to check and save user access token with following code as shown below.

<?php
session_start();
require_once('oauth/twitteroauth.php');
require_once('config.php');

if (isset($_REQUEST['oauth_token']) && $_SESSION['oauth_token'] !== $_REQUEST['oauth_token']) {
  $_SESSION['oauth_status'] = 'oldtoken';
header('Location: ./logout.php');
}

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);

$access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);
//save new access tocken array in session
$_SESSION['access_token'] = $access_token;

unset($_SESSION['oauth_token']);
unset($_SESSION['oauth_token_secret']);

if (200 == $connection->http_code) {
  $_SESSION['status'] = 'verified';
header('Location: ./index.php');
} else {
header('Location: ./logout.php');
}
?>

6.    To destroy the user session we will create another file called logout.php with following code as shown below:

<?php
session_start();
session_destroy();
header('Location: ./index.php');
?>

Hope this tutorial will useful for you still have any query regarding twitter api feel free to comment below.

Share this Article on Social Media

All of my Scripts are ready to customized as per your requirement. Feel free to contact for script customization.

Contact me at discussdesk@gmail.com

"Note : It will be charged as per your customization requirement :)"

Get Updates, Scripts & Other Tutorials to Directly to your Email

Over 20000+ Happy Readers already subscribed. (We don't send spam email). Every email subscriber can get our latest updates and download our 100+ scripts.

Comments