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;	
    	}	
    }
    

      

  • 相关阅读:
    接口测试和性能测试
    loadrunner总结
    loadrunner 基本操作
    loadrunner安装和应用
    qtp安装和使用
    Quality Center安装步骤
    JIRA的安装及配置
    testlink使用方法
    python3常用模块--熟练使用
    python2和python3差异
  • 原文地址:https://www.cnblogs.com/ganwenjun/p/7160644.html
Copyright © 2011-2022 走看看