php-post-to-twitter
Where This Code Came From
Working on a project for work I’ve been trying to find an easy way to post content in a form field to Twitter. I came across this code from the website below. It looked straightforward and easy to implement. The demo was the exact starting point that I wanted. However, I cannot get it to work. I’m posting it here so I can seek help from the online community to get it working. Below are the instructions that were found on the website.
Instructions from the website: https://www.discussdesk.com/download-php-code-to-post-tweet-on-twitter-with-live-demo.htm
-
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.
-
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.
-
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');
?>
- 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>';
}
?>
- 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');
}
?>
- 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');
?>