使用PHP将消息发送到Yahoo IM

时间:2009-05-02 21:33:52

标签: php yahoo-messenger

我正在尝试连接到Yahoo Messenger帐户并使用PHP或CURL发送PM。 我从phpclasses.org下载了以下类,它连接到eBuddy Web Messenger(http://www.ebuddy.com/)以发送消息到所需的IM 原始代码不起作用,所以我做了一些更改(原始语句被注释掉了),仍然没有运气。

如果有人帮助我弄清楚我在哪里出错,我真的很感激。

感谢。

<?php

class My_Yahoo_Messenger {

    /**
    * Server host
    * @access private
    */
    var $serverHost = 'denver.ebuddy.com';

    /**
    * Login user name
    * @access private
    */
    var $username   = null;

    /**
    * Login session
    * @access private
    */
    var $_cookie    = null;

    /**
    *Connection handle
    * @access private
    */
    var $fp_handle  = null;

    /**
    * Construct
    *@params:
    *   username: String Yahoo account user name
    *   password: String Yahoo account password
    *   login status: Boolean true from visible
    * @access public
    */
    function __construct($username, $password, $inv = false)
    {
        $this->login($username, $password, $inv);
    }

    /**
    * Construct PHP 4
    *@params:
    *   username: String Yahoo account user name
    *   password: String Yahoo account password
    *   login status: Boolean true from visible
    * @access public
    */
    function My_Yahoo_Messenger($username, $password, $inv = false)
    {
        $this->__construct($username, $password, $inv);
    }

    /**
    * Function provide login to yahoo account
    *@params:
    *   username: String Yahoo account user name
    *   password: String Yahoo account password
    *   login status: Boolean true from visible
    *@return: String cookie data
    * @access public
    */
    function login($username, $password, $inv = false)
    {
        $inv = $inv === false ? 'FALSE' : 'TRUE';
        $username = urlencode($username);
        $password = urlencode($password);

        $content = "password=$password&network=yahoo&initial=FALSE&login_network=yahoo&username=$username&pwd=&init_status=$inv";

        //$this->_createConnection('POST', '/vo074922/login.jsp', 'http://www.ebuddy.com/?web_messenger', "yahoo_uname=$username; Emessenger=yahoo; network=YAHOO;", $content, false);

        $this->_createConnection('POST', '/vo7.3.11/start.html', 'http://www.ebuddy.com/?web_messenger', "yahoo_uname=$username; Emessenger=yahoo; network=YAHOO;", $content, false);

        $headers = array();
        $content = '';

        $atStart = true;
        $atHeader= true;
        while (!feof($this->fp_handle)) {
            $line = fgets($this->fp_handle, 4096);

            if($atStart) {
                $atStart = false;
                if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/', $line, $m)) {
                    break;
                }
            }
            if ($atHeader) {
                if (trim($line) == '') {
                    $atHeader = false;
                    continue;
                }
                if (!preg_match('/([^:]+):\\s*(.*)/', $line, $m)) {
                    continue;
                }
                $key = strtolower(trim($m[1]));
                $val = trim($m[2]);
                if (isset($headers[$key])) {
                    if (is_array($headers[$key])) {
                        $headers[$key][] = $val;
                    } else {
                        $headers[$key] = array($headers[$key], $val);
                    }
                } else {
                    $headers[$key] = $val;
                }
                continue;
            }
            $content .= $line;
        }

        foreach ($headers as $name=>$header)
        {
            switch (true) {
                case strpos($name, 'location') !== false :
                    if(preg_match('@time\s*=\s*([0-9]+)@i', $header, $m)) {
                        $this->time = $m[1];
                    }
                    if(preg_match('@hash\s*=\s*([a-z0-9]+)@i', $header, $m)) {
                        $this->hash = $m[1];
                    }
                    break;
                case strpos($name, 'set-cookie') !== false :
                    if(is_array($header)) $header = implode(';', $header);
                    $this->_cookie = $header;
                    break;
            }
        }
        @fclose ($this->fp_handle);

        $this->username = $username;

        return $this->_cookie;
    }

    /**
    * Function for send message to fav. yahoo account
    *@params:
    *   to: String Yahoo account
    *   message: String Your message
    * @access public
    */
    function sendMessage($to, $message)
    {
        $message = urlencode($message);

        $message = preg_replace( '!<br.*>!iU', "\n", $message );

        $hash = isset($this->hash) ? $this->hash : '';
        $time = isset($this->time) ? $this->time : time();

        $content = "e_hash=$hash&e_action=send_message&e_user=$to&e_message=$message&e_format=FN%3DVerdana%3B%20EF%3D%3B%20CO%3D000000%3B%20CS%3D0%3B%20PF%3D00%3B%20RL%3D0&e_network=YAHOO&_=";

        //$this->_createConnection('POST', '/dispatch', "http://".$this->serverHost."/vo074922/main.jsp?hash=$hash&u=$this->username&network=YAHOO&time=$time", $this->_cookie, $content, false);

        $this->_createConnection('POST', '/dispatch', "http://".$this->serverHost."/vo7.3.11/main.html?hash=$hash&u=$this->username&network=YAHOO&time=$time", $this->_cookie, $content, false);
    }

    /**
    * Function for get unread  message from yahoo account
    *@return : Array From:Message
    * @access public
    */
    function getMessage()
    {
        $content = "e_format=short&e_action=poll&e_time=0&e_timeout=10000&e_max=100&_=";

        $hash = isset($this->hash) ? $this->hash : '';
        $time = isset($this->time) ? $this->time : time();

        //$this->_createConnection('POST', '/dispatch', "http://".$this->serverHost."/vo074922/main.jsp?hash=$hash&u=$this->username&network=YAHOO&time=$time", null, $content, false);

        $this->_createConnection('POST', '/dispatch', "http://".$this->serverHost."/vo7.3.11/main.html?hash=$hash&u=$this->username&network=YAHOO&time=$time", null, $content, false);

        $reciveMessage = array();

        $atStart = true;
        $atHeader= true;
        while (!feof($this->fp_handle)) {
            $line = fgets($this->fp_handle, 4096);

            if($atStart) {
                $atStart = false;
                if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/', $line, $m)) {
                    break;
                }
            }
            if ($atHeader) {
                if (trim($line) == '') {
                    $atHeader = false;
                    continue;
                }
                if (!preg_match('/([^:]+):\\s*(.*)/', $line, $m)) {
                    continue;
                }
                continue;
            }
            if(strpos($line, 'from') !== false) {
                $from = null; $message = null;
                if(preg_match('@from\s*=\s*([^;]*);?@i', $line, $m)) {
                    $from = $m[1];
                }
                if(preg_match('@msg\s*=\s*([^;]*);?@i', $line, $m)) {
                    $message = $m[1];
                }
                if(!empty($message)) {
                    $reciveMessage[ ] = array($from, $message);
                }
            }
        }
        return $reciveMessage;
    }

    /**
    * Function for create connection
    *@params:
    *   method: String Request method
    *   file: String Request file
    *   referer: String Referer
    *   cookie: String cookie for send by request
    *   content: String data content to post to server
    *   close: Boolean False for keep connection handle and not close it
    * @access public
    */
    function _createConnection($method = 'GET', $file = '/', $referer = null,
                                $cookie = null, $content = null, $close = true)
    {
        $this->fp_handle = @fsockopen ($this->serverHost, 80, $errno, $errstr, 100);
        if(!$this->fp_handle) {
            exit("can not connect to $this->serverHost");
        }

        $method = strtoupper($method);
        if(!in_array($method, array('GET', 'POST')) || is_null($content)) {
            $method = 'GET';
        }

        if(is_null($referer)) {
            $referer = $this->serverHost;
        }

        if(is_null($cookie)) {
            $cookie = $this->_cookie;
        }

        $data = "$method $file HTTP/1.1\n";
        $data.= "Accept: */*\n";
        $data.= "Accept-Language: fa\n";
        $data.= "Referer: $referer/\n";
        $data.= "Content-Type: application/x-www-form-urlencoded; charset=UTF-8\n";
        $data.= "Accept-Encoding: gzip, deflate\n";
        $data.= "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Avant Browser [avantbrowser.com]; .NET CLR 1.1.4322)\n";
        $data.= "Host: $this->serverHost\n";
        $data.= "Connection: Keep-Alive\n";
        $data.= "Cache-Control: no-cache\n";
        $data.= "Cookie: $cookie\n";

        if($method == 'POST') {
            $length = strlen($content);
            $data.= "Content-Length: $length\n\n";
            $data.= $content;
        } else {
            $data.= "\n";
        }

        @fputs ($this->fp_handle, $data);
        if($close) @fclose($this->fp_handle);
    }
}


?>

1 个答案:

答案 0 :(得分:0)

你可能忘了var $ password ='',var $ to ='', 我不确定'因为我不是php的专家......