微信公众平台消息接口校验与消息接口响应实例,校验实例
本文实例讲述了微信公众平台消息接口校验与消息接口响应的方法。分享给大家供大家参考。具体分析如下:
开发微信公众平台消息接口过程中,我们首先需要验证消息接口的有效性,验证通过后,才可以进行其他开发,首先我们来看一下微信公众平台给我的 PHP SDK.
1、需要设置一个 TOKEN 信息,代码如下:
复制代码 代码如下:
efine("TOKEN", "weixin");
这个 TOKEN 信息可以由开发者自行设定.
2、有个 wechatCallbackapiTest 类,该类中含有3个方法:valid、responseMsg、checkSignature,其中方法valid、checkSignature是用作验证接口URL有效性用的,responseMsg是我们使用最多的方法,后续大部分的开发工作都会在这里完成.
在我们对接口进行有效性验证通过后,就不再需要进行验证了,这时我们就需要使用到 responseMsg 方法,那么如何保证接口验证通过后,不需要去切换其中的方法或修改其中的代码呢?
我是这样做的,代码如下:
复制代码 代码如下:
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'])
希望本文所述对大家的php程序设计有所帮助。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

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

Dreamweaver CS6
Visual web development tools