public
Last active

How to tweet with OAuth in three lines

  • Download Gist
twitter.php
PHP
1 2 3 4 5 6 7 8
<?php
// Download the latest version of TwitterOAuth from http://github.com/abraham/twitteroauth/downloads
// Unpack the download and place the twitteroauth.php and OAuth.php files in the same directory as this file.
// Register an application at http://dev.twitter.com/apps and from your new apps page get "my access token".
 
require_once('twitteroauth.php');
$connection = new TwitterOAuth('app consumer key', 'app consumer secret', 'my access token', 'my access token secret');
$status = $connection->post('statuses/update', array('status' => 'text to be tweeted'));

That does'nt work, but this does

session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');

/* If access tokens are not available redirect to connect page. /
if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) {
header('Location: ./clearsessions.php');
}
/
Get user access tokens out of the session. */
$access_token = $_SESSION['access_token'];

/* Create a TwitterOauth object with consumer/user tokens. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);

$parameters = array('status' => 'put your tweet in here');
$status = $connection->post('statuses/update', $parameters);

// make sure you set your config.php page

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.