PHP版QQチャットボット(ログイン、メッセージ送受信)
01 |
02 | // 不多说了,直接上转载请著名出处 php100.com |
03 | include "http.class.php"; |
04 |
05 | class qq { |
06 |
07 | public $sid; |
08 | public $http; |
09 | public $qq_num; |
10 |
11 | function __construct() { |
12 | $this->http = new http; |
13 | } |
14 |
15 | function login($qq_num, $qq_pwd) { |
16 | $data = $this->http->get("http://pt.3g.qq.com/"); |
17 | $action = preg_match("/action="(.+)?"/", $data, $matches); |
18 | $action = $matches[1]; |
19 | $params = array(); |
20 | $params["login_url"] = "http://pt.3g.qq.com/s?aid=nLogin"; |
21 | $params["sidtype"] = 1; |
22 | $params["loginTitle"] = "手机腾讯网"; |
23 | $params["bid"] = 0; |
24 | $params["qq"] = $qq_num; |
25 | $params["pwd"] = $qq_pwd; |
26 | $params["loginType"] =1; |
27 | $data = $this->http->post($action, http_build_query($params)); |
28 | if(count(explode("验证码",$data))>1){ |
29 | preg_match("/ |
30 | echo $matches[1]; |
31 | exit("需要输入验证码"); |
32 | } |
33 | $action = preg_match("/sid=(.+?)&/", $data, $matches); |
34 | $this->sid = $matches[1]; |
35 | return $this->sid; |
36 | } |
37 |
38 | function sendMsg($to_num, $msg, $sid = 0) { |
39 | $sid = $sid ? $sid : $this->sid; |
40 | if (!$sid) |
41 | exit("sid值未传入进去"); |
42 | $params = array(); |
43 | $params["msg"] = $msg; |
44 | $params["u"] = $to_num; |
45 | $params["saveURL"] = 0; |
46 | $params["do"] = "send"; |
47 | $params["on"] = 1; |
48 | $params["aid"] = "发送"; |
49 | $url = "http://q16.3g.qq.com/g/s?sid=" . $sid; |
50 | $data = $this->http->post($url, http_build_query($params)); |
51 | return $data; |
52 | } |
53 |
54 | function getMsg($qq_num = 0, $sid = 0) { |
55 | $qq_num = $qq_num ? $qq_num : $this->qq_num; |
56 | if (!$qq_num) |
57 | exit("qq_num值未传入进去"); |
58 | $sid = $sid ? $sid : $this->sid; |
59 | if (!$sid) |
60 | exit("sid值未传入进去"); |
61 | $url = "http://q16.3g.qq.com/g/s?sid=" . $sid . "&3G_UIN=" . $qq_num ."&saveURL=0&aid=nqqChat"; |
62 | $data = $this->http->get($url); |
63 | preg_match("/name="u" value="(d+)"/", $data, $matches); |
64 | $result["qq"] = $matches[1]; |
65 | $data = explode(" |
66 | $data = $data[0]; |
67 |
preg_match_all("/ (.+)? /", $data, $matches); |
68 | unset($matches[1][0]); |
69 | $result["content"] = $matches[1]; |
70 | return $result; |
71 | } |
72 | function logout($sid){ |
73 | $url="http://pt.3g.qq.com/s?sid=".$sid."&aid=nLogout"; |
74 |
75 | echo $this->http->get($url); |
76 | } |
77 | } |
01 |
02 |
03 | class http { |
04 |
05 | private $curl; |
06 | public $user_agent = "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"; |
07 |
08 |
09 | public function get($url) { |
10 | $this->curl = curl_init(); |
11 | curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 20); |
12 | curl_setopt($this->curl, CURLOPT_URL, $url); |
13 | curl_setopt($this->curl, CURLOPT_HEADER, 1); |
14 | curl_setopt($this->curl, CURLOPT_USERAGENT, $this->user_agent); |
15 | curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); |
16 | $data = curl_exec($this->curl); |
17 | curl_close($this->curl); |
18 | return $data; |
19 | } |
20 |
21 | public function post($url, $params) { |
22 | $this->curl = curl_init(); |
23 | curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 20); |
24 | curl_setopt($this->curl, CURLOPT_URL, $url); |
25 | curl_setopt($this->curl, CURLOPT_HEADER, 1); |
26 | //curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true); |
27 | curl_setopt($this->curl, CURLOPT_POST, 1); |
28 | curl_setopt($this->curl, CURLOPT_USERAGENT, $this->user_agent); |
29 | curl_setopt($this->curl, CURLOPT_POSTFIELDS, $params); |
30 | curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); |
31 | $data = curl_exec($this->curl); |
32 | curl_close($this->curl); |
33 | return $data; |
34 | } |
35 |
36 | } |
37 |
38 | ?> |
39 | 原文:http://lvxinwei.sinaapp.com/961.html |