Download PHP Code to Post tweet on Twitter with Live Demo

Abhigyan Singh 29th Sep 2020

Here we will show to post a 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 the 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 the login button and write a post or message into text area you can achieve that thing.

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

1. Login from your twitter account and go to HTTP://twitter.com/app/new to create a new app. Fill all details like app name, redirect, URL and website URL. The app name will show when the user will log in through the app so give the name what you want to show there. In my case the app name is discussdesk_twitter.

2. After submitting the form you will have the 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 the following code as shown below as follows. Here you have to put the API and secret key of application in lines no. 2 and 3 respectively. THE redirect URL will put in line no. 4 in the defined method.

<?php

define('CONSUMER_KEY', 'API Key');

define('CONSUMER_SECRET', 'Secret Key');

define('OAUTH_CALLBACK', 'CALLBACK_URL');

?>

4. Create another file index.php with the following code as shown below. it is used to render the login button-like by clicking the user will go on the twitter site after that taking authentication will come back to the 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 the 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 the 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.

Authored By Abhigyan Singh

He is a continuous blogger and has blogged on different topic. He loves to surf Internet and always trying to get new Idea about new Technology and Innovations and sharing these great information to all the technology lovers.

Also on DiscussDesk