最简单的PHP接入微信消息事件

话不多说直接Coding

<?php

header("Content-Type:text/html; charset=utf-8");  
define("TOKEN", "weixin");  

$wechatObj = new wechatCallbackapiTest();  
//$wechatObj->valid();  
$wechatObj->responseMsg();
// 校验
// 校验


class wechatCallbackapiTest  
{  
    public function valid()  
    {  
        $echoStr = $_GET["echostr"];  

        //valid signature , option  
        if($this->checkSignature()){  
            header('content-type:text');  
            echo $echoStr;  
            exit;  
        }  
    }   

    public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

        //extract post data
        if (!empty($postStr)){

                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $type = $postObj->MsgType;
                $customrevent = $postObj->Event;
                $keyword = trim($postObj->Content);
                $scanresult=trim($postObj->ScanCodeInfo->ScanResult);
                $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($type=="event" and $customrevent=="subscribe"){
                    $contentStr = "欢迎关注\n";                    
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                    echo $resultStr;
                }   
                //省略微信多类事件。。。
                else{

                    $this->textMsg($fromUsername,$toUsername,$time);
                }

        }else {
            echo "";
            exit;
        }
    }

    private function checkSignature()  
    {  
        $signature = $_GET["signature"];  
        $timestamp = $_GET["timestamp"];  
        $nonce = $_GET["nonce"];      

        $token = TOKEN;  
        $tmpArr = array($token, $timestamp, $nonce);  
        sort($tmpArr);  
        $tmpStr = implode( $tmpArr );  
        $tmpStr = sha1( $tmpStr );  

        if( $tmpStr == $signature ){  
            return true;  
        }else{  
            return false;  
        }  
    }
    //接入多客服
    protected function textMsg($fromUsername,$toUsername,$time){
        $textTpl="<xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[transfer_customer_service]]></MsgType>

                </xml>";
        //$msgType="transfer_customer_service";
        $resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time);
        echo $resultStr;
    }

}  

?>
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013063880/article/details/78476964
文章标签: php 微信
个人分类: 微信开发
想对作者说点什么? 我来说一句

微信模板消息发送php

2016年05月05日 2KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭