关于微信开发模式的PHP代码
<?php<br />/**<br /> * wechat php test<br /> */<br /><br />//define your token<br />define("TOKEN", "weixin");<br />$wechatObj = new wechatCallbackapiTest();<br />//$wechatObj->valid();<br />$wechatObj->responseMsg();<br />class wechatCallbackapiTest<br />{<br /> public function valid()<br /> {<br /> $echoStr = $_GET["echostr"];<br /><br /> //valid signature , option<br /> if($this->checkSignature()){<br /> echo $echoStr;<br /> exit;<br /> }<br /> }<br /><br /> public function responseMsg()<br /> {<br /> //get post data, May be due to the different environments<br /> $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];<br /><br /> //extract post data<br /> if (!empty($postStr)){<br /> <br /> $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);<br /> $fromUsername = $postObj->FromUserName;<br /> $toUsername = $postObj->ToUserName;<br /> $type =$postObj->MsgType;<br /> $customevent = $postObj->Event;<br /> $latitude =$postObj->Location_X;<br /> $longitude =$postObj->Location_Y;<br /> $keyword = trim($postObj->Content);<br /> $time = time();<br /> $textTpl = "<xml><br /> <ToUserName><![CDATA[%s]]></ToUserName><br /> <FromUserName><![CDATA[%s]]></FromUserName><br /> <CreateTime>%s</CreateTime><br /> <MsgType><![CDATA[%s]]></MsgType><br /> <Content><![CDATA[%s]]></Content><br /> <FuncFlag>0</FuncFlag><br /> </xml>"; <br /> switch($type)<br /> {<br /> case "event";<br /> if($customevent == "subscribe")<br /> {$contentStr = "感谢关注"}<br /> break;<br /> case "image"<br /> $contentStr ="图片很棒";<br /> break;<br /> case "location";<br /> $contentStr ="{$latitude},{longitude}";<br /> break;<br /> case "text";<br /> switch($keyword)<br /> {case "1"<br /> $contentStr="1"<br /> break;<br /> case "2"<br /> $contentStr ="2"<br /> break;<br /> default;<br /> $contentStr= "hi";}<br /> break;<br /> }<br /> $msgType = "text";<br /> $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);<br /> echo $resultStr;<br /> }else <br /> {<br /> echo "";<br /> exit;<br /> }<br /> }<br /> <br /> private function checkSignature()<br /> {<br /> $signature = $_GET["signature"];<br /> $timestamp = $_GET["timestamp"];<br /> $nonce = $_GET["nonce"]; <br /> <br /> $token = TOKEN;<br /> $tmpArr = array($token, $timestamp, $nonce);<br /> sort($tmpArr, SORT_STRING);<br /> $tmpStr = implode( $tmpArr );<br /> $tmpStr = sha1( $tmpStr );<br /> <br /> if( $tmpStr == $signature ){<br /> return true;<br /> }else{<br /> return false;<br /> }<br /> }<br />}<br /><br />?>
public function valid()<br /> {<br /> $echoStr = $_GET["echostr"];<br /> <br /> //valid signature , option<br /> if($this->checkSignature()){<br /> echo $echoStr;<br /> exit;<br /> }<br /> }
public function valid()<br> {<div class="clear"> </div>