天天看點

微信客服之qq消息提醒

當使用者有消息進入微信公衆平台,讓接口自動回複消息到客服QQ上,達到及時回複的目的

<?php

/*******************************************
發送QQ資訊PHP接口 
*******************************************/

$sendQQ = new QQ('*******','***********');
$sid = $sendQQ->login();
$sendQQ->send('******','資訊',$sid);




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;
    $ssid and file_put_contents($this->cache,$ssid);
  }
    
    
    
  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)){
      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;
  }
};


?>      

繼續閱讀