微信公众号接入

Yii2 2017-12-15 浏览(2549 评论(0

第一步:填写服务器配置

图片描述

第二步:验证消息的确来自微信服务器

1.创建控制器(WxController)

<?php
namespace frontend\controllers;
use Yii;
use yii\web\Controller;
use frontend\components\wechatCallbackapiTest;

class WxController extends Controller
{

        public $enableCsrfValidation;

        public function actionIndex(){
                $wechatObj = new wechatCallbackapiTest();
                if (!isset($_GET['echostr'])) {
                    $wechatObj->responseMsg();
                }else{
                    $wechatObj->valid();
                }
        }
}

2.创建wechatCallbackapiTest;

<?php
namespace frontend\components;
use Yii;
define("TOKEN", "*****");//配置自己的token

class wechatCallbackapiTest{

    //验证签名
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }

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

    //响应消息
    public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        if (!empty($postStr)){
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $RX_TYPE = trim($postObj->MsgType);

            switch ($RX_TYPE)
            {
                case "text":
                    $result = $this->receiveText($postObj);
                    break;
                case "event":
                    $result = $this->receiveEvent($postObj);
                    break;
            }
            echo $result;
        }else {
            echo "";
            exit;
        }
    }

    //接收文本消息
    private function receiveText($object)
    {
        $words = trim($object->Content);
        if($words){
                $content = "欢迎关注";
                $result = $this->transmitText($object, $content);
        }
        return $result;
    }

    private function receiveEvent($object)
    {
        $content = "";
        $str = "嘿,终于等到你!还好我们都没放弃,感谢您关注【微信公众号】";
        switch ($object->Event)
        {
            case "subscribe":
                $content = $str;
                break;
            case "unsubscribe":
                $content = "取消关注";
                break;
        }
        $result = $this->transmitText($object, $content);
        return $result;
    }

    //接受文本消息
    private function transmitText($object, $content)
    {
        $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
        return $result;
    }


}

?>

打赏

如果此文对你有所帮助,请随意打赏鼓励作者^_^

黄信强博客