Geek Stuff
Post to twitter using PHP without using oauth or the API
by Lonnie on Mar.15, 2011, under Geek Stuff, Nerd Stuff, Programming
So you want to post twitter updates and perhaps follow people using PHP but you’re annoyed with that oauth implementation because it makes life difficult? no problem
I’ve constructed a class that implements two functions, posting a status update and following an author .. This is purely for educational purposes and is intended to demonstrate the art of page scraping as well as the use of CURL for posting and retrieving data, I would of course never personally recommend using the code because I’m sure it’s some sort of violation of their TOS ;) so yeah.. don’t do it! ;) ;)
Some important things to note:
1. This will break if twitter changes their html up too much
2. This is probably a violation, so if you do use it, use it reasonably .. don’t submit a million requests per second, throttle it down =)
3. You’re using it at your own risk
4. It will post status updates as if you posted from a mobile device ( Ie. Mobile Web )
DOWNLOAD: Click to download example and class file
USAGE:
include(“tweeter.php”);
$tweeter = new tweeter();
$tweeter->login($username,$password);
$tweeter->post_tweet(“Hey, this is cool!”);
$tweeter->folllow(“oneduality”);
Contents of tweeter.php:
class tweet {
var $user;
var $token='';
var $ch='';
var $action='';
// Logs in to twitter
function login($user,$pass) {
$this->user = $user;
// Initialize CH
if (!function_exists("curl_init")) die("This requires the CURL module, please install CURL for php.");
$this->ch = curl_init();
// Parse the login form
curl_setopt($this->ch, CURLOPT_URL, "https://mobile.twitter.com/session/new");
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->ch, CURLOPT_FAILONERROR, 1);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($this->ch, CURLOPT_TIMEOUT, 5);
curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->user . ".txt");
curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->user . ".txt");
curl_setopt($this->ch, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3 ");
$page = curl_exec($this->ch);
$page = stristr($page, "
");
preg_match("/form action=\"(.*?)\"/", $page, $this->action);
preg_match("/input name=\"authenticity_token\" type=\"hidden\" value=\"(.*?)\"/", $page, $this->token);
// Login and get your home page
$strpost = "authenticity_token=".urlencode($this->token[1])."&username=".urlencode($user)."&password=".urlencode($pass);
curl_setopt($this->ch, CURLOPT_URL, $this->action[1]);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $strpost);
$page = curl_exec($this->ch);
// Verify that we logged in ok
preg_match("/\
(.*?)\<\/div\>/", $page, $warning);
if (isset($warning[1])) return $warning[1];
$page = stristr($page,"
");
preg_match("/form action=\"(.*?)\"/", $page, $this->action);
preg_match("/input name=\"authenticity_token\" type=\"hidden\" value=\"(.*?)\"/", $page, $this->authenticity_token);
}
function post_tweet($status) {
// Set status
$tweet['display_coordinates']='';
$tweet['in_reply_to_status_id']='';
$tweet['lat']='';
$tweet['long']='';
$tweet['place_id']='';
$tweet['text']=$status;
$ar = array("authenticity_token" => $this->token[1], "tweet"=>$tweet);
$data = http_build_query($ar);
curl_setopt($this->ch, CURLOPT_URL, $this->action[1]);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $data);
$page = curl_exec($this->ch);
return true;
}
function follow($author_name) {
$action = "http://mobile.twitter.com/$author_name/follow";
$ar = array("authenticity_token" => $this->token[1], "tweet"=>$tweet, 'last_url'=>'/$author_name', );
$data = http_build_query($ar);
curl_setopt($this->ch, CURLOPT_URL, $action);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $data);
$page = curl_exec($this->ch);
return true;
}
}
Incoming search terms:
- curl login to twitter without Oauth
- post to twitter without oauth
- php post to twitter 2011
- tweeter api php oauth
- post to twitter php 2011
- twitter php api
- Posting to Twitter using PHP
- twitter post without oauth
- How to post to twitter with php and Curl without oAuth
- php post twitter without api