微信公众平台消息接口校验与消息接口响应例子
开发微信公众平台消息接口过程中,我们首先需要验证消息接口的有效性,验证通过后,才可以进行其他开发。首先我们来看一下微信公众平台给我的 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['echostr'])) { $echoStr = $_GET["echostr"]; //valid signature , option if ($this->checkSignatureAction()) { echo $echoStr; exit; } } else { $this->responseMsgAction(); } return FALSE; } ?>
讲解一下:
因为在验证接口有效性时传递了个 echostr 参数,而在消息接口响应过程中没有这个参数,所有我们这里用这个参数来判断是验证接口还是接口响应:
isset($_GET['echostr'])
永久链接:
转载随意!带上文章地址吧。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

