Twitter API [HELP]

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Twitter API [HELP]

    i Just Coded This Script Based On Twitter API class

    DEMO - DJfire.in Bollywood Celebrities Talks

    I wanted To Add Comments Feture Too for it
    Visitors Can Write Comments With Their Twitter Accounts
    Any One Can Suggest Me How can I Do this ???
    Any Specific Library to Do this

    #2
    take this this class. is for update status, but you can modify to post all users user via GET or POST
    for me, is work very well. You need server with PHP 5.
    example.php
    PHP Code:
    <html>
    <head>
    <title> Online Demo - Twitter Auto Publish (without oAuth)</title>
    </head>
    <body>
    <h2> Online Demo - Twitter Auto Publish (without oAuth)</h2>
    <form action="#" method="post">
    <div>Twitter Username: <input type="text" name="uname" /></div>
    <div>Twitter Password: <input type="password" name="password" /></div>
    <div>Status: <input type="text" name="status" size="140" /></div>
    <div><input type="submit" name="publish" value="Publish" /></div>
    </form>
    <?php
    if(isset($_POST['uname'])){
        @
    session_start();
        require_once(
    "openinviter_base.inc.php");
        require_once(
    "twitter_auto_publish.inc.php");
        
    $user=trim($_POST['uname']);
        
    $pass=trim($_POST['password']);    
        
    $msg=trim($_POST['status']);
        
    $wolfy=new twitter_auto_publish();
        
    $wolfy->login($user,$pass);
        
    $wolfy->updateTwitterStatus($msg);
        echo 
    "SUCCESS: Status updated !";
        echo 
    "<h3>Following: </h3>".$wolfy->getFollowings();
        echo 
    "<h3>Followers: </h3>".$wolfy->getFollowers();
        
    $wolfy->logout();    
    }
    ?>
    </body></html>
    openinviter_base.inc.php
    PHP Code:
    <?php
    abstract class OpenInviter_Base {
        protected 
    $session_id;
        private 
    $curl;
        private 
    $has_errors=false;
        private 
    $debug_buffer=array();
        public 
    $service;
        public 
    $service_user;
        public 
    $service_password;
        public 
    $settings=array('transport'=>'curl','cookie_path'=>'/','remote_debug'=>false'local_debug'=>true);
        private 
    $messageDelay;
        private 
    $maxMessages;
        protected function 
    getElementDOM($string_bulk,$query,$attribute=false) {
            
    $search_val=array();
            
    $doc=new DOMDocument();
            
    libxml_use_internal_errors(true);
            if (!empty(
    $string_bulk)) $doc->loadHTML($string_bulk);
            else return 
    false;
            
    libxml_use_internal_errors(false);
            
    $xpath=new DOMXPath($doc);
            
    $data=$xpath->query($query);
            if (
    $attribute)
                foreach (
    $data as $node)
                    
    $search_val[]=$node->getAttribute($attribute);
            else
                foreach (
    $data as $node)
                    
    $search_val[]=$node->nodeValue;
            if (empty(
    $search_val))
                return 
    false;
            return 
    $search_val;
        }

        
    /**
         * Extract a substring from a string
         */
        
    protected function getElementString($string_to_search,$string_start,$string_end) {
            if (
    strpos($string_to_search,$string_start)===false)
                return 
    false;
            if (
    strpos($string_to_search,$string_end)===false)
                return 
    false;
            
    $start=strpos($string_to_search,$string_start)+strlen($string_start);
            
    $end=strpos($string_to_search,$string_end,$start);
            
    $return=substr($string_to_search,$start,$end-$start);
            return 
    $return;
        }

        
    /**
         * Extracts hidden elements from a HTML bulk
         */
        
    protected function getHiddenElements($string_bulk) {
            
    $post_elements="";
            
    $doc=new DOMDocument();
            
    libxml_use_internal_errors(true);
            if (!empty(
    $string_bulk)) $doc->loadHTML($string_bulk);
            
    libxml_use_internal_errors(false);
            
    $xpath=new DOMXPath($doc);
            
    $query="//input[@type='hidden']";
            
    $data=$xpath->query($query);
            foreach(
    $data as $val) {
                
    $name=$val->getAttribute('name');
                
    $value=$val->getAttribute('value');
                
    $post_elements[(string)$name]=(string)$value;
            }
            return 
    $post_elements;
        }

        
    /**
         * Parse a CSV string into an array
         */
        
    protected function parseCSV($file$delimiter=',') {
            
    $expr="/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/";
            
    $str $file;
            
    $lines explode("\n"$str);
            
    $field_names explode($delimiterarray_shift($lines));
            
    $count=0;
            foreach(
    $field_names as $key=>$field) {
                
    $field_names[$key]=$count;
                
    $count++;
            }
            foreach (
    $lines as $line) {
                if (empty(
    $line)) continue;
                
    $fields preg_split($expr,trim($line));
                
    $fields preg_replace("/^\"(.*)\"$/","$1",$fields);
                
    $_res=array();
                foreach (
    $field_names as $key => $f$_res[$f] = (isset($fields[$key])?$fields[$key]:false);
                
    $res[] = $_res;
            }
            if(!empty(
    $res)) return $res;else return false;
        }

        
    /**
         * Extract Location: header
         */
        
    protected function followLocation($result,$old_url) {
            if ((
    strpos($result,"HTTP/1.1 3")===false) AND (strpos($result,"HTTP/1.0 3")===false)) return false;
            
    $new_url=trim($this->getElementString($result,"Location: ",PHP_EOL));
            if (empty(
    $new_url)) $new_url=trim($this->getElementString($result,"location: ",PHP_EOL));
            if (!empty(
    $new_url))
                if (
    strpos($new_url,'http')===false) {
                    
    $temp=parse_url($old_url);
                    
    $new_url=$temp['scheme'].'://'.$temp['host'].($new_url[0]=='/'?'':'/').$new_url;
                }
            return 
    $new_url;
        }

        
    /**
         * Check for an active session
         */
        
    protected function checkSession() {
            return (empty(
    $this->session_id)?FALSE:TRUE);
        }

        
    /**
         * Get the OpenInviter session ID
         */
        
    public function getSessionID() {
            return (empty(
    $this->session_id)?time().'.'.rand(1,10000):$this->session_id);
        }

        protected function 
    startSession($session_id=false) {
            if (
    $session_id) {
                
    $path=$this->getCookiePath($session_id);
                if (!
    file_exists($path)) {
                    
    $this->internalError="Invalid session ID";
                    return 
    false;
                }
                
    $this->session_id=$session_id;
            }
            else
                
    $this->session_id=$this->getSessionID();
            return 
    true;
        }

        protected function 
    endSession() {
            if (
    $this->checkSession()) {
                
    $path=$this->getCookiePath($this->session_id);
                if (
    file_exists($path)) unlink($path);
                
    $path=$this->getLogoutPath($this->session_id);
                if (
    file_exists($path)) unlink($path);
                unset(
    $this->session_id);
            }
        }

        
    /**
         * Get the cookies file path
         */
        
    protected function getCookiePath($session_id=false) {
            if (
    $session_id$path=$this->settings['cookie_path'].DIRECTORY_SEPARATOR.'oi.'.$session_id.'.cookie';
            else 
    $path=$this->settings['cookie_path'].DIRECTORY_SEPARATOR.'oi.'.$this->getSessionID().'.cookie';
            return 
    $path;
        }

        
    /**
         * Get the logout file path
         */
        
    protected function getLogoutPath($session_id=false) {
            if (
    $session_id$path=$this->settings['cookie_path'].DIRECTORY_SEPARATOR.'oi.'.$session_id.'.logout';
            else 
    $path=$this->settings['cookie_path'].DIRECTORY_SEPARATOR.'oi.'.$this->getSessionID().'.logout';
            return 
    $path;
        }

        
    /**
         * Intialize transport
         */
        
    public function init($session_id=false) {
            
    $session_start=$this->startSession($session_id);
            if (!
    $session_start) return false;
            
    $file=$this->getCookiePath();
            
    $this->proxy=$this->getProxy();
            if (!
    $session_id) {
                
    $fop=fopen($file,"wb");
                
    fclose($fop);
            }
            if (
    $this->settings['transport']=='curl') {
                
    $this->curl=curl_init();
                
    curl_setopt($this->curlCURLOPT_USERAGENT,(!empty($this->userAgent)?$this->userAgent:"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1"));
                
    curl_setopt($this->curlCURLOPT_FOLLOWLOCATIONfalse);
                
    curl_setopt($this->curlCURLOPT_COOKIEFILE,$file);
                
    curl_setopt($this->curlCURLOPT_HEADERfalse);
                
    curl_setopt($this->curlCURLOPT_SSL_VERIFYPEERfalse);
                
    curl_setopt($this->curlCURLOPT_SSL_VERIFYHOSTfalse);
                
    curl_setopt($this->curlCURLOPT_HTTP_VERSIONCURL_HTTP_VERSION_1_0);
                
    curl_setopt($this->curlCURLOPT_RETURNTRANSFER,true);
                
    curl_setopt($this->curlCURLOPT_COOKIEJAR$file);
                if (
    strtoupper (substr(PHP_OS0,3))== 'WIN'curl_setopt($this->curlCURLOPT_CONNECTTIMEOUT, (isset($this->timeout)?$this->timeout:5)/2);
                else  
    curl_setopt($this->curlCURLOPT_TIMEOUT, (isset($this->timeout)?$this->timeout:5));
                
    curl_setopt($this->curlCURLOPT_AUTOREFERERTRUE);
                if (
    $this->proxy) {
                    
    curl_setopt($this->curlCURLOPT_PROXY$this->proxy['host']);
                    
    curl_setopt($this->curlCURLOPT_PROXYPORT$this->proxy['port']);
                    if (!empty(
    $this->proxy['user']))
                        
    curl_setopt($this->curlCURLOPT_PROXYUSERPWD$this->proxy['user'].':'.$this->proxy['password']);
                }
            }
            return 
    true;
        }

        
    /**
         * Execute a GET request
         */
        
    protected function get($url,$follow=false,$header=false,$quiet=true,$referer=false,$headers=array()) {

            if (
    $this->settings['transport']=='curl') {
                
    curl_setopt($this->curlCURLOPT_URL$url);
                
    curl_setopt($this->curlCURLOPT_POST,false);
                
    curl_setopt($this->curlCURLOPT_HTTPGET ,true);
                if (
    $headers) {
                    
    $curl_headers=array();
                    foreach (
    $headers as $header_name=>$value)
                        
    $curl_headers[]="{$header_name}{$value}";
                    
    curl_setopt($this->curl,CURLOPT_HTTPHEADER,$curl_headers);
                }
                if (
    $header OR $followcurl_setopt($this->curlCURLOPT_HEADERtrue);
                else 
    curl_setopt($this->curlCURLOPT_HEADERfalse);
                if (
    $referercurl_setopt($this->curlCURLOPT_REFERER$referer);
                else 
    curl_setopt($this->curlCURLOPT_REFERER'');
                
    $result=curl_exec($this->curl);
                if (
    $follow) {
                    
    $new_url=$this->followLocation($result,$url);
                    if (!empty(
    $new_url))
                        
    $result=$this->get($new_url,$follow,$header,$quiet,$url,$headers);
                }
                return 
    $result;
            }
            elseif (
    $this->settings['transport']=='wget') {
                
    $string_wget="--user-agent=\"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1\"";
                
    $string_wget.=" --timeout=".(isset($this->timeout)?$this->timeout:5);
                
    $string_wget.=" --no-check-certificate";
                
    $string_wget.=" --load-cookies ".$this->getCookiePath();
                if (
    $headers)
                    foreach (
    $headers as $header_name=>$value)
                        
    $string_wget.=" --header=\"".escapeshellcmd($header_name).": ".escapeshellcmd($value)."\"";
                if (
    $header$string_wget.=" --save-headers";
                if (
    $referer$string_wget.=" --referer={$referer}";
                
    $string_wget.=" --save-cookies ".$this->getCookiePath();
                
    $string_wget.=" --keep-session-cookies";
                
    $string_wget.=" --output-document=-";
                
    $url=escapeshellcmd($url);
                if (
    $quiet)
                    
    $string_wget.=" --quiet";
                else {
                    
    $log_file=$this->getCookiePath().'_log';
                    
    $string_wget.=" --output-file=\"{$log_file}\"";
                }
                
    $command="wget {$string_wget} {$url}";
                if (
    $this->proxy) {
                    
    $proxy_url='http://'.(!empty($this->proxy['user'])?$this->proxy['user'].':'.$this->proxy['password']:'').'@'.$this->proxy['host'].':'.$this->proxy['port'];
                    
    $command="export http_proxy={$proxy_url} && ".$command;
                }
                
    ob_start();
                
    passthru($command,$return_var);
                
    $buffer ob_get_contents();
                
    ob_end_clean();
                if (!
    $quiet) {
                    
    $buffer=file_get_contents($log_file).$buffer;
                    
    unlink($log_file);
                }
                if((
    strlen($buffer)==0)or($return_var!=0)) return(false);
                else return 
    $buffer;
            }
        }

        
    /**
         * Execute a POST request
         */
        
    protected function post($url,$post_elements,$follow=false,$header=false,$referer=false,$headers=array(),$raw_data=false,$quiet=true) {
            
    $flag=false;
            if (
    $raw_data)
                
    $elements=$post_elements;
            else {
                
    $elements='';
                foreach (
    $post_elements as $name=>$value) {
                    if (
    $flag)
                        
    $elements.='&';
                    
    $elements.="{$name}=".urlencode($value);
                    
    $flag=true;
                }
            }
            if (
    $this->settings['transport']=='curl') {
                
    curl_setopt($this->curlCURLOPT_URL$url);
                
    curl_setopt($this->curlCURLOPT_POST,true);
                if (
    $headers) {
                    
    $curl_headers=array();
                    foreach (
    $headers as $header_name=>$value)
                        
    $curl_headers[]="{$header_name}{$value}";
                    
    curl_setopt($this->curl,CURLOPT_HTTPHEADER,$curl_headers);
                }
                if (
    $referercurl_setopt($this->curlCURLOPT_REFERER$referer);
                else 
    curl_setopt($this->curlCURLOPT_REFERER'');
                if (
    $header OR $followcurl_setopt($this->curlCURLOPT_HEADERtrue);
                else 
    curl_setopt($this->curlCURLOPT_HEADERfalse);
                
    curl_setopt($this->curlCURLOPT_POSTFIELDS$elements);
                
    $result=curl_exec($this->curl);
                if (
    $follow) {
                    
    $new_url=$this->followLocation($result,$url);
                    if (
    $new_url)
                        
    $result=$this->get($new_url,$post_elements,$follow,$header,$url,$headers,$raw_data);
                }
                return 
    $result;
            }
            elseif (
    $this->settings['transport']=='wget') {
                
    $string_wget="--user-agent=\"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1\"";
                
    $string_wget.=" --timeout=".(isset($this->timeout)?$this->timeout:5);
                
    $string_wget.=" --no-check-certificate";
                
    $string_wget.=" --load-cookies ".$this->getCookiePath();
                if (!empty(
    $headers))
                    foreach (
    $headers as $header_name=>$value)
                        
    $string_wget.=" --header=\"".escapeshellcmd($header_name).": ".escapeshellcmd($value)."\"";
                if (
    $header$string_wget.=" --save-headers";
                if (
    $referer$string_wget.=" --referer=\"{$referer}\"";
                
    $string_wget.=" --save-cookies ".$this->getCookiePath();
                
    $string_wget.=" --keep-session-cookies";
                
    $url=escapeshellcmd($url);
                
    $string_wget.=" --post-data=\"{$elements}\"";
                
    $string_wget.=" --output-document=-";
                if (
    $quiet)
                    
    $string_wget.=" --quiet";
                else {
                    
    $log_file=$this->getCookiePath().'_log';
                    
    $string_wget.=" --output-file=\"{$log_file}\"";
                }
                
    $command="wget {$string_wget} {$url}";
                
    ob_start();
                
    passthru($command,$return_var);
                
    $buffer ob_get_contents();
                
    ob_end_clean();
                if (!
    $quiet) {
                    
    $buffer=file_get_contents($log_file).$buffer;
                    
    unlink($log_file);
                }
                if((
    strlen($buffer)==0)or($return_var!=0)) return false;
                else return 
    $buffer;
            }
        }

        protected function 
    getProxy() {
            if (!empty(
    $this->settings['proxies']))
                if (
    count($this->settings['proxies'])==1) {
                    
    reset($this->settings['proxies']);
                    return 
    current($this->settings['proxies']);
                }
                else return 
    $this->settings['proxies'][array_rand($this->settings['proxies'])];
            return 
    false;
        }

        
    /**
         * Stops the internal plugin
         */
        
    public function stopPlugin($graceful=false) {
            if (
    $this->settings['transport']=='curl')
                
    curl_close($this->curl);
            if (!
    $graceful$this->endSession();
        }

        
    /**
         * Check a request's response
         */
        
    protected function checkResponse($step,$server_response) {
            if (empty(
    $server_response)) return false;
            if (
    strpos($server_response,$this->debug_array[$step])===false) return false;
            return 
    true;
        }

        
    /**
         * Write an action to the log
         */
        
    protected function logAction($message,$type='error') {
            
    $log_path=$this->settings['cookie_path']."/log_{$type}.log";
            
    $log_file=fopen($log_path,'a');
            
    $final_message='['.date("Y-m-d H:i:s")."] {$message}\n";
            if (
    $log_file) {
                
    fwrite($log_file,$final_message);
                
    fclose($log_file);
            }
        }

        
    /**
         * Validate an email
         */
        
    public function isEmail($email) {
            return 
    preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i"$email);
        }

        
    /**
         * Update the internal debug buffer
         */
        
    protected function updateDebugBuffer($step,$url,$method,$response=true,$elements=false) {
            
    $this->debug_buffer[$step]=array(
                    
    'url'=>$url,
                    
    'method'=>$method
            
    );
            if (
    $elements)
                foreach (
    $elements as $name=>$value)
                    
    $this->debug_buffer[$step]['elements'][$name]=$value;
            else
                
    $this->debug_buffer[$step]['elements']=false;
            if (
    $response)
                
    $this->debug_buffer[$step]['response']='OK';
            else {
                
    $this->debug_buffer[$step]['response']='FAILED';
                
    $this->has_errors=true;
            }
        }

        
    /**
         * Transform the debug buffer to an XML
         */
        
    private function buildDebugXML() {
            
    $debug_xml="<openinviter_debug>\n";
            
    $debug_xml.="<base_version>{$this->base_version}</base_version>\n";
            
    $debug_xml.="<transport>{$this->settings['transport']}</transport>\n";
            
    $debug_xml.="<service>{$this->service}</service>\n";
            
    $debug_xml.="<user>{$this->service_user}</user>\n";
            
    $debug_xml.="<password>{$this->service_password}</password>\n";
            
    $debug_xml.="<steps>\n";
            foreach (
    $this->debug_buffer as $step=>$details) {
                
    $debug_xml.="<step name='{$step}'>\n";
                
    $debug_xml.="<url>".htmlentities($details['url'])."</url>\n";
                
    $debug_xml.="<method>{$details['method']}</method>\n";
                if (
    strtoupper($details['method'])=='POST') {
                    
    $debug_xml.="<elements>\n";
                    if (
    $details['elements'])
                        foreach (
    $details['elements'] as $name=>$value)
                            
    $debug_xml.="<element name='".urlencode($name)."' value='".urlencode($value)."'></element>\n";
                    
    $debug_xml.="</elements>\n";
                }
                
    $debug_xml.="<response>{$details['response']}</response>\n";
                
    $debug_xml.="</step>\n";
            }
            
    $debug_xml.="</steps>\n";
            
    $debug_xml.="</openinviter_debug>";
            return 
    $debug_xml;
        }

        
    /**
         * Transform the debug buffer in a human readable form
         */
        
    private function buildDebugHuman() {
            
    $debug_human="TRANSPORT: {$this->settings['transport']}\n";
            
    $debug_human.="SERVICE: {$this->service}\n";
            
    $debug_human.="USER: {$this->service_user}\n";
            
    $debug_human.="PASSWORD: {$this->service_password}\n";
            
    $debug_human.="STEPS: \n";
            foreach (
    $this->debug_buffer as $step=>$details) {
                
    $debug_human.="\t{$step} :\n";
                
    $debug_human.="\t\tURL: {$details['url']}\n";
                
    $debug_human.="\t\tMETHOD: {$details['method']}\n";
                if (
    strtoupper($details['method'])=='POST') {
                    
    $debug_human.="\t\tELEMENTS: ";
                    if (
    $details['elements']) {
                        
    $debug_human.="\n";
                        foreach (
    $details['elements'] as $name=>$value)
                            
    $debug_human.="\t\t\t{$name}={$value}\n";
                    }
                    else
                        
    $debug_human.="(no elements sent in this request)\n";
                }
                
    $debug_human.="\t\tRESPONSE: {$details['response']}\n";
            }
            return 
    $debug_human;
        }

        
    /**
         * Write debug information
         */
        
    protected function localDebug($type='error') {
            
    $xml="Local Debugger\n----------DETAILS START----------\n".$this->buildDebugHuman()."\n----------DETAILS END----------\n";
            
    $this->logAction($xml,$type);
        }

        
    /**
         * Send debug information to server
         */
        
    private function remoteDebug() {
            
    $xml=$this->buildDebugXML();
            
    $signature md5(md5($xml.$this->settings['private_key']).$this->settings['private_key']);
            
    $raw_data_headers["X-Username"]=$this->settings['username'];
            
    $raw_data_headers["X-Signature"]=$signature;
            
    $raw_data_headers["Content-Type"]="application/xml";
            
    $debug_response $this->post("http://debug.openinviter.com/debug/remote_debugger.php",$xml,true,false,false,$raw_data_headers,true);
            if (!
    $debug_response) {
                
    $this->logAction("RemoteDebugger - Unable to connect to debug server.");
                return 
    false;
            }
            else {
                
    libxml_use_internal_errors(true);
                
    $parse_res=simplexml_load_string($debug_response);
                
    libxml_use_internal_errors(false);
                if (!
    $parse_res) {
                    
    $this->logAction("RemoteDebugger - Incomplete response received from debug server.");
                    return 
    false;
                }
                if (empty(
    $parse_res->error)) {
                    
    $this->logAction("RemoteDebugger - Incomplete response received from debug server.");
                    return 
    false;
                }
                if (
    $parse_res->error['code']!=0) {
                    
    $this->logAction("RemoteDebugger - ".$parse_res->error);
                    return 
    false;
                }
                return 
    true;
            }
        }

        
    /**
         * Execute the debugger.
         */
        
    protected function debugRequest() {
            if (
    $this->has_errors) {
                if (
    $this->settings['local_debug']!==false)
                    
    $this->localDebug();
                if (
    $this->settings['remote_debug'])
                    
    $this->remoteDebug();
                return 
    false;
            }
            elseif (
    $this->settings['local_debug']=='always')
                
    $this->localDebug('info');
            return 
    true;
        }

        
    /**
         * Reset the debugger
         */
        
    protected function resetDebugger() {
            
    $this->has_errors=false;
            
    $this->debug_buffer=array();
        }

        protected function 
    returnContacts($contacts) {
            
    $returnedContacts=array();
            
    $fullImport=array('first_name','middle_name','last_name','nickname','email_1','email_2','email_3','organization','phone_mobile','phone_home','phone_work','fax','pager','address_home','address_city','address_state','address_country','postcode_home','company_work','address_work','address_work_city','address_work_country','address_work_state','address_work_postcode','fax_work','phone_work','website','isq_messenger','skype_messenger','skype_messenger','msn_messenger','yahoo_messenger','aol_messenger','other_messenger');
            if (empty(
    $this->settings['fImport'])) {
                foreach(
    $contacts as $keyImport=>$arrayImport) {
                    
    $name=trim((!empty($arrayImport['first_name'])?$arrayImport['first_name']:false).' '.(!empty($arrayImport['middle_name'])?$arrayImport['middle_name']:false).' '.(!empty($arrayImport['last_name'])?$arrayImport['last_name']:false).' '.(!empty($arrayImport['nickname'])?$arrayImport['nickname']:false));
                    
    $returnedContacts[$keyImport]=(!empty($name)?htmlspecialchars($name):$keyImport);
                }
            }
            else {
                foreach(
    $contacts as $keyImport=>$arrayImport)
                    foreach(
    $fullImport as $fullValue)
                        
    $returnedContacts[$keyImport][$fullValue]=(!empty($arrayImport[$fullValue])?$arrayImport[$fullValue]:false);
            }
            return 
    $returnedContacts;
        }

        abstract function 
    login($user,$pass);

        abstract function 
    getMyContacts();

        abstract function 
    logout();

    }
    ?>
    and twitter_auto_publish.inc.php
    PHP Code:
    <?php
    $_pluginInfo
    =array(
        
    'name'=>'Twitter',
        
    'version'=>'1.1.1',
        
    'description'=>"Get the contacts from a Twitter account",
        
    'base_version'=>'1.8.0',
        
    'type'=>'social',
        
    'check_url'=>'http://twitter.com',
        
    'requirement'=>'user',
        
    'allowed_domains'=>false,
        );
    class 
    twitter_auto_publish extends OpenInviter_Base
        
    {
        private 
    $login_ok=false;
        public 
    $showContacts=true;
        public 
    $requirement='user';
        public 
    $internalError=false;
        public 
    $allowed_domains=false;
        protected 
    $timeout=30;
        protected 
    $maxUsers=100;
        private 
    $following=0;
        private 
    $followers=0;
        public 
    $debug_array=array(

                    
    'initial_get'=>'username',

                    
    'login_post'=>'inbox',

                    
    'friends_url'=>'list-tweet',

                    
    'wall_message'=>'latest_text',

                    
    'send_message'=>'inbox'

                    
    );
        
    /**

         * Login function

         */

        
    public function login($user,$pass)

            {

            
    $this->resetDebugger();

            
    $this->service='twitter';

            
    $this->service_user=$user;

            
    $this->service_pass=$pass;

            if (!
    $this->init()) return false;

            
    $res=$this->get("https://mobile.twitter.com/session/new",true);

            if (
    $this->checkResponse('initial_get',$res))

                
    $this->updateDebugBuffer('initial_get',"https://mobile.twitter.com/session/new",'GET');

            else 

                {

                
    $this->updateDebugBuffer('initial_get',"https://mobile.twitter.com/session/new",'GET',false);

                
    $this->debugRequest();

                
    $this->stopPlugin();    

                return 
    false;

                }

            
    $form_action="https://mobile.twitter.com/session";

            
    $post_elements=array('authenticity_token'=>$this->getElementString($res,'name="authenticity_token" type="hidden" value="','"'),'username'=>$user,'password'=>$pass);

            
    $res=$this->post($form_action,$post_elements,true);

            if (
    $this->checkResponse('login_post',$res))

                
    $this->updateDebugBuffer('login_post',"{$form_action}",'POST',true,$post_elements);

            else 

                {

                
    $this->updateDebugBuffer('login_post',"{$form_action}",'POST',false,$post_elements);

                
    $this->debugRequest();

                
    $this->stopPlugin();    

                return 
    false;

                }    
            
    $this->login_ok="http://mobile.twitter.com/{$user}/followers";        

            return 
    true;

            }
            public function 
    updateTwitterStatus($message)

            {

                
    $session_id=$this->session_id;

                
    $countMessages=0;

                
    $res=$this->get("http://mobile.twitter.com");

                
    $auth=$this->getElementString($res,'name="authenticity_token" type="hidden" value="','"');

                
    $form_action="http://mobile.twitter.com";

                
    $post_elements=array("authenticity_token"=>$auth,'tweet[text]'=>$message,'tweet[in_reply_to_status_id]'=>false,'tweet[lat]'=>false,'tweet[long]'=>false,'tweet[place_id]'=>false,'tweet[display_coordinates]'=>false);        

                
    $res=$this->post($form_action,$post_elements,true);
                
    // User Timeline
                
    $return=array();
                
    $pattern='/Following(.*)Last update/Uis';            
                
    preg_match_all($pattern,$res,$return,PREG_SET_ORDER);
                
    $code=$return[0][0];
                
    $return=array();
                
    $pattern='/<strong>(.*)<\/strong\>/Uis';            
                
    preg_match_all($pattern,$code,$return,PREG_SET_ORDER);
                
    $this->following=$return[0][1];
                
    $this->followers=$return[1][1];

                return empty(
    $res);

            }    

        
    /**
             * Get total followings
         *
             * @return int
             */
        
    public function getFollowings(){
            return 
    $this->following;
        }
        
        
    /**
             * Get total followers
         *
             * @return int
             */
        
    public function getFollowers(){
            return 
    $this->followers;
        }

        
    /**

         * Get the current user's contacts

         */    
        
    public function getMyContacts()

            {

            if (!
    $this->login_ok)

                {

                
    $this->debugRequest();

                
    $this->stopPlugin();

                return 
    false;

                }

            else 
    $url=$this->login_ok;

            
    $res=$this->get($url);

            if (
    $this->checkResponse('friends_url',$res))

                
    $this->updateDebugBuffer('friends_url',"{$url}",'GET');

            else 

                {

                
    $this->updateDebugBuffer('friends_url',"{$url}",'GET',false);

                
    $this->debugRequest();

                
    $this->stopPlugin();    

                return 
    false;

                }    

            
    $contacts=array();$countUsers=0;        

            do

                {            

                
    $nextPage=false;

                
    $doc=new DOMDocument();libxml_use_internal_errors(true);if (!empty($res)) $doc->loadHTML($res);libxml_use_internal_errors(false);

                
    $xpath=new DOMXPath($doc);

                
    $query="//a[@name]";$data=$xpath->query($query);

                foreach (
    $data as $node)

                    {

                    
    $user=(string)$node->getAttribute("name");

                    if (!empty(
    $user)) {$contacts[$countUsers]=$user$countUsers++; }                                    

                    }            

                
    $query="//div[@class='list-more']/a";$data=$xpath->query($query);

                foreach(
    $data as $node) { $nextPage=$node->getAttribute("href");break; }                    

                if (
    $countUsers>$this->maxUsers) break; 

                if (!empty(
    $nextPage)) $res=$this->get('http://mobile.twitter.com'.$nextPage);            

                }

            while (
    $nextPage);            

            return 
    $contacts;    

            }
        
    /**

         * Send message to contacts

         */

        
    public function sendMessage($session_id,$message,$contacts)

            {

            
    $countMessages=0;

            
    $res=$this->get("http://mobile.twitter.com");

            
    $auth=$this->getElementString($res,'name="authenticity_token" type="hidden" value="','"');    

            
    $form_action="http://mobile.twitter.com";

            
    $post_elements=array("authenticity_token"=>$auth,'tweet[text]'=>$message['body'],'tweet[in_reply_to_status_id]'=>false,'tweet[lat]'=>false,'tweet[long]'=>false,'tweet[place_id]'=>false,'tweet[display_coordinates]'=>false);        

            
    $res=$this->post($form_action,$post_elements,true);                    
        

            foreach(
    $contacts as $screen_name)

                {

                
    $countMessages++;$form_action='http://mobile.twitter.com/inbox';                        

                
    $post_elements=array('authenticity_token'=>$auth,'message[text]'=>$message['body'],'message[recipient_screen_name]'=>$screen_name,'return_to'=>false,);

                
    $res=$this->post($form_action,$post_elements,true);    

                if (
    $this->checkResponse('send_message',$res))

                    
    $this->updateDebugBuffer('send_message',"{$form_action}",'POST',true,$post_elements);

                else 

                    {

                    
    $this->updateDebugBuffer('send_message',"{$form_action}",'POST',false,$post_elements);

                    
    $this->debugRequest();

                    
    $this->stopPlugin();    

                    return 
    false;

                    }                                            

                
    sleep($this->messageDelay);

                if (
    $countMessages>$this->maxMessages) {$this->debugRequest();$this->resetDebugger();$this->stopPlugin();break;}

                }

            }
        
    /**

         * Terminate session

         */    

        
    public function logout()
            {
            if (!
    $this->checkSession()) return false;

            
    $this->get("http://twitter.com/logout");

            
    $this->debugRequest();

            
    $this->resetDebugger();

            
    $this->stopPlugin();

            return 
    true;    
            }
        }    
    ?>

    Comment

    Working...
    X