├── .gitignore ├── README.md ├── Wechat.php ├── sample.php └── snoopy.php /.gitignore: -------------------------------------------------------------------------------- 1 | /data/cookie.log 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 因为私人原因,所以不再公开其他 2 | 不过基本上思路都在这了 3 | 对微信接口的一层简单封装 4 | 设置Wechat.php 5 | define("TOKEN", "微信密钥"); 6 | define("ACCOUNT", "你的微信公众帐号"); 7 | define("PASSWORD", "你的微信公众密码"); 8 | define("METHOD", "redis或者file"); 9 | 几个参数 10 | 示例代码 11 | 12 | include("Wechat.php"); 13 | include("snoopy.php"); 14 | $wechat = new weChatApi(); 15 | // 回复消息 16 | if($wechat->checkSignature()){ 17 | $return = $wechat->parseData(); 18 | // $reply = $this->reply($return["Content"]); 19 | $reply = '回复内容'; 20 | $wechat->sendText($return["FromUserName"],$return["ToUserName"],"text",$reply); 21 | } 22 | // 主动发消息 23 | $wechat = new weChatApi(); 24 | $wechat->send('1034585',time()); 25 | // 获取用户信息 26 | $data = $wechat->getInfo('1034585'); 27 | var_dump($data); 28 | 增加了cookie文件存储,每次请求都会验证cookie的可用性,可以去掉验证,然后cron每几分钟生成新cookie 29 | 增加了redis存储,可以自主选择 30 | -------------------------------------------------------------------------------- /Wechat.php: -------------------------------------------------------------------------------- 1 | cookie = $this->redisCookie(); 14 | }else{ 15 | $this->cookie = $this->read('cookie.log'); 16 | } 17 | } 18 | 19 | /** 20 | * 检查是否是合理的请求(官方函数) 21 | * @return boolean 22 | */ 23 | public function checkSignature() 24 | { 25 | if($_GET){ 26 | $signature = $_GET["signature"]; 27 | $timestamp = $_GET["timestamp"]; 28 | $nonce = $_GET["nonce"]; 29 | 30 | $token = TOKEN; 31 | $tmpArr = array($token, $timestamp, $nonce); 32 | sort($tmpArr); 33 | $tmpStr = implode( $tmpArr ); 34 | $tmpStr = sha1( $tmpStr ); 35 | if( $tmpStr == $signature ){ 36 | return true; 37 | }else{ 38 | return false; 39 | } 40 | }else{ 41 | return false; 42 | } 43 | } 44 | 45 | /** 46 | * 主动发消息 47 | * @param string $id 用户的fakeid 48 | * @param string $content 发送的内容 49 | * @return [type] [description] 50 | */ 51 | public function send($id,$content) 52 | { 53 | $send_snoopy = new Snoopy; 54 | $post = array(); 55 | $post['tofakeid'] = $id; 56 | $post['type'] = 1; 57 | $post['content'] = $content; 58 | $post['ajax'] = 1; 59 | $send_snoopy->referer = "http://mp.weixin.qq.com/cgi-bin/singlemsgpage?fromfakeid={$id}&msgid=&source=&count=20&t=wxm-singlechat&lang=zh_CN"; 60 | $send_snoopy->rawheaders['Cookie']= $this->cookie; 61 | $submit = "http://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response"; 62 | $send_snoopy->submit($submit,$post); 63 | return $send_snoopy->results; 64 | } 65 | 66 | 67 | /** 68 | * 批量发送(可能需要设置超时) 69 | * @param [type] $ids 用户的fakeid集合,逗号分割 70 | * @param [type] $content [description] 71 | * @return [type] [description] 72 | */ 73 | public function batSend($ids,$content) 74 | { 75 | $ids_array = explode(",", $ids); 76 | $result = array(); 77 | foreach ($ids_array as $key => $value) { 78 | $send_snoopy = new Snoopy; 79 | $post = array(); 80 | $post['type'] = 1; 81 | $post['content'] = $content; 82 | $post['ajax'] = 1; 83 | $send_snoopy->referer = "http://mp.weixin.qq.com/cgi-bin/singlemsgpage?fromfakeid={$value}&msgid=&source=&count=20&t=wxm-singlechat&lang=zh_CN"; 84 | $send_snoopy->rawheaders['Cookie']= $this->cookie; 85 | $submit = "http://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response"; 86 | $post['tofakeid'] = $value; 87 | $send_snoopy->submit($submit,$post); 88 | $tmp = $send_snoopy->results; 89 | array_push($result, $tmp); 90 | } 91 | return $result; 92 | } 93 | 94 | /** 95 | * 获取用户的信息 96 | * @param string $id 用户的fakeid 97 | * @return [type] [description] 98 | */ 99 | public function getInfo($id) 100 | { 101 | $send_snoopy = new Snoopy; 102 | $send_snoopy->rawheaders['Cookie']= $this->cookie; 103 | $submit = "http://mp.weixin.qq.com/cgi-bin/getcontactinfo?t=ajax-getcontactinfo&lang=zh_CN&fakeid=".$id; 104 | $send_snoopy->submit($submit,array()); 105 | $result = json_decode($send_snoopy->results,1); 106 | if(!$result){ 107 | $this->login(); 108 | } 109 | return $result; 110 | } 111 | 112 | /** 113 | * 被动发送内容 114 | * @param [type] $fromUsername [description] 115 | * @param [type] $toUsername [description] 116 | * @param [type] $msgType [description] 117 | * @param [type] $content [description] 118 | * @return [type] [description] 119 | */ 120 | public function sendText($fromUsername,$toUsername,$msgType,$content) 121 | { 122 | $textTpl = " 123 | 124 | 125 | %s 126 | 127 | 128 | 0 129 | "; 130 | $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, $content); 131 | echo $resultStr; 132 | } 133 | 134 | /** 135 | * 解析数据 136 | * @return [type] [description] 137 | */ 138 | public function parseData(){ 139 | $return = array(); 140 | $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; 141 | if (!empty($postStr)){ 142 | $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); 143 | $postObj = json_encode($postObj); 144 | $postObj = json_decode($postObj,1); 145 | return $postObj; 146 | }else { 147 | return $return; 148 | } 149 | } 150 | 151 | /** 152 | * 模拟登录获取cookie 153 | * @return [type] [description] 154 | */ 155 | public function login($locate="file"){ 156 | $snoopy = new Snoopy; 157 | $submit = "http://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN"; 158 | $post["username"] = ACCOUNT; 159 | $post["pwd"] = md5(PASSWORD); 160 | $post["f"] = "json"; 161 | $snoopy->submit($submit,$post); 162 | $cookie = ''; 163 | foreach ($snoopy->headers as $key => $value) { 164 | $value = trim($value); 165 | if(strpos($value,'Set-Cookie: ') || strpos($value,'Set-Cookie: ')===0){ 166 | $tmp = str_replace("Set-Cookie: ","",$value); 167 | $tmp = str_replace("Path=/","",$tmp); 168 | $cookie.=$tmp; 169 | } 170 | } 171 | if($locate == 'file'){ 172 | $this->write("cookie.log",$cookie); 173 | } 174 | return $cookie; 175 | } 176 | 177 | public function redisCookie(){ 178 | $redis = new Redis(); 179 | $redis->pconnect('127.0.0.1', 6379); 180 | if ($redis->exists('cookie')) { 181 | return $redis->get('cookie'); 182 | }else{ 183 | $cookie = $this->login(); 184 | $redis->setex('cookie', 600, $cookie); 185 | return $cookie; 186 | } 187 | } 188 | 189 | 190 | /** 191 | * 把内容写入文件 192 | * @param string $filename 文件名 193 | * @param string $content 文件内容 194 | * @return [type] [description] 195 | */ 196 | public function write($filename,$content){ 197 | $fp= fopen("./data/".$filename,"w"); 198 | fwrite($fp,$content); 199 | fclose($fp); 200 | } 201 | 202 | /** 203 | * 读取文件内容 204 | * @param string $filename 文件名 205 | * @return [type] [description] 206 | */ 207 | public function read($filename){ 208 | if(file_exists("./data/".$filename)){ 209 | $data = ''; 210 | $handle=fopen("./data/".$filename,'r'); 211 | while (!feof($handle)){ 212 | $data.=fgets($handle); 213 | } 214 | fclose($handle); 215 | if($data){ 216 | $send_snoopy = new Snoopy; 217 | $send_snoopy->rawheaders['Cookie']= $data; 218 | $submit = "http://mp.weixin.qq.com/cgi-bin/getcontactinfo?t=ajax-getcontactinfo&lang=zh_CN&fakeid="; 219 | $send_snoopy->submit($submit,array()); 220 | $result = json_decode($send_snoopy->results,1); 221 | if(!$result){ 222 | return $this->login(); 223 | }else{ 224 | return $data; 225 | } 226 | }else{ 227 | return $this->login(); 228 | } 229 | }else{ 230 | return $this->login(); 231 | } 232 | } 233 | 234 | /** 235 | * 验证cookie的有效性 236 | * @return [type] [description] 237 | */ 238 | public function checkValid() 239 | { 240 | $send_snoopy = new Snoopy; 241 | $post = array(); 242 | $submit = "http://mp.weixin.qq.com/cgi-bin/getregions?id=1017&t=ajax-getregions&lang=zh_CN"; 243 | $send_snoopy->rawheaders['Cookie']= $this->cookie; 244 | $send_snoopy->submit($submit,$post); 245 | $result = $send_snoopy->results; 246 | if(json_decode($result,1)){ 247 | return true; 248 | }else{ 249 | return false; 250 | } 251 | } 252 | 253 | } 254 | -------------------------------------------------------------------------------- /sample.php: -------------------------------------------------------------------------------- 1 | checkSignature()){ 11 | $return = $wechat->parseData(); 12 | $reply = '回复内容'; 13 | $wechat->sendText($return["fromUsername"],$return["toUsername"],"text",$reply); 14 | } 15 | // 主动发消息 16 | $wechat->send('1034585',time()); 17 | // 批量发送信息 18 | $wechat->batSend('1034585,1034586',time()); 19 | // 获取用户信息 20 | $data = $wechat->getInfo('1034585'); 21 | var_dump($data); -------------------------------------------------------------------------------- /snoopy.php: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zscorpio/weChat/de6363730d53340ee53a8fb238effa6a1841413f/snoopy.php --------------------------------------------------------------------------------