问题描述:在做微信公众号开发的时候,第一次看见微信开发文档 简直就是一脸懵逼啊!都什么跟什么啊,请看下面详解:
1.微信开发需要启用开启开发者模式(不懂的自行百度)
2.填写服务器地址,token
3.服务器配置:
1 <?php 2 3 class Test{ 4 private $token = ‘4512c35162db71cb7ecee5bc5cd65978‘; 5 private $weixin_token = null; 6 /** 7 * @desc 验证微信接入 8 * @author wzh 9 * @qq646943067 10 */ 11 private function valid(){ 12 $echoStr = $_GET["echostr"]; 13 if($this->checkSignature()){ 14 echo $echoStr; 15 exit; 16 } 17 } 18 /** 19 * @desc 验证签名 20 * @author wzh 21 */ 22 private function checkSignature(){ 23 if (! $this -> token) { 24 throw new Exception(‘TOKEN is not defined!‘); 25 } 26 27 $signature = $_GET["signature"]; 28 $timestamp = $_GET["timestamp"]; 29 $nonce = $_GET["nonce"]; 30 $token = $this -> token; 31 32 $tmpArr = array($token, $timestamp, $nonce); 33 // use SORT_STRING rule 34 sort($tmpArr, SORT_STRING); 35 $tmpStr = implode( $tmpArr ); 36 $tmpStr = sha1( $tmpStr ); 37 38 if( $tmpStr == $signature ){ 39 return true; 40 }else{ 41 return false; 42 } 43 } 44 45 /** 46 * 处理xml数据 47 * 48 */ 49 private function transmitText($object, $content, $flag = 0) 50 { 51 $textTpl = "<xml> 52 <ToUserName><![CDATA[%s]]></ToUserName> 53 <FromUserName><![CDATA[%s]]></FromUserName> 54 <CreateTime>%s</CreateTime> 55 <MsgType><![CDATA[text]]></MsgType> 56 <Content><![CDATA[%s]]></Content> 57 <FuncFlag>%d</FuncFlag> 58 </xml>"; 59 $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag); 60 return $resultStr; 61 } 62 63 //回复图文消息 64 private function transmitNews($object, $newsArray) 65 { 66 if(!is_array($newsArray)){ 67 return; 68 } 69 $itemTpl = "<item> 70 <Title><![CDATA[%s]]></Title> 71 <Description><![CDATA[%s]]></Description> 72 <PicUrl><![CDATA[%s]]></PicUrl> 73 <Url><![CDATA[%s]]></Url> 74 </item>"; 75 $item_str = ""; 76 foreach ($newsArray as $item){ 77 $item_str .= sprintf($itemTpl, $item[‘Title‘], $item[‘Description‘], $item[‘PicUrl‘], $item[‘Url‘]); 78 } 79 $xmlTpl = "<xml> 80 <ToUserName><![CDATA[%s]]></ToUserName> 81 <FromUserName><![CDATA[%s]]></FromUserName> 82 <CreateTime>%s</CreateTime> 83 <MsgType><![CDATA[news]]></MsgType> 84 <ArticleCount>%s</ArticleCount> 85 <Articles> 86 $item_str</Articles> 87 </xml>"; 88 89 $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray)); 90 return $result; 91 } 92 93 94 //post请求 95 public function http_request($url, $data){ 96 $curl = curl_init(); 97 curl_setopt($curl, CURLOPT_URL, $url); 98 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 99 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); 100 if(!empty($data)){ 101 curl_setopt($curl, CURLOPT_POST, 1); 102 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 103 104 } 105 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 106 $output = curl_exec($curl); 107 curl_close($curl); 108 return $output; 109 } 110 111 112 /** 113 * @desc 微信接入 114 * @author wzh 115 * @qq 646943067 116 */ 117 public function weixin(){ 118 $this -> valid(); //如果已经验证通过 则把该行注释 119 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; 120 $this -> responseMsg($postStr); 121 } 122 123 /** 124 * 接收推送消息 125 * 126 */ 127 public function responseMsg($postStr){ 128 if (!empty($postStr)){ 129 $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA); 130 $RX_TYPE = trim($postObj->MsgType); 131 switch ($RX_TYPE) 132 { 133 case "text": 134 $resultStr = $this->receiveText($postObj); /*文本消息*/ 135 break; 136 case "image": 137 $resultStr = $this->receiveImage($postObj); /*图片消息*/ 138 break; 139 case "location": 140 $resultStr = $this->receiveLocation($postObj);/*地理位置消息*/ 141 break; 142 case "voice": 143 $resultStr = $this->receiveVoice($postObj);/*音频消息*/ 144 break; 145 case "video": 146 $resultStr = $this->receiveVideo($postObj);/*视频消息*/ 147 break; 148 case "link": 149 $resultStr = $this->receiveLink($postObj);/*连接消息*/ 150 break; 151 case "event": 152 $resultStr = $this->receiveEvent($postObj);/*事件消息*/ 153 break; 154 default: 155 $resultStr = "unknow msg type: ".$RX_TYPE;/*未知类型消息*/ 156 break; 157 } 158 echo $resultStr; 159 }else { 160 echo ""; 161 exit; 162 } 163 } 164 165 166 /** 167 * 文本消息 168 */ 169 private function receiveText($object){ 170 /*用户输入的内容*/ 171 $funcFlag = 0; 172 $content = $contentStr = $object->Content ; 173 /* 在这里书写 你的项目逻辑 */ 174 $str = ‘‘; 175 if($content == ‘联系方式‘){ 176 $str = ‘646943067‘; 177 } 178 /* 通过逻辑后 得到的字符串 后处理成xml格式 发送给微信*/ 179 $resultStr = $this->transmitText($object, $contentStr, $funcFlag); 180 return $resultStr; 181 } 182 /** 183 * 图片消息 184 * 185 */ 186 private function receiveImage($object,$media_id){ 187 $textTpl = " 188 <xml> 189 <ToUserName><![CDATA[%s]]></ToUserName> 190 <FromUserName><![CDATA[%s]]></FromUserName> 191 <CreateTime>%s</CreateTime> 192 <MsgType><![CDATA[image]]></MsgType> 193 <Image> 194 <MediaId><![CDATA[%s]]></MediaId> 195 </Image> 196 </xml>"; 197 $time = time(); 198 $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, $time, $media_id); 199 200 return $resultStr; 201 } 202 203 /** 204 * 地理位置 205 * 206 */ 207 private function receiveLocation($object){ 208 $funcFlag = 0; 209 $contentStr = "你发送的是位置,纬度为:".$object->Location_X.";经度为:".$object->Location_Y.";缩放级别为:".$object->Scale.";位置为:".$object->Label; 210 $resultStr = $this->transmitText($object, $contentStr, $funcFlag); 211 return $resultStr; 212 } 213 214 /** 215 * 语音消息 216 * 217 */ 218 private function receiveVoice($object){ 219 $funcFlag = 0; 220 $contentStr = "你发送的是语音,媒体ID为:".$object->MediaId; 221 $resultStr = $this->transmitText($object, $contentStr, $funcFlag); 222 return $resultStr; 223 } 224 /** 225 * 视频消息 226 * 227 */ 228 private function receiveVideo($object){ 229 $funcFlag = 0; 230 $contentStr = "你发送的是视频,媒体ID为:".$object->MediaId; 231 $resultStr = $this->transmitText($object, $contentStr, $funcFlag); 232 return $resultStr; 233 } 234 /** 235 * 连接地址 236 * 237 */ 238 private function receiveLink($object){ 239 $funcFlag = 0; 240 $contentStr = "你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url; 241 $resultStr = $this->transmitText($object, $contentStr, $funcFlag); 242 return $resultStr; 243 } 244 /** 245 * 事件消息 246 *@modify by yangyanzhao 247 *@2016-03-19 248 */ 249 private function receiveEvent($object) 250 { 251 $contentStr = ""; 252 253 switch ($object->Event) 254 { 255 case "subscribe": 256 257 $id = (!empty($object->EventKey))?(str_replace("qrscene_","",$object->EventKey)):""; 258 $wxuid = $this -> addwxUser($id,$object -> FromUserName); 259 //file_put_contents(‘/alidata1/www/yatibang/abc.txt‘, var_export($object->EventKey,true),FILE_APPEND); 260 $id = (int) $id; 261 $wx_name = DB::result_first("select wx_name from app_wx_user where wxuid = $id "); 262 if($wx_name){ 263 $contentStr = "嗨,伙伴~您通过【".$wx_name."】来到php深入浅出公众微信"; 264 }else{ 265 $contentStr = "嗨,伙伴~终于把你等来了,我们一起学习吧"; 266 } 267 $this->pushMessage($wxuid); 268 269 break; 270 case "unsubscribe": 271 $contentStr = "取消关注"; 272 break; 273 case "SCAN": 274 //已关注的用户扫码 275 break; 276 case "CLICK": 277 switch ($object->EventKey) 278 { 279 case "tel": 280 $contentStr = "12345678911"; 281 break; 282 case "resolve": 283 $content = array(); 284 $content[] = array( 285 "Title"=>"web前端入门", 286 "Description"=>"", 287 "PicUrl"=>$this -> local . "/images/weixin/moreimage/1.png", 288 "Url" =>"http://www.baidu.com/", 289 ); 290 $content[] = array( 291 "Title"=>"php入门基础知识", 292 "Description"=>"", 293 "PicUrl"=>$this -> local . "/images/weixin/moreimage/2.png", 294 "Url" =>"http://php.net" 295 ); 296 $content[] = array( 297 "Title"=>"框架学习", 298 "Description"=>"", 299 "PicUrl"=>$this -> local . "/images/weixin/moreimage/3.png", 300 "Url" =>"thinkphp.cn" 301 ); 302 if (isset($content[0][‘PicUrl‘])){ 303 $result = $this->transmitNews($object, $content); 304 return $result; 305 } 306 break; 307 308 default: 309 $contentStr = "你点击了: ".$object->EventKey; 310 break; 311 } 312 break; 313 default: 314 $contentStr = "receive a new event: ".$object->Event; 315 break; 316 } 317 $resultStr = $this->transmitText($object, $contentStr); 318 319 return $resultStr; 320 } 321 322 }
4.总结:以上代码逻辑转载 互联网,结合自身逻辑 书写代码