zoukankan      html  css  js  c++  java
  • 微信公众号-消息响应

    消息响应

    <?php
    define("APPID","wx4cff8e15a7a0801d");//填写自己的APPID
    define("APPSECRET","4d7cb4b8b54412d9ef0c6a7c011cd570");//填写自己的APPSECRET
    define("TOKEN", "weixin");//token随便填,只要一致就行。
    $wechat = new wechat();
    $wechat->responseMsg();
     
    class wechat{
        private $_appid;
        private $_appsecret;
        private $_token;
    	private $tpl=array(
    				//发送文本消息模板
    				'text' => '	<xml>
    							<ToUserName><![CDATA[%s]]></ToUserName>
    							<FromUserName><![CDATA[%s]]></FromUserName>
    							<CreateTime>%s</CreateTime>
    							<MsgType><![CDATA[text]]></MsgType>
    							<Content><![CDATA[%s]]></Content>
    							</xml>',
    	);
        public function __construct(){
            $this->_appid =APPID;
            $this->_appsecret =APPSECRET;
            $this->_token =TOKEN;
        }
    	/**
    	  *响应微信平台发送的消息
    	**/
        public function responseMsg()//所有的被动消息处理都从这里开始
        {
    		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];//获得用户发送信息
    		$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);//解析XML到对象
    		switch($postObj->MsgType){
    			case 'text': //文本处理
    				$this->_doText($postObj);
    				break;
    			case 'event': //事件处理
    				$this->_doEvent($postObj);
    				break;
    			default: exit;
    		}
    	}
        /**
    	  *_doText():处理文本消息
    	  *@postObj:响应的消息对象
    	**/
    	private function _doText($postObj)
    	{
    		$fromUsername = $postObj->FromUserName;
    		$toUsername = $postObj->ToUserName;
    		$keyword = trim($postObj->Content);
    		$time = time();           
    		if(!empty( $keyword ))
    		{
    			$contentStr='hello world!';
                            //如果不想输出任何消息直接exit停止运行程序就行。
    			//这里可以做一些业务处理
    			if($keyword == "hello")
    				$contentStr = "Welcome to wechat world!";
    			$msgType = "text";
    			$resultStr = sprintf($this->tpl['text'], $fromUsername, $toUsername, $time, $contentStr);
    			echo $resultStr;
    		}
            exit;	
    	}	
    }
    

      

  • 相关阅读:
    JS,JQ及时监听input值的变化,MUI的input搜索框里的清除按钮的点击监听事件
    MUI 底部弹出的选择框
    MUI 拍照或选取照片上传作为头像
    多行文本文本输入框 textarea 可点击任意地方编辑的问题
    mui dtpicker 时间的设置 以及MUI的弹窗
    MUI 样式按钮的禁用
    利用渐变实现书本的效果
    调用sqlserver中的存储过程
    XmlHelper
    面试题 数据库sql
  • 原文地址:https://www.cnblogs.com/ganwenjun/p/7160644.html
Copyright © 2011-2022 走看看