Feb
23

code #2. simple twitter php library 1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

posted by admin, permanent link, 3 comments

this is a simple twitter interface. you need your twitter account, let’s say, for the fun of it, chunkylover35 with the password password. this is not a valid account, but you should obtain yours by registering to twitter here. and to use it, do something like this:

<?php
include ('class_twitter.php');
$twitter = new Twitter ('chunkylover35','password');
if (!$twitter->updateStatus ("I'm watching the Simpsons.")) {
        die ($twitter->getError() . "\nFailed Receiving :\n" . $twitter->getReply());
        }
?>

getError() and getReply() methods are intended for error handling. the code is based on two php classes found on twitter api page, here and here, first which used sockets (and avoids curl) which was broken, and the second, which uses curl and don’t know if it works (as i hate curl and i don’t have it compiled in my php). my class is used only to set the user status, and uses sockets instead of curl.

class_twitter.php

class Twitter {
        var $nick;
        var $pass;
        var $errm;
        var $rply;

        function Twitter ($nick, $pass) {
                $this->nick = $nick;
                $this->pass = $pass;
                $this->errm = '';
                $this->rply = '';
                }

        function updateStatus ($status) {
                $postData = "status=".urlencode($status)."&";
                $toSend = "POST /statuses/update.json HTTP/1.1\r\n";
                $toSend .= "Host: twitter.com\r\n";
                $toSend .= "Authorization: Basic ".
                                base64_encode($this->nick.':'.$this->pass)."\r\n";
                $toSend .= "Connection: close\r\n";
                $toSend .= "Content-Type: application/x-www-form-urlencoded\r\n";
                $toSend .= "Content-Length: " . strlen($postData) . "\r\n";
                $toSend .= "X-Twitter-Client: ElfTwitterLib\r\n";
                $toSend .= "X-Twitter-Client-Version: 0.1\r\n";
                $toSend .= "X-Twitter-Client-URL: http://www.asmallelf.com\r\n";
                $toSend .= "\r\n{$postData}\r\n\r\n";

                $hSocket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
                if (!$hSocket) {
                        $this->errm = "Twitter error: failed creating socket ("
                                . socket_strerror(socket_last_error()) . ")\n";
                        return 0;
                        }
                if (!@socket_connect($hSocket, "twitter.com", 80)) {
                        $this->errm = "Twitter error: failed connecting to twitter.com ("
                                . socket_strerror(socket_last_error()) . ")\n";
                        return 0;
                        }
                if (@socket_send($hSocket, $toSend, strlen($toSend), 0) === false) {
                        $this->errm = "Twitter error: sending data (" .
                                socket_strerror(socket_last_error()) . ").\n";
                        return 0;
                        }
                $sReceived = '';
                while ($buf = @socket_read($hSocket, 4096)) { $sReceived .= $buf; }
                socket_close($hSocket);
                $this->rply = $sReceived;
                if (strpos($sReceived,'HTTP/1.1 200 OK')!==FALSE) return 1;
                $this->errm = "Twitter error: failed to login to twitter.com.\n";
                return 0;
                }

        function getError () { return $this->errm; }
        function getReply () { return $this->rply; }
        }
You can leave a response, or trackback from your own site.
tags: , , , posted in code, 3 comments »

Felix Oghina @ code #2. simple twitter php library

Hey, the first class you based your code on is mine :P . Indeed it didn’t work before, but I just updated it a couple days ago and now it works and it’s complete with error handling and stuff.

Mike @ code #2. simple twitter php library

Thanks for the class, useful!

Yogesh @ code #2. simple twitter php library

Thanks for the code, i’ve used it in my applications and its working like a charm..
Thanks again…

post your comment:
name (mandatory):
e-Mail (mandatory):
website:
comment (at least 10 characters):