微信公众平台开发接口PHP SDK完整版

官方提供的SDK只有一个文本消息功能,我们将所有消息的消息类型及事件响应都整理了进来,并且加入日志记录,代码如下:

微信公众平台开发接口PHP SDK完整版
  1 <?php
  2 /*
  3     卓越工作室
  4     CopyRight 2014 All Rights Reserved
  5 */
  6 
  7 define("TOKEN", "weixin");
  8 
  9 $wechatObj = new wechatCallbackapiTest();
 10 if (!isset($_GET[‘echostr‘])) {
 11     $wechatObj->responseMsg();
 12 }else{
 13     $wechatObj->valid();
 14 }
 15 
 16 class wechatCallbackapiTest
 17 {
 18     public function valid()
 19     {
 20         $echoStr = $_GET["echostr"];
 21         if($this->checkSignature()){
 22             echo $echoStr;
 23             exit;
 24         }
 25     }
 26 
 27     private function checkSignature()
 28     {
 29         $signature = $_GET["signature"];
 30         $timestamp = $_GET["timestamp"];
 31         $nonce = $_GET["nonce"];
 32         $token = TOKEN;
 33         $tmpArr = array($token, $timestamp, $nonce);
 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     public function responseMsg()
 46     {
 47         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
 48         if (!empty($postStr)){
 49             $this->logger("R ".$postStr);
 50             $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA);
 51             $RX_TYPE = trim($postObj->MsgType);
 52 
 53             switch ($RX_TYPE)
 54             {
 55                 case "event":
 56                     $result = $this->receiveEvent($postObj);
 57                     break;
 58                 case "text":
 59                     $result = $this->receiveText($postObj);
 60                     break;
 61                 case "image":
 62                     $result = $this->receiveImage($postObj);
 63                     break;
 64                 case "location":
 65                     $result = $this->receiveLocation($postObj);
 66                     break;
 67                 case "voice":
 68                     $result = $this->receiveVoice($postObj);
 69                     break;
 70                 case "video":
 71                     $result = $this->receiveVideo($postObj);
 72                     break;
 73                 case "link":
 74                     $result = $this->receiveLink($postObj);
 75                     break;
 76                 default:
 77                     $result = "unknow msg type: ".$RX_TYPE;
 78                     break;
 79             }
 80             $this->logger("T ".$result);
 81             echo $result;
 82         }else {
 83             echo "";
 84             exit;
 85         }
 86     }
 87 
 88     private function receiveEvent($object)
 89     {
 90         $content = "";
 91         switch ($object->Event)
 92         {
 93             case "subscribe":
 94                 $content = "欢迎关注卓越工作室 ";
 95                 $content .= (!empty($object->EventKey))?("\n来自二维码场景 ".str_replace("qrscene_","",$object->EventKey)):"";
 96                 break;
 97             case "unsubscribe":
 98                 $content = "取消关注";
 99                 break;
100             case "SCAN":
101                 $content = "扫描场景 ".$object->EventKey;
102                 break;
103             case "CLICK":
104                 switch ($object->EventKey)
105                 {
106                     case "COMPANY":
107                         $content = "卓越工作室提供互联网相关产品与服务。";
108                         break;
109                     default:
110                         $content = "点击菜单:".$object->EventKey;
111                         break;
112                 }
113                 break;
114             case "LOCATION":
115                 $content = "上传位置:纬度 ".$object->Latitude.";经度 ".$object->Longitude;
116                 break;
117             default:
118                 $content = "receive a new event: ".$object->Event;
119                 break;
120         }
121         $result = $this->transmitText($object, $content);
122         return $result;
123     }
124 
125     private function receiveText($object)
126     {
127         $keyword = trim($object->Content);
128         switch ($keyword)
129         {
130             case "文本":
131                 $content = "这是个文本消息";
132                 break;
133             case "图文":
134             case "单图文":
135                 $content[] = array("Title"=>"单图文标题", "Description"=>"单图文内容", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
136                 break;
137             case "多图文":
138                 $content[] = array("Title"=>"多图文1标题", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
139                 $content[] = array("Title"=>"多图文2标题", "Description"=>"", "PicUrl"=>"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
140                 $content[] = array("Title"=>"多图文3标题", "Description"=>"", "PicUrl"=>"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
141                 break;
142             case "音乐":
143                 $content = array("Title"=>"最炫民族风", "Description"=>"歌手:凤凰传奇", "MusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3", "HQMusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3");
144                 break;
145             default:
146                 $content = "当前时间:".date("Y-m-d H:i:s",time());
147                 break;
148         }
149         if(is_array($content)){
150             if (isset($content[0][‘PicUrl‘])){
151                 $result = $this->transmitNews($object, $content);
152             }else if (isset($content[‘MusicUrl‘])){
153                 $result = $this->transmitMusic($object, $content);
154             }
155         }else{
156             $result = $this->transmitText($object, $content);
157         }
158         return $result;
159     }
160 
161     private function receiveImage($object)
162     {
163         $content = array("MediaId"=>$object->MediaId);
164         $result = $this->transmitImage($object, $content);
165         return $result;
166     }
167 
168     private function receiveLocation($object)
169     {
170         $content = "你发送的是位置,纬度为:".$object->Location_X.";经度为:".$object->Location_Y.";缩放级别为:".$object->Scale.";位置为:".$object->Label;
171         $result = $this->transmitText($object, $content);
172         return $result;
173     }
174 
175     private function receiveVoice($object)
176     {
177         if (empty($object->Recognition)){
178             $content = array("MediaId"=>$object->MediaId);
179             $result = $this->transmitVoice($object, $content);
180         }else{
181             $content = "你刚才说的是:".$object->Recognition;
182             $result = $this->transmitText($object, $content);
183         }
184 
185         return $result;
186     }
187 
188     private function receiveVideo($object)
189     {
190         $content = array("MediaId"=>$object->MediaId, "ThumbMediaId"=>$object->ThumbMediaId, "Title"=>"", "Description"=>"");
191         $result = $this->transmitVideo($object, $content);
192         return $result;
193     }
194 
195     private function receiveLink($object)
196     {
197         $content = "你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url;
198         $result = $this->transmitText($object, $content);
199         return $result;
200     }
201 
202     private function transmitText($object, $content)
203     {
204         $textTpl = "<xml>
205 <ToUserName><![CDATA[%s]]></ToUserName>
206 <FromUserName><![CDATA[%s]]></FromUserName>
207 <CreateTime>%s</CreateTime>
208 <MsgType><![CDATA[text]]></MsgType>
209 <Content><![CDATA[%s]]></Content>
210 </xml>";
211         $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
212         return $result;
213     }
214 
215     private function transmitImage($object, $imageArray)
216     {
217         $itemTpl = "<Image>
218     <MediaId><![CDATA[%s]]></MediaId>
219 </Image>";
220 
221         $item_str = sprintf($itemTpl, $imageArray[‘MediaId‘]);
222 
223         $textTpl = "<xml>
224 <ToUserName><![CDATA[%s]]></ToUserName>
225 <FromUserName><![CDATA[%s]]></FromUserName>
226 <CreateTime>%s</CreateTime>
227 <MsgType><![CDATA[image]]></MsgType>
228 $item_str
229 </xml>";
230 
231         $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
232         return $result;
233     }
234 
235     private function transmitVoice($object, $voiceArray)
236     {
237         $itemTpl = "<Voice>
238     <MediaId><![CDATA[%s]]></MediaId>
239 </Voice>";
240 
241         $item_str = sprintf($itemTpl, $voiceArray[‘MediaId‘]);
242 
243         $textTpl = "<xml>
244 <ToUserName><![CDATA[%s]]></ToUserName>
245 <FromUserName><![CDATA[%s]]></FromUserName>
246 <CreateTime>%s</CreateTime>
247 <MsgType><![CDATA[voice]]></MsgType>
248 $item_str
249 </xml>";
250 
251         $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
252         return $result;
253     }
254 
255     private function transmitVideo($object, $videoArray)
256     {
257         $itemTpl = "<Video>
258     <MediaId><![CDATA[%s]]></MediaId>
259     <ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
260     <Title><![CDATA[%s]]></Title>
261     <Description><![CDATA[%s]]></Description>
262 </Video>";
263 
264         $item_str = sprintf($itemTpl, $videoArray[‘MediaId‘], $videoArray[‘ThumbMediaId‘], $videoArray[‘Title‘], $videoArray[‘Description‘]);
265 
266         $textTpl = "<xml>
267 <ToUserName><![CDATA[%s]]></ToUserName>
268 <FromUserName><![CDATA[%s]]></FromUserName>
269 <CreateTime>%s</CreateTime>
270 <MsgType><![CDATA[video]]></MsgType>
271 $item_str
272 </xml>";
273 
274         $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
275         return $result;
276     }
277 
278     private function transmitNews($object, $newsArray)
279     {
280         if(!is_array($newsArray)){
281             return;
282         }
283         $itemTpl = "    <item>
284         <Title><![CDATA[%s]]></Title>
285         <Description><![CDATA[%s]]></Description>
286         <PicUrl><![CDATA[%s]]></PicUrl>
287         <Url><![CDATA[%s]]></Url>
288     </item>
289 ";
290         $item_str = "";
291         foreach ($newsArray as $item){
292             $item_str .= sprintf($itemTpl, $item[‘Title‘], $item[‘Description‘], $item[‘PicUrl‘], $item[‘Url‘]);
293         }
294         $newsTpl = "<xml>
295 <ToUserName><![CDATA[%s]]></ToUserName>
296 <FromUserName><![CDATA[%s]]></FromUserName>
297 <CreateTime>%s</CreateTime>
298 <MsgType><![CDATA[news]]></MsgType>
299 <Content><![CDATA[]]></Content>
300 <ArticleCount>%s</ArticleCount>
301 <Articles>
302 $item_str</Articles>
303 </xml>";
304 
305         $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray));
306         return $result;
307     }
308 
309     private function transmitMusic($object, $musicArray)
310     {
311         $itemTpl = "<Music>
312     <Title><![CDATA[%s]]></Title>
313     <Description><![CDATA[%s]]></Description>
314     <MusicUrl><![CDATA[%s]]></MusicUrl>
315     <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
316 </Music>";
317 
318         $item_str = sprintf($itemTpl, $musicArray[‘Title‘], $musicArray[‘Description‘], $musicArray[‘MusicUrl‘], $musicArray[‘HQMusicUrl‘]);
319 
320         $textTpl = "<xml>
321 <ToUserName><![CDATA[%s]]></ToUserName>
322 <FromUserName><![CDATA[%s]]></FromUserName>
323 <CreateTime>%s</CreateTime>
324 <MsgType><![CDATA[music]]></MsgType>
325 $item_str
326 </xml>";
327 
328         $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
329         return $result;
330     }
331 
332     private function logger($log_content)
333     {
334         if(isset($_SERVER[‘HTTP_APPNAME‘])){   //SAE
335             sae_set_display_errors(false);
336             sae_debug($log_content);
337             sae_set_display_errors(true);
338         }else if($_SERVER[‘REMOTE_ADDR‘] != "127.0.0.1"){ //LOCAL
339             $max_size = 10000;
340             $log_filename = "log.xml";
341             if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}
342             file_put_contents($log_filename, date(‘H:i:s‘)." ".$log_content."\r\n", FILE_APPEND);
343         }
344     }
345 }
346 
347 
348 ?>
微信公众平台开发接口PHP SDK完整版

微信公众平台开发接口PHP SDK完整版,布布扣,bubuko.com

微信公众平台开发接口PHP SDK完整版

上一篇:java awt小程序菜单乱码怎么解决


下一篇:.NET生成小程序码,并合自定义背景图生成推广小程序二维码