PHP学会网 php培训网 PHP暑期培训 PHP寒假培训 PHP假期培训 » 入门和基础知识 » 用php聊QQ
自由的龙

该用户目前不在线
级别: 中级程序员
精华: 1
发帖: 1264
威望: 1267 点
金钱: 12660 PYMB
贡献值: 0 点
在线时间:0(小时)
注册时间:2006-04-16
最后登录:2006-06-27

用php聊QQ


我也是最近才看到的,大家可以到网上搜索一下QQ的http协议,实现很简单,我只是稍微试了一下,写了以下几段代码,如果哪位有兴趣可以继续研究。如果有J2ME或者各大手机厂商的SDK,研究一下javaQQ也不错。最近要考试,只能先贴出来,先说明,未全部完成
<?php
class Query {
   
private $timeout;           // = 100; // Max time for stablish the conection
private $server;                 // = '219.133.51.11';//'219.133.51.11';         // IP address
    private $host;                     //   = 'tqq.tencent.com';         // Domain name
    private $port;                     //   = 8000;

    private $postValues;                 // = array ( 'VER' => '1.1',
    private $ret;
                               

   
    public function go(){
         
          $this->postValues = substr( $this->postValues, 0, -1 );

          $request = "POST HTTP/1.1
";
          $request .= "Host: $this->host
";

            $length = strlen( $this->postValues );
            $request .= "Content-Type: application/x-www-form-urlencoded
";
            $request .= "Content-Length: $length
";
            $request .= "
";
            $request .= $this->postValues;
            $socket = fsockopen( $this->server, $this->port, $errno, $errstr, $this->timeout );
            fputs( $socket, $request );
    $ret = '';
    while ( !feof( $socket ) ) {
      $ret .= fgets( $socket, 4096 );
    }
      fclose( $socket );
          $this->setRetValues( $ret );
    }
   
    public function clearParams() {
          $this->postValues = '';
          return true;
    }
   
    public function addParams( $var , $value ) {
          $this->postValues.= urlencode( $var ) . "=" . urlencode( $value ) . '&';
    }
   
public function setTimeout( $timeout ) {
      $this->timeout = $timeout;
      return true;
}
public function setServer( $server ) {
      $this->server = $server;
      return true;
}
public function setHost( $host ) {
      $this->host = $host;
      return true;
}
public function setPort( $port ) {
      $this->port = $port;
      return true;
}

public function getRetValues() {
      parse_str(iconv('UTF-8','GB2312',$this->ret),$arrValues);
      return $arrValues;
      //return $this->ret;
}
     
   
    private function setRetValues( $ret ) {
          $this->ret=$ret;
          return ture;
    }
}
?>
<?php
class QQ {
    private $query;
    private $no;
    private $pass;
   
    public function login() {
          $this->query->clearParams();
          $this->query->addParams('VER','1.1');
          $this->query->addParams('CMD','Login');
          $this->query->addParams('SEQ',rand(1000,9000));
          $this->query->addParams('UIN',$this->no);
          $this->query->addParams('PS',$this->pass);
          $this->query->addParams('M5','1');
          $this->query->addParams('LC','9326B87B234E7235');
          $this->query->go();
          return $this->query->getRetValues();
    }

    public function getFriendList() {
          // VER=1.1&CMD=List&SEQ=&UIN=&TN=160&UN=0
          $this->query->clearParams();
          $this->query->addParams('VER','1.1');
          $this->query->addParams('CMD','List');
          $this->query->addParams('SEQ',rand(1000,9000));
          $this->query->addParams('UIN',$this->no);
          $this->query->addParams('TN','160');
          $this->query->addParams('UN','0');
          //$this->query->addParams('LC','9326B87B234E7235');
          $this->query->go();          
          return $this->query->getRetValues();
    }
   
    public function changeStat($st) {
          $this->query->clearParams();
          $this->query->addParams('VER','1.1');
          $this->query->addParams('CMD','Change_Stat');
          $this->query->addParams('SEQ',rand(1000,9000));
          $this->query->addParams('UIN',$this->no);
          $this->query->addParams('ST',$st);
          $this->query->go();          
          return $this->query->getRetValues();
          //VER=1.1&CMD=Change_Stat&SEQ=&UIN=&ST=
          //ST为要改变的状态,10为上线,20为离线,30为忙碌。
    }

    public function getMsg() {
    //VER=1.1&CMD=GetMsgEx&SEQ=&UIN=
          $this->query->clearParams();
          $this->query->addParams('VER','1.1');
          $this->query->addParams('CMD','GetMsgEx');
          $this->query->addParams('SEQ',rand(1000,9000));
          $this->query->addParams('UIN',$this->no);
          $this->query->go();          
          return $this->query->getRetValues();                          
    }
   
    public function getUserInfo($user_no) {
          // VER=1.1&CMD=GetInfo&SEQ=&UIN=&LV=2&UN=
          $this->query->clearParams();
          $this->query->addParams('VER','1.1');
          $this->query->addParams('CMD','GetInfo');
          $this->query->addParams('SEQ',rand(1000,9000));
          $this->query->addParams('UIN',$this->no);
          $this->query->addParams('LV','2');
          $this->query->addParams('UN',$user_no);
          $this->query->go();          
          echo time().' '.(double)microtime(true).'<br>';
          //echo $user_no.'ok';
          //print_r( $this->query->getRetValues());                          
    }
   
    public function setQQInfo( $no , $pass ) {
          $this->no = $no;
          $this->pass = md5($pass);
          return true;
    }
   

   
    public function setQuery($query) {
          $this->query = $query;
          return true;
    }

}
?>
顶端 Posted: 2006-04-16 15:28 | [楼 主]
PHP学会网 php培训网 PHP暑期培训 PHP寒假培训 PHP假期培训 » 入门和基础知识

时:01-08 20:50 Copyright © 2006 phpwhy.com 权
ICP05060669

曳息 -