php微信開發之關鍵詞回覆功能

本文原創自news.mkq.online
本文地址:news.wpj.online
版權聲明:本文爲原創文章,版權牛站www.niuzhan.com所有
本文實例爲大家分享了php微信開發之關鍵詞回覆的具體代碼,供大家參考,具體內容如下

view sourceprint?
01

valid(); 10 $wechatObj->responseMsg(); 11 class wechatCallbackapiTest 12 { 13 public function valid() 14 { 15 $echoStr = $_GET["echostr"]; 16 17 //valid signature , option 18 if($this->checkSignature()){ 19 echo $echoStr; 20 exit; 21 } 22 } 23 24 public function responseMsg() 25 { 26 //get post data, May be due to the different environments 27 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; 28 29 //extract post data 30 if (!empty($postStr)){ 31 32 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); 33 $fromUsername = $postObj->FromUserName; 34 $toUsername = $postObj->ToUserName; 35 $keyword = trim($postObj->Content); 36 $time = time(); 37 $textTpl = "%s0"; 38 if(!empty( $keyword )) 39 { 40 $msgType = "text"; 41 if($keyword=="1"){ 42 $contentStr = "qiphon";} 43 if($keyword=="2"){ 44 $contentStr = "最新資訊:《環境違法行爲舉報獎勵辦法》";} 45 if($keyword=="3"){ 46 $contentStr = "31離婚協議\n32租房合同\n33交通事故協議書";} 47 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); 48 echo $resultStr; 49 }else{ 50 echo "Input something..."; 51 } 52 53 }else { 54 echo ""; 55 exit; 56 } 57 } 58 59 private function checkSignature() 60 { 61 $signature = $_GET["signature"]; 62 $timestamp = $_GET["timestamp"]; 63 $nonce = $_GET["nonce"]; 64 65 $token = TOKEN; 66 $tmpArr = array($token, $timestamp, $nonce); 67 sort($tmpArr); 68 $tmpStr = implode( $tmpArr ); 69 $tmpStr = sha1( $tmpStr ); 70 71 if( $tmpStr == $signature ){ 72 return true; 73 }else{ 74 return false; 75 } 76 } 77 } 78 79 ?> 以上就是本文的全部內容,希望對大家的學習有所幫助。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章