Heim >php教程 >php手册 >PHP模拟QQ登录,发送和接收信息的例子

PHP模拟QQ登录,发送和接收信息的例子

WBOY
WBOYOriginal
2016-05-25 16:45:161109Durchsuche

下面我们一起来看一个PHP模拟QQ登录,发送和接收信息的例子,希望此例子对各位同学会有所帮助.

PHP实例代码如下:

<?php
header(&#39;Content-type: text/html; charset=utf-8&#39;);
$sid = qqLogin(&#39;发送信息的QQ&#39;, &#39;发送信息的QQ密码&#39;); //登录帐号
sendMsg($sid, &#39;接收信息的QQ&#39;, &#39;聊天内容&#39;); //发送消息
$data = getMsg($sid, &#39;接收信息的QQ&#39;); //获取和某个帐号的聊天记录
if (!emptyempty($data)) {
    foreach ($data as $v) {
        echo $v[&#39;title&#39;] . &#39;<br />&#39;;
        echo $v[&#39;msg&#39;] . &#39;<br />&#39;;
        echo &#39;————————<br />&#39;;
    }
}
function qqLogin($qq_num, $qq_pwd) { //登录帐号
    $data = httpGet(&#39;http://pt.3g.qq.com/&#39;);
    $action = preg_match("/action="( . +) ? "/", $data, $matches);
    $action = $matches[1];
    $params = array();
    $params["login_url"] = &#39;http://pt.3g.qq.com/s?aid=nLogin&#39;;
    $params["sidtype"] = 1;
    $params["loginTitle"] = &#39;手机腾讯网&#39;;
    $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 &#39;登录失败&#39;;
    }
}
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(&#39;/消息发送成功/&#39;, $data)) echo &#39;发送成功<br />&#39;;
    else echo &#39;发送失败&#39;;
}
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(&#39;/<div class="main-module bm-gray">s+<p class="(?:ft-s-gray|ft-s ft-cl2)">(.+?)</p>s+<p>(.+?)</p></div>/s&#39;, str_replace("rn", "", $data) , $m);
    foreach ($m[1] as $k => $v) {
        $tmp[&#39;title&#39;] = $v;
        $tmp[&#39;msg&#39;] = $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 => &#39;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&#39;
    );
    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 => &#39;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&#39;
    );
    return curlRun($opt);
}
function curlRun($opt) {
    $ch = curl_init();
    curl_setopt_array($ch, $opt);
    $r = curl_exec($ch);
    curl_close($ch);
    return $r;
}
?>


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn