<?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(“rn”,”",$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; } ?> |