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

      

  • 相关阅读:
    zoj 1004 Anagrams by Stack (dfs+stack)
    poj 3009 Curling 2.0 (dfs)
    poj 2965 The Pilots Brothers' refrigerator (bfs+位运算)
    bcl 1387 最长重复子串 (后缀数组)
    zoj 3332 Strange Country II (dfs)
    poj 2157 Maze (bfs)
    poj 1564 && zoj 1711 Sum It Up (dfs)
    hdu 2686 Matrix (多进程DP)
    poj 3256 Cow Picnic (dfs)
    poj 1606 Jugs (bfs)
  • 原文地址:https://www.cnblogs.com/ganwenjun/p/7160644.html
Copyright © 2011-2022 走看看