欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

朋友网关于QQ相关的PHP代码(研究QQ的绝佳资料)

程序员文章站 2022-05-20 18:06:04
复制代码 代码如下:

复制代码 代码如下:

<?
/***************************************

****************************************/
error_reporting(e_all ^ e_notice);

require_once( 'http.inc.php' );
require_once( 'class.chinese.php');


//成功2xx
    define( 'qq_return_success',    200 );
    define( 'qq_login_success',    201 );
    define( 'qq_list_none',        202 );
    define( 'qq_addtolist_success',    203 );
    define( 'qq_replyadd_success',    204 );
    define( 'qq_getmsg_none',    205 );

//警告3xx
    define( 'qq_addtolist_needauth',300 );
    define( 'qq_addtolist_refuse',    301 );
    define( 'qq_addtolist_unknown',    302 );

//失败4xx
    define( 'qq_return_failed',    400 );
    define( 'qq_list_error',    401 );
    define( 'qq_getmsg_error',    402 );

//在线状态
    define( 'qq_status_online',    10);
    define( 'qq_status_offline',    20);
    define( 'qq_status_busy',    30);

//血型
    $qq_data_bt = array
        (
=> '',
=> 'a型',
=> 'b型',
=> 'o型',
=> 'ab型',
=> '其他'
        );

//星座
    $qq_data_co = array
        (
=> '',
=> '水瓶座',
=> '双鱼座',
=> '牡羊座',
=> '金牛座',
=> '双子座',
=> '巨蟹座',
=> '狮子座',
=> '处女座',
=> '天秤座',
=> '天蝎座',
=> '射手座',
=> '摩羯座'
        );

//生肖
    $qq_data_sh = array
        (
=> '',
=> '鼠',
=> '牛',
=> '虎',
=> '兔',
=> '龙',
=> '蛇',
=> '马',
=> '羊',
=> '猴',
=> '鸡',
=> '狗',
=> '猪'
        );

//性别
    $qq_data_sx = array
        (
=> '男',
=> '女'
        );

class qqclient
{
    var $uin;
    var $pwd;

    var $server    =    'kconn.tencent.com';
    //备用:219.133.51.11
    var $port    =    21001;
    //备用:8000
    var $httpclient;
    var $chs    =    null;

    function qqclient($uin,$pwd)
    {
        $this->uin = $uin;
        $this->pwd = $pwd;
    }

    function encode($str)
    /*
        说明:把key1=val1&key2=val2格式变为数组
    */
    {
        $arr = explode('&' , $str);
        $return = array();
        foreach($arr as $k=>$v)
        {
            list($key,$val) = explode('=',$v);
            $return[$key] = $val;
            $this->chs = null;
        }
        return $return;
    }

    function utf8_to_gb2312($str)
    {
        $this->chs = new chinese("utf8","gb2312", $str );
        return $this->chs->convertit();
    }

    function gb2312_to_utf8($str)
    {
        $this->chs = new chinese("gb2312","utf8", $str );
        return $this->chs->convertit();
    }

    function query($str)
    {
        $this->httpclient = new http( http_v11, true );
        $this->httpclient->host = 'kconn.tencent.com';
        $this->httpcilent->port = 21001;

        $query = $this->encode($str);
        $status = $this->httpclient->post( '', $query, '' );
        if ( $status == http_status_ok ) {
            return $this->httpclient->get_response_body();
        }
        else
        {
            print_r($this->httpclient);
            return false;
        }
        $this->httpclient->disconnect();
        unset($this->httpclient);
    }

    function split_str($str)
    {
        $arr = explode("," , $str);
        if($arr[count($arr)-1] == null)
        {
            unset($arr[count($arr)-1]);
        }
        return $arr;
    }

    function login()
    {
        //登陆
        //ver=1.1&cmd=login&seq=&uin=&ps=&m5=1&lc=9326b87b234e7235
        $str = "ver=1.1&cmd=login&seq=".rand(1000,9000)."&uin=".$this->uin."&ps=".md5($this->pwd)."&m5=1&lc=9326b87b234e7235";
        $return = $this->encode($this->query($str));
        if($return['res']==0 and $return['uin'] == $this->uin)
        {
            //返回成功
            if($return['rs']==0)
            {
                //登陆成功
                return qq_login_success;
            }
            else
            {
                //登陆失败
                $globals['qq_error_msg'] = $this->utf8_to_gb2312($return['ra']);
                return qq_login_failed;
            }
        }
        else
        {
            //返回失败
            return qq_return_failed;
           
        }
    }

    function getfriendslist()
    {
        //得到好友列表
        //ver=1.1&cmd=list&seq=&uin=&tn=160&un=0
        $str = "ver=1.1&cmd=list&seq=".rand(1000,9000)."&uin=".$this->uin."&tn=160&un=0";
        $return = $this->encode($this->query($str));
        if($return['res']==0 and $return['uin'] == $this->uin)
        {
            //返回成功
            return $this->split_str($return['un']);
        }
        else
        {
            //返回失败
            return qq_return_failed;
           
        }
    }

    function getonlinelist()
    {
        //得到在线好友列表
        //ver=1.1&cmd=query_stat&seq=&uin=&tn=50&un=0
        $str = "ver=1.1&cmd=query_stat&seq=".rand(1000,9000)."&uin=".$this->uin."&tn=50&un=0";
        $return = $this->encode($this->query($str));
        if($return['res']==0 and $return['uin'] == $this->uin)
        {
            //返回成功
            if($return['sn'] > 0)
            {
                //在线好友数>0
                $uns = $this->split_str($return['un']);    //号码列表
                $nks = $this->split_str($return['nk']); //昵称列表
                $sts = $this->split_str($return['st']); //状态列表
                $fcs = $this->split_str($return['fc']); //头像列表
                $error = 0;
                ((count($uns)==count($nks))==(count($sts)==count($fcs)))==(count($nks)==count($sts)) ?
                    $num = count($uns)
                    :
                    $error = 1;
                ;
                if($error == 1) return qq_list_error;
                $arr = array();
                for($i=0;$i<$num;$i++)
                {
                    $arr[] = array(
                        "un" => $uns[$i] ,
                        "nk" => $this->utf8_to_gb2312($nks[$i]) ,
                        "st" => $sts[$i] ,
                        "fc" => $fcs[$i]
                    );
                }
                return ($arr);
            }
            else
            {
                //在线好友数<=0
                return qq_list_none;
            }
           
        }
        else
        {
            //返回失败
            return qq_return_failed;
               
        }
    }

    function getinfo($uin)
    {
        //得到好友信息
        //ad为联系地址,ag为年龄,em为mail,fc为头像,hp为网站,jb为职业,pc为邮编,ph为联系电话,pr为简介,pv为省,rn为真实名称,sc为毕业院校,sx为性别,un为qq号,nk为qq昵称
        //以下注释研究 by hackfan
        //bt为血型,co为星座,ct为城市,cy为国家,mo为移动电话,sh生肖
        //lv为查询的号码(1为精简查询,2为普通查询,3为详细查询)
        //cv未知,id未知(身份证?),mt未知,mv未知,
        //ver=1.1&cmd=getinfo&seq=&uin=&lv=3&un=
        $str = "ver=1.1&cmd=getinfo&seq=".rand(1000,9000)."&uin=".$this->uin."&lv=3&un=".$uin;
        $return = $this->encode($this->query($str));
        if($return['res']==0 and $return['uin'] == $this->uin)
        {
            //返回成功
            $arr = array
                (
                    'ad' => $this->utf8_to_gb2312($return['ad']),        //联系地址
                    'ag' => $this->utf8_to_gb2312($return['ag']),        //年龄
                    'bt' => $return['bt'],        //血型
                    'co' => $return['co'],        //星座
                    'ct' => $this->utf8_to_gb2312($return['ct']),        //城市
                    'cy' => $this->utf8_to_gb2312($return['cy']),        //国家
                    'em' => $this->utf8_to_gb2312($return['em']),        //email
                    'fc' => $return['fc'],        //头像
                    'hp' => $this->utf8_to_gb2312($return['hp']),        //网站
                    'jb' => $this->utf8_to_gb2312($return['jb']),        //职业
                    'mo' => $return['mo'],        //移动电话
                    'pc' => $this->utf8_to_gb2312($return['pc']),        //邮编
                    'ph' => $this->utf8_to_gb2312($return['ph']),        //联系电话
                    'pr' => $this->utf8_to_gb2312($return['pr']),        //简介
                    'pv' => $this->utf8_to_gb2312($return['pv']),        //省
                    'rn' => $this->utf8_to_gb2312($return['rn']),        //真实姓名
                    'sc' => $this->utf8_to_gb2312($return['sc']),        //毕业院校
                    'sh' => $return['sh'],        //生肖
                    'sx' => $return['sx'],        //性别
                    'un' => $return['un'],        //qq号
                    'nk' => $this->utf8_to_gb2312($return['nk'])        //昵称
                );
            return $arr;
        }
        else
        {
            //返回失败
            return qq_return_failed;
               
        }

    }

    function addfriend($uin)
    {
        //添加新好友
        //ver=1.1&cmd=addtolist&seq=&uin=&un=
        $str = "ver=1.1&cmd=addtolist&seq=".rand(1000,9000)."&uin=".$this->uin."&un=".$uin;
        $return = $this->encode($this->query($str));
        if($return['res']==0 and $return['uin'] == $this->uin)
        {
            //返回成功
            switch($return['cd'])
            {
                case 0 :
                    //对方允许任何人加为好友
                    return qq_addtolist_success;
                    break;
                case 1 :
                    //需要验证
                    return qq_addtolist_needauth;
                    break;
                case 3 :
                    //不允许任何人加为好友
                    return qq_addtolist_refuse;
                    break;
                default :
                    //未知的代码
                    return qq_addtolist_unknown;
                    break;
            }
        }
        else
        {
            //返回失败
            return qq_return_failed;
        }
    }

    function replyadd($uin,$type,$msg)
    {
        //回应添加好友
        //ver=1.1&cmd=ack_addtolist&seq=&uin=&un=&cd=&rs=
        //cd为响应状态,cd为0表示“通过验证”。cd为1表示“拒决加为对方为好友”。cd为2表示“为请求对方加为好友”。rs为你要请求的理由
        $str = "ver=1.2&cmd=ack_addtolist&seq=".rand(1000,9000)."&uin=".$this->uin."&un=".$uin."&cd=".$type."&rs=".$this->gb2312_to_utf8($msg);
        $return = $this->encode($this->query($str));
       
        if($return['res']==0 and $return['uin'] == $this->uin)
        {
            //服务器成功得到信息
            return qq_return_success;
        }
        else
        {
            //失败
            return qq_return_failed;           
        }
    }

    function delfriend($uin)
    {
        //删除好友
        //ver=1.1&cmd=delfromlist&seq=&uin=&un=
        $str = "ver=1.1&cmd=delfromlist&seq=".rand(1000,9000)."&uin=".$this->uin."&un=$uin";
        $return = $this->encode($this->query($str));
        if($return['res']==0 and $return['uin'] == $this->uin)
        {
            //服务器成功得到信息
            return qq_return_success;
        }
        else
        {
            //失败
            return qq_return_failed;
        }
    }

    function changestatus($status)
    {
        //改变状态
        //ver=1.1&cmd=change_stat&seq=&uin=&st=
        //st为要改变的状态,10为上线,20为离线,30为忙碌。
        $str = "ver=1.1&cmd=change_stat&seq=".rand(1000,9000)."&uin=".$this->uin."&st=".$status;
        $return = $this->encode($this->query($str));
        if($return['res']==0 and $return['uin'] == $this->uin)
        {
            //服务器成功得到信息
            return qq_return_success;
        }
        else
        {
            //失败
            return qq_return_failed;
        }
    }

    function logout()
    {
        //退出登陆
        //ver=1.1&cmd=logout&seq=&uin=
        $str = "ver=1.1&cmd=logout&seq=".rand(1000,9000)."&uin=".$this->uin;
        $return = $this->encode($this->query($str));
        if($return['res']==0 and $return['uin'] == $this->uin)
        {
            //服务器成功得到信息
            return qq_return_success;
        }
        else
        {
            //失败
            return qq_return_failed;
        }
    }

    function getmsg()
    {
        //获得消息
        //ver=1.1&cmd=getmsgex&seq=&uin=
        //mt表示消息类型,99表示系统消息,9表示用户消息。un表示消息发送来源用户,mg表示发送的消息,mg消息可以表示某些特定的系统含意
        //当mt=99时:mg=10表示用户上线,mg=20表示用户离线,mg=30表示用户忙碌
        $str = "ver=1.1&cmd=getmsgex&seq=".rand(1000,9000)."&uin=".$this->uin;
        $return = $this->encode($this->query($str));
        if($return['res']==0 and $return['uin'] == $this->uin)
        {
            //服务器成功得到信息
            if($return['mn'] > 0)
            {
                //消息数>0
                $mts = $this->split_str($return['mt']);    //消息类型
                $uns = $this->split_str($return['un']); //发送者号码
                $mgs = $this->split_str($return['mg']); //消息内容
                $error = 0;
                (count($mts)==count($uns))==(count($uns)==count($mgs))?
                    $num = count($uns)
                    :
                    $error = 1;
                ;
                if($error == 1) return qq_getmsg_error;    //出差错了
                $arr = array();
                for($i=0;$i<$num;$i++)
                {
                    $arr[] = array(
                        "mt" => $mts[$i] ,
                        "un" => $uns[$i] ,
                        "mg" => $this->utf8_to_gb2312($mgs[$i])
                    );
                }
                return ($arr);
            }
            else
            {
                //在线好友数<=0
                return qq_getmsg_none;
            }
        }
        else
        {
            //失败
            return qq_return_failed;
        }
    }

    function sendmsg($uin,$msg)
    {
        //发送消息
        //ver=1.1&cmd=cltmsg&seq=&uin=&un=&mg=
        $str = "ver=1.1&cmd=cltmsg&seq=".rand(1000,9000)."&uin=".$this->uin."&un=".$uin."&mg=".$this->gb2312_to_utf8($msg);
        $return = $this->encode($this->query($str));
        if($return['res']==0 and $return['uin'] == $this->uin)
        {
            //服务器成功得到信息
            return qq_return_success;
        }
        else
        {
            //失败
            return qq_return_failed;
        }
    }

}
?>