valid();$wechatObj->responseMsg();class wechatCallbackapiTest{public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()){ echo $echoStr; exit; } } public function responseMsg() {//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); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $type=$postObj->MsgType;$customevent=$postObj->Event; $latitude=$postObj->Location_X; $l $textTpl = "%s%s%s%s%s0"; //發送位置回複 if($type=="location"){ $c $msgType = "text"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } //關鍵字回複 if(!empty( $keyword )) { $msgType = "text"; if($keyword=="1") { $contentStr = "感謝關注泛IT!";} if($keyword=="2") { $contentStr = "聯絡QQ:,歡迎投稿以及提出寶貴意見";} if($keyword=="3") { $contentStr = "你的圖片不錯,還是輸入文字吧!";} $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ echo "Input something..."; } //關注公眾號後回複 if($type="event" and $customevent="subscribe"){ $c $msgType = "text"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } //圖片回複 if($type="image"){ $c $msgType = "text"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }
//switch語句最佳化
/* switch($type){ //處理文本 case "text": //關鍵字回複 if(!empty( $keyword )) { //$msgType = "text"; if($keyword=="1") { $contentStr = "回複1查看使用說明\n回複2查看連絡方式\n回複3使用翻譯字典\n回複4使用機器人聊天功能";} if($keyword=="2") { $contentStr = "聯絡QQ:,歡迎投稿以及提出寶貴意見";} }else{ $c } break; //處理圖片資訊 case "image":$c break; //處理位置資訊 case "location":$cbreak; //處理關注事件 case "event":if($customevent=="subscribe"){$cbreak; default:$c } $msgType = "text"; $resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);echo $resultStr; */
}else { echo ""; exit; } }
private function checkSignature()
{ // you must define TOKEN by yourself if (!defined("TOKEN")) { throw new Exception('TOKEN is not defined!'); } $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN;$tmpArr = array($token, $timestamp, $nonce); // use SORT_STRING rulesort($tmpArr, SORT_STRING);$tmpStr = implode( $tmpArr );$tmpStr = sha1( $tmpStr );if( $tmpStr == $signature ){return true;}else{return false;}}}?>
以上就介紹了php開發01,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。