php - 使用 PHP 向 Yahoo IM 发送消息

标签 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);
    }
}


?>

最佳答案

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

关于php - 使用 PHP 向 Yahoo IM 发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/815680/

相关文章:

php - 从不同形式的表更新数据库中的表

javascript - 根据单选按钮选择自动更新 .php 变量的值,无需提交或刷新

PHP 更改图像中的颜色(包括阴影)

PHP Yahoo Messenger API - 我可以在不添加好友的情况下让某人显示图像/头像吗?

python - 有没有适用于 python 的 yahoo Messenger 库?

c++ - 如何用 C++ 制作 Yahoo 10 IM 自动回复程序?

点击 PHP "continue"

php - 如何将 Php::DateInterval 转换为秒?

java - 如何在雅虎通中跟踪数据包!