关闭

[置顶] 最简单的PHP接入微信消息事件

标签: php微信
56人阅读 评论(0) 收藏 举报
分类:

话不多说直接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;
    }

}  

?>
1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:514次
    • 积分:99
    • 等级:
    • 排名:千里之外
    • 原创:9篇
    • 转载:2篇
    • 译文:0篇
    • 评论:2条
    文章分类
    文章存档
    最新评论