Lonnie Knows Everything

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, "

VN:F [1.9.3_1094]
Rating: +1 (from 1 vote)

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
Leave a Comment :, , , more...



Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...