<?php header(‘Content-type: text/html; charset=utf-8′); $sid = qqLogin(‘发送信息的QQ’,'发送信息的QQ密码’); //登录帐号 sendMsg($sid,’接收信息的QQ’,'聊天内容’); //发送消息 $data = getMsg($sid,’接收信息的QQ’); //获取和某个帐号的聊天记录 if(!empty($data)){ foreach($data as $v){ echo $v['title'].’<br />’; echo $v['msg'].’<br />’; echo ‘————————<br />’; } } function qqLogin($qq_num,$qq_pwd){ //登录帐号 $data = httpGet(‘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["loginTitle"] = ‘手机腾讯网’; $params["bid"] = 0; $params["qq"] = $qq_num; $params["pwd"] = $qq_pwd; $params["loginType"] =1; $data = httpPost($action, $params,1); $action = preg_match(“/sid=(.+?)&/”, $data, $matches); $sid = $matches[1]; if($sid){ return $sid; }else { echo ‘登录失败’; } } function sendMsg($sid,$to_num,$msg){ //发送消息 $params = array(); $params["msg"] = $msg; $params["u"] = $to_num; $params["saveURL"] = 0; $params["do"] = “send”; $params["on"] = 1; $params["aid"] = “发送”; $url = “http://q16.3g.qq.com/g/s?sid=” . $sid; $data = httpPost($url, $params); if(preg_match(‘/消息发送成功/’,$data)) echo ‘发送成功<br />’; else echo ‘发送失败’; } function getMsg($sid,$qq_num) { //获取消息 $url = “http://q16.3g.qq.com/g/s?sid=” . $sid . “&u=” . $qq_num . “&saveURL=0&aid=nqqChat”; $data = httpGet($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),$m); foreach($m[1] as $k=>$v){ $tmp['title']=$v; $tmp['msg']=$m[2][$k]; $r[]=$tmp; } return $r; } function httpGet($url,$header=0){ //GET请求 $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 curlRun($opt); } function httpPost($url,$data,$header=0){ //POST请求 $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 curlRun($opt); } function curlRun($opt){ $ch = curl_init(); curl_setopt_array($ch,$opt); $r = curl_exec($ch); curl_close($ch); return $r; } ?>