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

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

WBOY
WBOYOriginal
2016-05-25 16:46:381037browse

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

1、需要设置一个 TOKEN 信息

efine("TOKEN", "weixin");

这个 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;
}
?>

讲解一下:

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

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


永久链接:

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

Statement:
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