首頁  >  文章  >  後端開發  >  用php聊QQ

用php聊QQ

高洛峰
高洛峰原創
2016-12-02 10:41:441195瀏覽

我也是最近才看到的,大家可以上網搜尋QQ的http協議,實作很簡單,我只是稍微試了一下,寫了以下幾段程式碼,如果哪位有興趣可以繼續研究。如果有J2ME或各大手機廠商的SDK,研究一下javaQQ也不錯。最近要考試,只能先貼出來,先說明,未全部完成
代碼:

<?php
class Query {
private $timeout;		// = 100;  // Max time for stablish the conection
private $server;			//  = &#39;219.133.51.11&#39;;//&#39;219.133.51.11&#39;;            // IP address
private $host;				//    = &#39;tqq.tencent.com&#39;;             // Domain name
private $port;				//    = 8000;
private $postValues;			// = array ( &#39;VER&#39; => &#39;1.1&#39;,
private $ret;
public function go(){
$this->postValues = substr( $this->postValues, 0, -1 );
$request  = "POST HTTP/1.1\r\n";
$request .= "Host: $this->host\r\n";
$length = strlen( $this->postValues );
$request .= "Content-Type: application/x-www-form-urlencoded\r\n";
$request .= "Content-Length: $length\r\n";
$request .= "\r\n";
$request .= $this->postValues;
$socket  = fsockopen( $this->server, $this->port, $errno, $errstr, $this->timeout );
fputs( $socket, $request );
$ret = &#39;&#39;;
while ( !feof( $socket ) ) {
$ret .= fgets( $socket, 4096 );
}
fclose( $socket );
$this->setRetValues( $ret );
}
public function clearParams() {
$this->postValues = &#39;&#39;;
return true;
}
public function addParams( $var , $value ) {
$this->postValues.= urlencode( $var ) . "=" . urlencode( $value ) . &#39;&&#39;;
}
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(&#39;UTF-8&#39;,&#39;GB2312&#39;,$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(&#39;VER&#39;,&#39;1.1&#39;);
$this->query->addParams(&#39;CMD&#39;,&#39;Login&#39;);
$this->query->addParams(&#39;SEQ&#39;,rand(1000,9000));
$this->query->addParams(&#39;UIN&#39;,$this->no);
$this->query->addParams(&#39;PS&#39;,$this->pass);
$this->query->addParams(&#39;M5&#39;,&#39;1&#39;);
$this->query->addParams(&#39;LC&#39;,&#39;9326B87B234E7235&#39;);
$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(&#39;VER&#39;,&#39;1.1&#39;);
$this->query->addParams(&#39;CMD&#39;,&#39;List&#39;);
$this->query->addParams(&#39;SEQ&#39;,rand(1000,9000));
$this->query->addParams(&#39;UIN&#39;,$this->no);
$this->query->addParams(&#39;TN&#39;,&#39;160&#39;);
$this->query->addParams(&#39;UN&#39;,&#39;0&#39;);
//$this->query->addParams(&#39;LC&#39;,&#39;9326B87B234E7235&#39;);
$this->query->go();
return $this->query->getRetValues();
}
public function changeStat($st) {
$this->query->clearParams();
$this->query->addParams(&#39;VER&#39;,&#39;1.1&#39;);
$this->query->addParams(&#39;CMD&#39;,&#39;Change_Stat&#39;);
$this->query->addParams(&#39;SEQ&#39;,rand(1000,9000));
$this->query->addParams(&#39;UIN&#39;,$this->no);
$this->query->addParams(&#39;ST&#39;,$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(&#39;VER&#39;,&#39;1.1&#39;);
$this->query->addParams(&#39;CMD&#39;,&#39;GetMsgEx&#39;);
$this->query->addParams(&#39;SEQ&#39;,rand(1000,9000));
$this->query->addParams(&#39;UIN&#39;,$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(&#39;VER&#39;,&#39;1.1&#39;);
$this->query->addParams(&#39;CMD&#39;,&#39;GetInfo&#39;);
$this->query->addParams(&#39;SEQ&#39;,rand(1000,9000));
$this->query->addParams(&#39;UIN&#39;,$this->no);
$this->query->addParams(&#39;LV&#39;,&#39;2&#39;);
$this->query->addParams(&#39;UN&#39;,$user_no);
$this->query->go();
echo time().&#39; &#39;.(double)microtime(true).&#39;<br>&#39;;
//echo $user_no.&#39;ok&#39;;
//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;
}
}
?>


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn