文本消息
1)路由:
Route::rule('weixin','admin/Weixin/valid');//
2)配置中的路径:http://zhll.jujiaoweb.com/weixin(域名+路由)
3)由于微信配置成功中填写路径直接进入该控制器如:
public function valid() { $wechatObj = new Mweixin(); $wechatObj->responseMsg(); }
4)model层:
public function responseMsg() { //get post data, May be due to the different environments //$postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //当普通微信用户向公众账号发送消息时,微信服务器将POST消息的XML数据包到开发者填写的URL上,因此消息是以XML格式的数据包发送的。 //1)首先得到xml数据包信息: $postStr = file_get_contents("php://input");//?????????????????? //extract post data 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); //得到了数据之后,然后我们就是要解析微信服务器发送过来的xml数据包了 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName;//发送方帐号(一个OpenID) $toUsername = $postObj->ToUserName;// 开发者微信号 $keyword = trim($postObj->Content);//Content $msgType=$postObj->MsgType;//类型 $event=$postObj->Event; //接收事件类型 $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>"; if(!empty( $keyword )) { $msgType = "text"; //mb_strpos(原句,所要查的字段)----返回字符串在另一字符串中第一次出现的位置,如果没有找到字符串则返回 FALSE。 if(mb_strpos($keyword,'1')!==false){ $contentStr="你好!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }elseif(mb_strpos($keyword,"2")!==false){ $contentStr="hello"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else if(mb_strpos($keyword,"3")!==false){ $arr = array( array( 'title'=>'百度啦啦啦', 'description'=>"qq is very cool", 'picUrl'=>'http://zhll.jujiaoweb.com/static/admin/image/2.jpg', 'url'=>'http://www.baidu.com', ), array( 'title'=>'百度咳咳咳', 'description'=>"百度 is very cool", 'picUrl'=>'http://zhll.jujiaoweb.com/static/admin/image/2.jpg', 'url'=>'http://www.baidu.com', ), ); $template = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <ArticleCount>".count($arr)."</ArticleCount> <Articles>"; foreach($arr as $k=>$v){ $template .="<item> <Title><![CDATA[".$v['title']."]]></Title> <Description><![CDATA[".$v['description']."]]></Description> <PicUrl><![CDATA[".$v['picUrl']."]]></PicUrl> <Url><![CDATA[".$v['url']."]]></Url> </item>"; } $template .="</Articles> </xml> "; $resultStr=sprintf($template, $fromUsername, $toUsername, time(), 'news'); echo $resultStr; }else if(mb_strpos($keyword,"4")!==false){ //获取个人信息 $userInfo=$this->getUserInfo($fromUsername); $name=$userInfo->nickname; $contentStr=$name; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, json_encode($userInfo)); echo $resultStr; } else{ $contentStr = "欢迎来到awake720公众号!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } }else{ echo "Input something..."; } }else { echo ""; exit; } if($msgType=="event"){//关注/取消关注事件----》消息类型,event if($event=="subscribe"){//Event 事件类型,subscribe(订阅)、unsubscribe(取消订阅) $msgType = "text"; //创建菜单 $sss=$this->set_menue(); //获取个人信息添加到数据库 $this->addUserInfo($fromUsername); //当用户关注后的问候语句 $contentStr = "Welcome to wechat world-----subscribe!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; //跳转路径 // header("Location:http://zhll.jujiaoweb.com/index/index/sub"); } } }