当前位置:   article > 正文

laravel 实现微信公众号关注回复文本消息_php 关注微信公众号 回调接口

php 关注微信公众号 回调接口

微信公众号文本消息

  • 首先需要创建一个微信公众号

  • 在基本配置中添加服务器配置(已启用),验证接口

  • 代码

<?php

namespace App\Http\Controllers\Weixin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class IndexController extends Controller
{
    public function checkSignature(Request $request)
    {
        if($request->method()=='GET'){
            $signature = $_GET['signature'];
            $timestamp = $_GET['timestamp'];
            $nonce = $_GET['nonce'];
            $token = 'mayuliang';
            $tmpArr = array($token, $timestamp, $nonce);
            sort($tmpArr);
            $tmpStr = sha1(implode($tmpArr));
            if($tmpStr == $signature && isset($_GET['echostr']) && $_GET['echostr']) {
                echo  $_GET['echostr'];
                Log::error('success', ['message'=>'success']);
                exit;
            }
        } else { 
            echo $this->reponseMsg();
        }
    }

    public function reponseMsg()
    {
        $postStr = file_get_contents("php://input");
        $postArr = json_decode(json_encode(simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        Log::error('error', ['message'=>$postArr['FromUserName']]);
        $FromUserName = $postArr['FromUserName']; //获取发送方帐号(OpenID)
        $ToUserName = $postArr['ToUserName']; //获取接收方账号
        Log::error('error',['message'=>$postArr['MsgType']]);
        if (strtolower($postArr['MsgType'])=='event') {
            if(strtolower($postArr['Event'])=='subscribe'){
                $template = "<xml>
                              <ToUserName><![CDATA[%s]]></ToUserName>
                              <FromUserName><![CDATA[%s]]></FromUserName>
                              <CreateTime>%s</CreateTime>
                              <MsgType><![CDATA[%s]]></MsgType>
                              <Content><![CDATA[%s]]></Content>
                            </xml>";
                $content = '欢迎关注';
                $type = "text";
                $resultStr = sprintf($template,$FromUserName,$ToUserName, time(), $type, $content);
                Log::error('error', ['message'=>$resultStr]);
                return $resultStr;
            }
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

遇到的坑:

  1. laravel框架需要处理验证,需要去 Kernel.php 中去掉 VerifyCsrfToken::class
  2. 接收到的ToUserName 是目标 FromUserName
  3. 网上很多simplexml_load_string($postStr, ‘SimpleXMLElement’, LIBXML_NOCDATA)但是我没有获取到,json_decode(json_encode(simplexml_load_string($postStr, ‘SimpleXMLElement’, LIBXML_NOCDATA)), true)
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/449270
推荐阅读
相关标签
  

闽ICP备14008679号