入口文件代码 <?php /** * wechat php test */ //define your token //定义秘钥文件 define("TOKEN", "weixin"); //实例化微信对象 $wechatObj = new wechatCallbackapiTest(); //注释valid功能 //$wechatObj->valid(); //开启自动回复功能 $wechatObj->responseMsg(); //定义类文件 class wechatCallbackapiTest { //实现VALID验证方法:实现对接微信公众平台 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 //接收用户端发送过来的XML数据 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //extract post data //判断XML数据是否为空 if (!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); //通过simplexml进行XML解析 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); //手机端 $fromUsername = $postObj->FromUserName; //微信的公众平台 $toUsername = $postObj->ToUserName; //接收用户发送的关键词 $keyword = trim($postObj->Content); $msgType = $postObj->MsgType ; //定义$longitude 与 $latitude接收用户发送的经纬度信息 $latitude = $postObj->Location_X; $longitude = $postObj->Location_Y; //时间戳 $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>"; //音乐发送模板 $musicTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Music> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <MusicUrl><![CDATA[%s]]></MusicUrl> <HQMusicUrl><![CDATA[%s]]></HQMusicUrl> </Music> </xml>"; //图文发送模板 $newsTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <ArticleCount>%s</ArticleCount> %s </xml> "; if($msgType == 'text') { //判断用户发送关键词是否为空 if(!empty( $keyword )) { if($keyword == '文本'){ //回复类型,如果为text,代表文本类型 $msgType = "text"; //回复内容 $contentStr = "您发送的是文本消息"; //格式化字符串 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); //把XML数据返回给手机端 echo $resultStr; } elseif ($keyword == '?'|| $keyword == '?'){ // 定义回复类型 $msgType = 'text'; // 回复内容 $contentStr = "【1】特种服务号码\n【2】通讯服务号码\n【3】银行服务号码\n您可以通过输入【】方括号的编号获取内容哦!"; $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr); //返回数据到微信客户端 echo $resultStr; } elseif ($keyword == '1'){ // 定义回复类型 $msgType = 'text'; // 回复内容 $contentStr = "常用特殊服务号码:\n匪警:110\n火警:119\n救护车:120"; $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr); //返回数据到微信客户端 echo $resultStr; } elseif ($keyword == '2'){ // 定义回复类型 $msgType = 'text'; // 回复内容 $contentStr = "常用通讯服务号码:\n中国移动:10086\n中国电信:10000"; $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr); //返回数据到微信客户端 echo $resultStr; } elseif ($keyword == '3'){ // 定义回复类型 $msgType = 'text'; // 回复内容 $contentStr = "常用银行服务号码:\n工商银行:95588\n建设银行:95533"; $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr); //返回数据到微信客户端 echo $resultStr; //音乐回复接口 } elseif ($keyword == '音乐'){ //定义回复类型 $msgType = 'music'; //定义音乐标题 $title = '冰雪奇缘'; //定义音乐描述 $desc = '《冰雪奇缘》原声大碟。。。'; //定义音乐链接 $url = 'http://1404604487.applinzi.com/music.mp3'; //定义音乐高清链接 $hqurl = 'http://1404604487.applinzi.com/music.mp3'; //格式化字符串 $resultStr = sprintf($musicTpl,$fromUsername,$toUsername,$time,$msgType,$title,$desc,$url,$hqurl); //返回XML数据到微信客户端 echo $resultStr; } elseif ($keyword == '图文'){ //设置回复类型 $msgType = 'news'; //设置返回图文数量 $count = 4; //设置要回复的图文数据 $str = "<Articles>"; for($i=1;$i<=$count;$i++){ $str .="<item> <Title><![CDATA[微信二次开发{$i}]]></Title> <Description><![CDATA[微信二次开发过程]]></Description> <PicUrl><![CDATA[http://1404604487.applinzi.com/images/{$i}.jpg]]></PicUrl> <Url><![CDATA[http://www.wmpzn.com]]></Url> </item>"; } $str .="</Articles>"; //格式化字符串 $resultStr = sprintf($newsTpl,$fromUsername,$toUsername,$time,$msgType,$count,$str); echo $resultStr; } }else{ echo "Input something..."; } } elseif ($msgType == 'image'){ //回复类型,如果为image,代表图片类型 $msgType = "text"; //回复内容 $contentStr = "您发送的是图片消息"; //格式化字符串 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); //把XML数据返回给手机端 echo $resultStr; } elseif ($msgType == 'voice'){ //回复类型,如果为voice,代表语音类型 $msgType = "text"; //回复内容 $contentStr = "您发送的是语音消息"; //格式化字符串 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); //把XML数据返回给手机端 echo $resultStr; } elseif ($msgType == 'video'){ //回复类型,如果为video,代表视屏类型 $msgType = "text"; //回复内容 $contentStr = "您发送的是视屏消息"; //格式化字符串 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); //把XML数据返回给手机端 echo $resultStr; } elseif ($msgType == 'location'){ //回复类型,如果为location,代表位置类型 $msgType = "text"; //回复内容 $contentStr = "您发送的是地理位置消息,您的位置:经度{$longitude},维度{$latitude}"; //格式化字符串 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); //把XML数据返回给手机端 echo $resultStr; } elseif ($msgType == 'link'){ //回复类型,如果为link,代表链接类型 $msgType = "text"; //回复内容 $contentStr = "您发送的是链接消息"; //格式化字符串 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); //把XML数据返回给手机端 echo $resultStr; } } else { echo ""; exit; } } //定义 checkSignature private function checkSignature() { // you must define TOKEN by yourself //判断TOKEN秘钥是否定义 if (!defined("TOKEN")) { throw new Exception('TOKEN is not defined!'); } //接收微信加密签名 $signature = $_GET["signature"]; //接收时间戳 $timestamp = $_GET["timestamp"]; //接收随机数 $nonce = $_GET["nonce"]; //把TOKEN常量赋值给$token变量 $token = TOKEN; //把相关参数组装为数组(秘钥、时间戳、随机数) $tmpArr = array($token, $timestamp, $nonce); // use SORT_STRING rule //通过字典法进行排序 sort($tmpArr, SORT_STRING); //把排序后的数组转化字符串 $tmpStr = implode( $tmpArr ); //通过哈希算法对字符串进行加密操作 $tmpStr = sha1( $tmpStr ); //与加密签名进行对比 if( $tmpStr == $signature ){ return true; }else{ return false; } } } ?>
个人主页