Home php教程 php手册 微信公众平台消息接口校验与消息接口响应例子

微信公众平台消息接口校验与消息接口响应例子

May 25, 2016 pm 04:46 PM

开发微信公众平台消息接口过程中,我们首先需要验证消息接口的有效性,验证通过后,才可以进行其他开发。首先我们来看一下微信公众平台给我的 PHP SDK

1、需要设置一个 TOKEN 信息

efine("TOKEN", "weixin");
Copy after login

这个 TOKEN 信息可以由开发者自行设定。

2、有个 wechatCallbackapiTest 类,该类中含有3个方法:valid、responseMsg、checkSignature,其中方法valid、checkSignature是用作验证接口URL有效性用的,responseMsg是我们使用最多的方法,后续大部分的开发工作都会在这里完成。

在我们对接口进行有效性验证通过后,就不再需要进行验证了,这时我们就需要使用到 responseMsg 方法。那么如何保证接口验证通过后,不需要去切换其中的方法或修改其中的代码呢?

我是这样做的:

<?php
public function indexAction() {
    if (isset($_GET[&#39;echostr&#39;])) {
        $echoStr = $_GET["echostr"];
        //valid signature , option
        if ($this->checkSignatureAction()) {
            echo $echoStr;
            exit;
        }
    } else {
        $this->responseMsgAction();
    }
    return FALSE;
}
?>
Copy after login

讲解一下:

因为在验证接口有效性时传递了个 echostr 参数,而在消息接口响应过程中没有这个参数,所有我们这里用这个参数来判断是验证接口还是接口响应:

isset($_GET[&#39;echostr&#39;])
Copy after login


永久链接:

转载随意!带上文章地址吧。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)