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


Posted in PHP onJanuary 26, 2015
<?

/***************************************
****************************************/

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;

        }

    }
}

?>
PHP 相关文章推荐
smarty的保留变量问题
Oct 23 PHP
PHPWind 发帖回帖Api PHP版打包下载
Feb 08 PHP
php empty() 检查一个变量是否为空
Nov 10 PHP
PHP编程函数安全篇
Jan 08 PHP
PHP rawurlencode与urlencode函数的深入分析
Jun 08 PHP
关于PHP session 存储方式的详细介绍
Jun 25 PHP
PHP用星号隐藏部份用户名、身份证、IP、手机号等实例
Apr 08 PHP
php使用类继承解决代码重复的问题
Feb 11 PHP
php中get_cfg_var()和ini_get()的用法及区别
Mar 04 PHP
PHP实践教程之过滤、验证、转义与密码详解
Jul 24 PHP
PHP实现的随机红包算法示例
Aug 14 PHP
Laravel中的Blade模板引擎示例详解
Oct 10 PHP
php使用cookie显示用户上次访问网站日期的方法
Jan 26 #PHP
php批量删除cookie的简单实现方法
Jan 26 #PHP
php创建、获取cookie及基础要点分析
Jan 26 #PHP
php表单提交与$_POST实例分析
Jan 26 #PHP
Symfony数据校验方法实例分析
Jan 26 #PHP
symfony表单与页面实现技巧
Jan 26 #PHP
php使用cookie保存登录用户名的方法
Jan 26 #PHP
You might like
php 获得汉字拼音首字母的函数
2009/08/01 PHP
使用HMAC-SHA1签名方法详解
2013/06/26 PHP
php自定义分页类完整实例
2015/12/25 PHP
jQuery News Ticker 基于jQuery的即时新闻行情展示插件
2011/11/05 Javascript
Three.js源码阅读笔记(光照部分)
2012/12/27 Javascript
用jQuery模拟select下拉框的简单示例代码
2014/01/26 Javascript
jquery中子元素和后代元素的区别示例介绍
2014/04/02 Javascript
jQuery实现宽屏图片轮播实例教程
2015/11/24 Javascript
JS中使用apply方法通过不同数量的参数调用函数的方法
2016/05/31 Javascript
JS选取DOM元素的简单方法
2016/07/08 Javascript
jquery在vue脚手架中的使用方式示例
2017/08/29 jQuery
vue 添加vux的代码讲解
2017/11/30 Javascript
webpack公共组件引用路径简化小技巧
2018/06/15 Javascript
JS实现判断数组是否包含某个元素示例
2019/05/24 Javascript
JS实现随机抽选获奖者
2019/11/07 Javascript
详解JavaScript 高阶函数
2020/09/14 Javascript
[34:39]DOTA2上海特级锦标赛主赛事日 - 4 败者组第四轮#1COL VS EG第二局
2016/03/05 DOTA
[01:05]主宰至宝剑心之遗
2017/03/16 DOTA
[55:32]2018DOTA2亚洲邀请赛 4.4 淘汰赛 EG vs LGD 第二场
2018/04/05 DOTA
python 实现文件的递归拷贝实现代码
2012/08/02 Python
在Python中使用模块的教程
2015/04/27 Python
python Opencv将图片转为字符画
2021/02/19 Python
浅谈django orm 优化
2018/08/18 Python
Python实现爬取马云的微博功能示例
2019/02/16 Python
详解python中的index函数用法
2019/08/06 Python
HTML5 canvas基本绘图之绘制线段
2016/06/27 HTML / CSS
英国标志性奢侈品牌:Burberry
2016/07/28 全球购物
应届大学生自荐信
2013/12/05 职场文书
英文自我鉴定
2013/12/10 职场文书
企业负责人任命书
2014/06/05 职场文书
不服从上级领导安排的检讨书
2014/09/14 职场文书
房产授权委托书范本
2014/09/22 职场文书
公司向个人借款协议书范本
2014/10/09 职场文书
2014年体育教师工作总结
2014/12/03 职场文书
2015年幼儿园保育工作总结
2015/05/12 职场文书
人生一定要学会的三样东西:放下、忘记、珍惜
2019/08/21 职场文书