PHP類比QQ登入,發送和接收資訊的例子

來源:互聯網
上載者:User
 代碼如下 複製代碼


<?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;
}

?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.