<?phpnamespaceapp\index\controller;/**
* wechat php test
*///define your tokendefine("TOKEN","sudons");classWechat{publicfunctionindex(){if(isset($_GET['echostr'])){$this->valid();}else{$this->responseMsg();}}publicfunctionvalid(){$echoStr=$_GET["echostr"];//valid signature , optionif($this->checkSignature()&&$echoStr){echo$echoStr;
exit;}}privatefunctioncheckSignature(){// you must define TOKEN by yourselfif(!defined("TOKEN")){thrownewException('TOKEN is not defined!');}//获取微信提交的参数$signature=$_GET["signature"];$timestamp=$_GET["timestamp"];$nonce=$_GET["nonce"];$token=TOKEN;//微信提交参数放入数组$tmpArr=array($token,$timestamp,$nonce);// use SORT_STRING rule字典序排序sort($tmpArr,SORT_STRING);//拼接字符串$tmpStr=implode($tmpArr);//加密$tmpStr=sha1($tmpStr);//检验signatureif($tmpStr==$signature){returntrue;}else{returnfalse;}}publicfunctionresponseMsg(){//get post data, May be due to the different environments$postStr=$GLOBALS["HTTP_RAW_POST_DATA"];//extract post dataif(!empty($postStr)){/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
the best way is to check the validity of xml by yourself */libxml_disable_entity_loader(true);$postObj=simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA);$RX_TYPE=trim($postObj->MsgType);switch($RX_TYPE){case"text":$resultStr=$this->receiveText($postObj);break;case"image":$resultStr=$this->receiveImage($postObj);break;case"location":$resultStr=$this->receiveLocation($postObj);break;case"voice":$resultStr=$this->receiveVoice($postObj);break;case"video":$resultStr=$this->receiveVideo($postObj);break;case"link":$resultStr=$this->receiveLink($postObj);break;case"event":$resultStr=$this->receiveEvent($postObj);break;default:$resultStr="unknow msg type: ".$RX_TYPE;break;}echo$resultStr;}else{echo"";
exit;}}//统一用文本回复privatefunctiontransmitText($object,$content,$flag=0){$textTpl="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>%d</FuncFlag>
</xml>";$resultStr=sprintf($textTpl,$object->FromUserName,$object->ToUserName,time(),$content,$flag);return$resultStr;}//1.事件消息privatefunctionreceiveEvent($object){$contentStr="";switch($object->Event){case"subscribe":$contentStr="欢迎关注塞雅创想";break;case"unsubscribe":$contentStr="";break;case"CLICK":switch($object->EventKey){default:$contentStr="你点击了菜单: ".$object->EventKey;break;}break;default:$contentStr="receive a new event: ".$object->Event;break;}$resultStr=$this->transmitText($object,$contentStr);return$resultStr;}//2.文本消息privatefunctionreceiveText($object){$funcFlag=0;$contentStr="你发送的是文本,内容为:".$object->Content;$resultStr=$this->transmitText($object,$contentStr,$funcFlag);return$resultStr;}//3.图片消息privatefunctionreceiveImage($object){$funcFlag=0;$contentStr="你发送的是图片,地址为:".$object->PicUrl;$resultStr=$this->transmitText($object,$contentStr,$funcFlag);return$resultStr;}//4.语音消息privatefunctionreceiveVoice($object){$funcFlag=0;$contentStr="你发送的是语音,媒体ID为:".$object->MediaId;$resultStr=$this->transmitText($object,$contentStr,$funcFlag);return$resultStr;}//5.视频消息privatefunctionreceiveVideo($object){$funcFlag=0;$contentStr="你发送的是视频,媒体ID为:".$object->MediaId;$resultStr=$this->transmitText($object,$contentStr,$funcFlag);return$resultStr;}//6.位置消息privatefunctionreceiveLocation($object){$funcFlag=0;$contentStr="你发送的是位置,纬度为:".$object->Location_X.";经度为:".$object->Location_Y.";缩放级别为:".$object->Scale.";位置为:".$object->Label;$resultStr=$this->transmitText($object,$contentStr,$funcFlag);return$resultStr;}//7.链接消息privatefunctionreceiveLink($object){$funcFlag=0;$contentStr="你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url;$resultStr=$this->transmitText($object,$contentStr,$funcFlag);return$resultStr;}}?>