1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
| <?php
namespace Mirai;
use Exception;
/** * Mirai QQ机器人 * @param string $_url MiraiHTTP接口地址 * @param string $_authKey Mirai认证Key * @param string $_sessionKey Mirai会话Key */ class Bot { private $_url; private $_authKey; private $_sessionKey;
/** * 构造函数 * @param string $url MiraiHTTP接口地址 * @param string $authKey Mirai认证Key */ function __construct(string $url, string $authKey) { $this->_url = $url; $this->_authKey = array('authKey' => $authKey); }
/** * cURL获取数据 * @param string $url 发送请求的链接 * @param int $ifPost 是否为post请求(1||0) * @param mixed $postFields post的数据 * @param string $cookie 发送请求携带的cookie * @param mixed $cookieFile cookie文件 * @param int $ifHeader 是否获取响应头信息(1||0) * @throws Exception 请求失败 * @return mixed 响应结果 */ public function httpRequest(string $url, int $ifPost = 0, $postFields = '', string $cookie = '', $cookieFile = '', int $ifHeader = 0) { // 模拟http请求header头 $header = array( "Connection: Keep-Alive", "Accept: text/html, application/xhtml+xml, */*", "Pragma: no-cache", "Accept-Language: zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3", "User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)" );
// 初始化一个cURL会话 $ch = curl_init();
// 设置cURL传输选项 curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, $ifHeader); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); $ifPost && curl_setopt($ch, CURLOPT_POST, $ifPost); $ifPost && curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $cookie && curl_setopt($ch, CURLOPT_COOKIE, $cookie); // 发送cookie变量 $cookieFile && curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile); // 发送cookie文件 $cookieFile && curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile); // 写入cookie到文件 curl_setopt($ch, CURLOPT_TIMEOUT, 60); // 允许执行的最长秒数 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// 执行cURL会话 $result = curl_exec($ch);
// 失败则抛出异常 if ($result === false) { throw new Exception('Sending request to ' . $url . ' failed!'); } // 关闭 cURL 会话 curl_close($ch);
// 释放$ch unset($ch);
return $result; }
/** * 进行认证 * @throws Exception 认证失败 * @return bool */ public function auth(): bool { $url = $this->_url . '/auth'; $postData = json_encode($this->_authKey); $response = json_decode($this->httpRequest($url, 1, $postData)); if ($response !== false && $response->code === 0) { $this->_sessionKey = $response->session; return true; } else { throw new Exception('Mirai authentication failed!'); } }
/** * 校验Session * @param int $qq Session将要绑定的Bot的qq号 * @throws Exception 校验Session失败 * @return bool */ public function verify(int $qq): bool { $url = $this->_url . '/verify'; $postData = json_encode( array( 'sessionKey' => $this->_sessionKey, 'qq' => $qq ) ); $response = json_decode($this->httpRequest($url, 1, $postData)); if ($response !== false && $response->code === 0) { return true; } else { throw new Exception('Validation session failed!' . 'The qq is ' . $qq . '.'); } }
/** * 释放Session * @param int $qq 与该Session绑定Bot的QQ号码 * @throws Exception 释放Session失败 * @return bool */ public function release(int $qq): bool { $url = $this->_url . '/release'; $postData = json_encode( array( 'sessionKey' => $this->_sessionKey, 'qq' => $qq ) ); $response = json_decode($this->httpRequest($url, 1, $postData)); if ($response !== false && $response->code === 0) { return true; } else { throw new Exception('Failed to release session! The qq is ' . $qq . '!'); } }
/** * 发送好友消息 * @param int $qq 发送消息目标好友的QQ号 * @param array $messageChain 消息链,消息对象构成的数组 * @param int $quote 回复消息的messageId * @throws Exception 发送好友消息失败 * @return int messageId 可引用进行回复 */ public function sendFriendMessage(int $qq, array $messageChain, $quote = null): int { $url = $this->_url . '/sendFriendMessage'; $postData = json_encode( array( 'sessionKey' => $this->_sessionKey, 'target' => $qq, 'quote' => $quote, 'messageChain' => $messageChain ) ); $response = json_decode($this->httpRequest($url, 1, $postData)); if ($response !== false && $response->code === 0) { return $response->messageId; } else { throw new Exception('Failed to send friend message to qq:' . $qq . '!'); } } }
|