微信接入:

/**
* @Desc   : 获取微信事件通知主函数
* @Data   : 2015-12-15 10:28:39
*/
public function getNoticeByEventMainAction()
{/*{{{*/
    $echostr    = $this->getParam('echostr');
    $timestamp  = $this->getParam('timestamp');
    $signatrue  = $this->getParam('signature');
    $nonce      = $this->getParam('nonce');
   //1.验证来源是否属于微信
   $signStatus  = $this->wxApiMod->checkSignAture($signatrue,$timestamp,$nonce);
   if($signStatus===false) exit;
   //2.验证是否为首次接入
   if(isset($echostr) && $echostr){ die($echostr); }
   //3.相应微信上行数据
   $msgStr = trim(file_get_contents('php://input'));
   $this->wxApiMod->parseWeixinMsgData($msgStr);
}/*}}}*/

 
/**
* @Desc   : 微信通信签名真实性验证
* @Param  : signature  微信签名串
* @Param  : timestamp  时间戳 
* @Param  : nonce      随机数
* @Date   : 2015-12-15 10:28:39
* @Return :(boolen)
*/

public function checkSignAture($signature,$timestamp,$nonce)
{/*{{{*/

    $tmpArr     = array(self::$_signToken, $timestamp, $nonce);
    sort($tmpArr,SORT_STRING);
    $tmpStr     = implode( $tmpArr );
    $tmpStr     = sha1( $tmpStr );
    if($tmpStr != $signature){
        self::write('~~checkSignAture Error~~'."signature:$signature~~timestamp:$timestamp~~noce:$nonce");
        return false;
    }else{
        return true;
    }

}/*}}}*/

微信XML 数据解析代码

    /**
    * @Desc  : 解析微信推送的XML数据
    * @param :$strXml
    * @Date  : 2015-12-18 16:00:57 
    */
   public static function parseWeixinMsgData($strXml)
   {/*{{{ */

       $strXml       = trim($strXml);
       self::writeLog($strXml,'notice');
       if(empty($strXml)) return false;
       $msgXmlObj       = simplexml_load_string($strXml, 'SimpleXMLElement', LIBXML_NOCDATA);
       if(!is_object($msgXmlObj)){
            self::writeLog('~~strXml parse error~~'.$strXml);
            return false;
       }
      self::$_wxParseXmlData  = array(
             'fromUserName' => (string) $msgXmlObj->FromUserName,
             'toUserName'   => (string) $msgXmlObj->ToUserName,
             'msgType'      => strtolower((string) $msgXmlObj->MsgType),
             'createTime'   => (string) $msgXmlObj->CreateTime,
             'messageIu'    => (string) $msgXmlObj->MsgId,
      );

      switch (self::$_wxParseXmlData['msgType']) {
        case 'text' : //纯文本
            self::$_wxParseXmlData['content']       = trim((string) $msgXmlObj->Content);
            break;
        case 'image': //图片
            self::$_wxParseXmlData['media_url']     = (string) $msgXmlObj->PicUrl;
            self::$_wxParseXmlData['media_id']      = (string) $msgXmlObj->MediaId;
            break;
        case 'voice': //音频
            self::$_wxParseXmlData['media_id']      = (string) $msgXmlObj->MediaId;
            self::$_wxParseXmlData['format']        = (string) $msgXmlObj->Format;
            self::$_wxParseXmlData['recognition']   = (string) $msgXmlObj->Recognition;
            break;
        case 'video': //视频
            self::$_wxParseXmlData['media_id']      = (string) $msgXmlObj->MediaId;
            self::$_wxParseXmlData['thumbMediaId']  = (string) $msgXmlObj->ThumbMediaId;
            break;
        case 'location': //地理位置
            self::$_wxParseXmlData['location_x']    = (string) $msgXmlObj->Location_X;
            self::$_wxParseXmlData['location_y']    = (string) $msgXmlObj->Location_Y;
            self::$_wxParseXmlData['scale']         = (string) $msgXmlObj->Scale;
            self::$_wxParseXmlData['label']         = (string) $msgXmlObj->Label;
            break;
        case 'link'  : //连接
            self::$_wxParseXmlData['title']         = (string) $msgXmlObj->Title;
            self::$_wxParseXmlData['description']   = (string) $msgXmlObj->Description;
            self::$_wxParseXmlData['url']           = (string) $msgXmlObj->Url;
            break;
        case 'event' : //微信用户触发事件
            self::$_wxParseXmlData['event_type']    = strtolower((string) $msgXmlObj->Event);
            self::$_wxParseXmlData['event_key']     = self::$_wxParseXmlData['event_key']  = (string) $msgXmlObj->EventKey;
            switch (self::$_wxParseXmlData['event_type']) {
                case 'subscribe'  :   //关注
                        $msgStr="感谢您的关注!,欢迎欢迎,热烈欢迎";
                        echo self::weixinHotMess(self::$_wxParseXmlData['fromUserName'],$msgStr,self::$_wxParseXmlData['toUserName']);
                break;
                case 'unsubscribe':   //取消关注
                    self::updateWxUserInfo(self::$_wxParseXmlData['fromUserName'],0);
                break;
                case 'click'      :   //点击事件
                break;
                case 'scan'       :   //扫描事件
                break;
                case 'location'   :   //上报地理位置
                    self::$_wxParseXmlData['latitude']   = (string) $msgXmlObj->Latitude;
                    self::$_wxParseXmlData['longitude']  = (string) $msgXmlObj->Longitude;
                    self::$_wxParseXmlData['precision']  = (string) $msgXmlObj->Precision;
                    break;
            }
            break;
        default:
            break;
        }
        self::writeLog('~~xml parse data~~'.json_encode(self::$_wxParseXmlData));
        return self::$_wxParseXmlData;

   }/*}}}*/

发送模板消息代码:

/**
* @Desc   : 给用活跃用户发送消息
* @Date   : 2016-01-14 18:44:46
*/
private static function weixinHotMess($toUser,$msgInfo,$fromUser='',$msgType='text')
{/*{{{*/
    $msgTpl    = '';
    $resultStr = '';
    switch($msgType){
            //纯文本消息
            case 'text' :
            $msgTpl  = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[%s]]></MsgType>
                        <Content><![CDATA[%s]]></Content>
                        <FuncFlag>0</FuncFlag>
                        </xml>";
            $resultStr = sprintf($msgTpl,$toUser,$fromUser, time(), $msgType,$msgInfo);
            break;
            //图片消息
            case 'image':
            break;
            //...待拓展共6种

    }
    return $resultStr;
}/*}}}*/

发送模板消息代码:

/**
* @Desc   : 发送问题相关消息给用户,并绑定关系
* @Param  : logins为-1为未登录提问,关注问题, 1正常直接发信息
* @Date   : 2015-12-25 12:18:20  
*/
public static function sendQuestionMsg($askid,$openid,$logins='1')
{/*{{{*/
    $msgBody  = array(
        'touser'        => $openid,
        'template_id'   => self::tmpIdForAsk,  //模板ID
        'url'           => self::urlWap.$askid,
        'data'          => array(
            'first'     => array(
                'value' => '一旦有人回答,您将第一时间收到消息,请留意微信哦~',
                'color' => self::tmpMsgColor,
            ), //标题
            'keyword1'  => array(
                'value' => mb_substr($askInfo['ask_info']['title'],0,13,'UTF8').'...',
                'color' => self::tmpMsgColor,
            ), //问题
            'keyword2'  => array(
                'value' => '暂无', 
                'color' => self::tmpMsgColor,
            ), //答案
            'keyword3'  => array(
                'value' => date('Y-m-d H:i',$askInfo['ask_info']['create_time']),
                'color' => self::tmpMsgColor,
            ), //提交时间
            //'remark'        => array(
            //    'value' => '问题一旦被解答,我们将第一时间给你推送答案~ 平均5分钟就会有答案,亲,稍候片刻哦~',
            //    'color' => self::tmpMsgColor,
            // ), //引导语

        ),
    );
    if(!$msgBody['touser'] || !$msgBody['template_id'] || !is_array($msgBody['data'])){
        self::writeLog('~~sendtmplatemessages error~~'.json_encode($msgBody));
        return false;    
    }
    $url       = self::getPostPath(self::urlSendTmpMsg);
    $msgResult = WeixinApiRequest::post($url,$msgBody);
    if($msgResult['errmsg']!='ok'){
        return false;
    }else{
        return true;
    }
}/*}}}*/

 URL工具处理方法:

/**
* @Desc   : 处理post请求URL路径
* @Date   : 2015-12-16 14:12:06
*/
private static function getPostPath($urlPath)
{/*{{{*/

    $tmpStr = strpos('?',$urlPath) ? '&access_token=' : '?access_token=';
    return    $urlPath.$tmpStr.self::getAccessToken();

}/*}}}*/