最近大家对这个QQ发送信息的程序特别感兴趣,于是我就把原来散乱的代码做了个简易的封装。其实也只封装了发送信息而已,其它功能我用不上也懒的封装了,如果有需要自己去分析接口。另外请使用等级高的QQ,消息尽量不要带URL,要不然可能会被要求验证码。
/*******************************************
发送QQ信息PHP接口 2013-08-29
作者:次碳酸钴(admin@web-tinker.com)
构造:
new QQ('QQ号','QQ密码'[,会话ID]);
属性:
username QQ号
passowrd QQ密码
ssid 会话ID
error Array([错误时的服务返回数据])
方法:
send('目标QQ号','信息')
成功返回:true
失败返回:false
login()
成功返回:ssid
失败返回:false
*******************************************/
class QQ {
public $id;
public $password;
public $error=array();
public $ssid;
private $cache;
public function QQ($id,$password,$ssid=null){
$this->id=$id;
$this->password=$password;
touch($this->cache=sys_get_temp_dir().'qq.php.'.md5($id));
$ssid and file_put_contents($this->cache,$ssid);
$this->ssid=file_get_contents($this->cache) or $this->login();
}
public function login(){
$r=QQ::ajax(
'http://pt.3g.qq.com/psw3gqqLogin',
array(
qq=>$this->id,pwd=>$this->password,
loginType=>1,i_p_w=>'qq|pwd|',
toQQchat=>'true',bid_code=>'3GQQ',
aid=>'nLoginHandle'
)
);
if(preg_match('/成功[\s\S]+sid=([^&]+)/',$r,$m)){
file_put_contents($this->cache,$m[1]);
return $this->ssid=$m[1];
}else{
$this->error[]=$r;
return false;
};
}
public function send($qq,$message){
$retry=3;
while($retry--){
$r=QQ::ajax(
'http://q16.3g.qq.com/g/s?sid='.$this->ssid,
array(
msg=>$message,num=>$qq,'do'=>'send',u=>$qq,
'saveURL'=>'0',aid=>'发送QQ消息',on=>1
)
);
if(preg_match('/消息发送成功/',$r))return true;
$this->error[]=$r;
$this->login();
};
return false;
}
private static function ajax($url,$data=null){
curl_setopt($c=curl_init($url),CURLOPT_RETURNTRANSFER,1);
$data and $data=http_build_query($data)
and curl_setopt($c,CURLOPT_POSTFIELDS,$data);
$s=curl_exec($c);
curl_close($c);
return $s;
}
};