Skip to content

Instantly share code, notes, and snippets.

@PizzaLiu
Last active December 16, 2015 15:39
Show Gist options
  • Save PizzaLiu/5457096 to your computer and use it in GitHub Desktop.
Save PizzaLiu/5457096 to your computer and use it in GitHub Desktop.
class qq_msg_sender {
private $qq_num;
private $qq_pw;
private $sid;
public function __construct($qq_num,$qq_pw){
$this->qq_num = $qq_num;
$this->qq_pw = $qq_pw;
}
private function _http_get($url,$header=0){
$opt = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => $header,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 60,
CURLOPT_USERAGENT=>'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.29 Safari/525.13'
);
return $this->_curl_run($opt);
}
private function _http_post($url,$data,$header=0){
$opt = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => $header,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 60,
CURLOPT_POSTFIELDS => http_build_query($data),
CURLOPT_USERAGENT=>'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.29 Safari/525.13'
);
return $this->_curl_run($opt);
}
private function _curl_run($opt){
$ch=curl_init();
curl_setopt_array($ch,$opt);
$r = curl_exec($ch);
curl_close($ch);
return $r;
}
private function login(){
if($this->sid) {
return $this->sid;
}
$data = $this->_http_get('http://pt.3g.qq.com/');
$action = preg_match("/action=\"(.+)?\"/", $data, $matches);
$action = $matches[1];
$params = array();
$params["login_url"] = 'http://pt.3g.qq.com/s?aid=nLogin';
$params["sidtype"] = 1;
$params["qq"] = $this->qq_num;
$params["pwd"] = $this->qq_pw;
$params["loginType"] =1;
$data = $this->_http_post($action, $params,1);
$action = preg_match("/sid=(.+?)&/", $data, $matches);
$sid = $matches[1];
if($sid){
$this->sid = $sid;
return $sid;
} else {
return false;
}
}
/*
** send the meg to $to_num (by $this->qq_num and $this->qq_pw)
*/
public function send_msg($to_num,$msg){
$this->login();
$sid = $this->sid;
$params = array();
$params["msg"] = $msg;
$params["u"] = $to_num;
$params["saveURL"] = 0;
$params["aid"] = "发送";
$url = "http://q16.3g.qq.com/g/s?sid=" . $sid;
$data = $this->_http_post($url, $params);
if(preg_match('/消息发送成功/',$data)) $res = true;
else $res = false;
return $res;
}
/*
** get the talk list between $this->qq_num and $from_num.
** data format : array(
** array(
** 'title'=>'',
** 'msg'=>''
** )
** )
*/
public function get_msg($from_num) {
$this->login();
$sid = $this->sid;
$url = "http://q16.3g.qq.com/g/s?sid=" . $sid . "&u=" . $from_num . "&saveURL=0&aid=nqqChat";
$data = $this->_http_get($url);
preg_match_all('/<div class=\"main-module bm-gray\">\s+<p class=\"(?:ft-s-gray|ft-s ft-cl2)\">(.+?)<\/p>\s+<p>(.+?)<\/p><\/div>/s',str_replace("\r\n","",$data),$match_arr);
$messages = null;
if($match_arr[1]) foreach($match_arr[1] as $k=>$v){
$tmp['title']=$v;
$tmp['msg']=$match_arr[2][$k];
$messages[]=$tmp;
}
return $messages;
}
}
@PizzaLiu
Copy link
Author

user guide:

include('./qq_msg_sender.class.php');
$qq_msg_sender = new qq_msg_sender($qq_num,$qq_pw);
// send a message to $to_qq_num
$qq_msg_sender->send_msg($to_qq_num,$msg);
// get the messages
$message_array = $qq_msg_sender->get_msg($from_qq_num);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment