Post Live Tweet On Twitter From Your Website Using PHP



Do you want to post live tweet on twitter directly from your website? Twitter is one of the most popular social media website where you can tweet anything with a maximum limit of 140 characters. So today I’ll show you how to post live tweet on twitter from your website using PHP and twitter oAuth API.

Here I’ll show step by step how you can post live tweet on twitter as follows:

STEP 1:

  • Login from your twitter account.
  • 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 the name according to  you want to show there. In my case app name is discussdesk_twitter.

STEP 2:

After submitting the form, you will have following screen as shown below. Here you will find Consumer Key and Secret Key which we will use in our project further.

STEP 3:

Create a file config.php with following code as shown below. 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'); ?>

 

STEP 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.';
    }
   
}
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"><img src="./images/twitter.png" alt="Sign in with Twitter"/></a>';
}
else
{
    echo "<a href='logout.php'>Logout</a>
";
    echo '
'.$message.'
    
<form action="index.php" method="post">
        <input type="text" name="status" id="status" placeholder="Write a comment...."><input type="submit" value="Post Tweet!" style="padding: 5px;">
    </form>

';
}
?>

 

STEP 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');
}

?>

 

STEP 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'); ?>

 

Finally our project ‘Post Live Tweet On Twitter From Your Website Using PHP‘ is complete and ready to run. Hope this tutorial will be useful for you.

Don’t forget to share your doubts in the comment box and also share this post on social media and with your friends becaus“You share, I share, let’s make the world aware”.

You may want to take a look at the following related posts:

Also for more awesome tutorials, please don’t forget to like our facebook page TryCatch Classes.

 

Bonus: We also give training on following topics:

                       1. PHP Training in Mumbai.

2. Bootstrap Training Course in Mumbai.

3.  Web Designing Training in Mumbai.

4. UI / UX Training.

5. IOS Training Institute in Mumbai.