下面我们一起来看一个PHP模拟QQ登录,发送和接收信息的例子,希望此例子对各位同学会有所帮助.
PHP实例代码如下:
- <?php
- header(‘Content-type: text/html; charset=utf-8′);
- $sid = qqLogin(‘发送信息的QQ’,'发送信息的QQ密码’);
- sendMsg($sid,’接收信息的QQ’,'聊天内容’);
-
- $data = getMsg($sid,’接收信息的QQ’);
-
- if(!emptyempty($data)){
- foreach($data as $v){
- echo $v['title'].’<br />’;
- echo $v['msg'].’<br />’;
- echo ‘————————<br />’;
- }
- }
-
- function qqLogin($qq_num,$qq_pwd){
- $data = httpGet(‘http:
- $action = preg_match(“/action=”(.+)?”/”, $data, $matches);
- $action = $matches[1];
- $params = array();
- $params["login_url"] = ‘http:
- $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:
- $data = httpPost($url, $params);
- if(preg_match(‘/消息发送成功/’,$data)) echo ‘发送成功<br />’;
- else echo ‘发送失败’;
- }
-
- function getMsg($sid,$qq_num) {
- $url = “http:
- $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){
- $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){
- $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;
- }
-
- ?>
|