PHP发送QQ信息封装

最近大家对这个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;
  }
};
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值