<?php /** * wechat php test */ //define your token define("TOKEN", "123456"); $wechatObj = new wechatCallbackapiTest(); $wechatObj->responseMsgCity(); class wechatCallbackapiTest { public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()){ echo $echoStr; exit; } } //特定消息特定回复 public function responseMsgCity() { //PHP5.6之前用这个获取原始数据 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; // error_log(var_export($postStr,1),3,'./log.txt');exit; if(!empty($postStr)) { $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; $imagesTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Image> <MediaId><![CDATA[%s]]></MediaId> </Image> </xml>"; $voiceTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Voice> <MediaId><![CDATA[%s]]></MediaId> </Voice> </xml>"; if(!empty($keyword)) { // $url = "http://api.k780.com/?app=weather.today&weaid={$keyword}&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json"; // $weather = file_get_contents($url); // //将json数据转化为数组 // $weather_array = json_decode($weather,true); // if($weather_array['success'] == 1) // { // $msgType = "text"; // $contentStr = "您的位置在".$weather_array['result']['citynm']."\r\n"."目前是".$weather_array['result']['week']."\r\n". // "温度为".$weather_array['result']['temperature']; // $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); // echo $resultStr; // } // else // { // $msgType = "text"; // $contentStr = "您输入的方位不对!!!"; // $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); // echo $resultStr; // } //图片 if($keyword=='图片'){ $msgType = "image"; $contentStr = "ieoPDQ_Cty67siv0BQmvrHl59C8j8z7rj9-lieoWY69_2g_pzEHZMIwgFHeA1FDK"; $resultStr = sprintf($imagesTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } //语音 // if($keyword=='语音'){ // $msgType = "voice"; // $contentStr = "XvAALcAwIOusMKPJAQVCFgcoE-4yncuRplNFxBa1nVuqSSLeHLeNAlCkHnpO4sxT"; // $resultStr = sprintf($voiceTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); // echo $resultStr; // } } } } private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } }
微信 被回复信息
最新推荐文章于 2024-07-26 18:04:10 发布