微信公众号开发教程 接口配置与身份验证
要想成为微信公众号的开发者,首先必须有可以对外访问的服务器。微信公众号的开发不限制使用什么语言,这里我们使用PHP作为开发语言。
接口接入配置
登录微信公众平台官网后,在公众平台后台管理页面 - 开发者中心页,点击“修改配置”按钮,填写服务器地址(URL)、Token和EncodingAESKey,其中URL是开发者用来接收微信消息和事件 的接口URL。Token可由开发者可以任意填写,用作生成签名(该Token会和接口URL中包含的Token进行比对,从而验证安全性)。 EncodingAESKey由开发者手动填写或随机生成,将用作消息体加解密密钥。
同时,开发者可选择消息加解密方式:明文模式、兼容模式和安全模式。模式的选择与服务器配置在提交后都会立即生效,请开发者谨慎填写及选择。加解密方式的默认状态为明文模式,选择兼容模式和安全模式需要提前配置好相关加解密代码,详情请参考消息体签名及加解密部分的文档。
身份验证
填写完接口信息以后,开始进行进行身份验证同时也是验证服务器的有效性。在我们提交信息以后,微信服务器会发送GET请求到我们上面填写的URL上。在上面填写的URL地址中我们可以看到,我们填写了一个php文件。没错,我们就是用这个PHP文件来进行验证的。
部分验证代码如下
public function valid(){
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
private function checkSignature(){
// you must define TOKEN by yourself
if (!defined("TOKEN")) {
throw new Exception('TOKEN is not defined!');
}
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
// use SORT_STRING rule
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
通过上面的代码我们可以看到,微信服务器会向我们的服务器地址发送四个参数,分别是:
signature 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
timestamp 时间戳
nonce 随机数
echostr 随机字符串
那么token又是从哪里来的呢?这里的token就是我们上面填写的Token——URL下面的字段。所以在我们的验证代码中需要定义token常量
define("TOKEN",”我们填写的token”); //注意,这里的值必须和我们上面填写的一致
php完整代码下载
我们的验证代码准备完成以后,提交信息,我们会惊喜的看到在顶部会提示我们配置成功。
接入成功以后,我们要想着启用我们的接口,然后我们就可以根据开发文档来实现我们想要的效果了。
相关文章
WeChat public account development tutorial interface configuration and identity a
发布时间:2025/03/16 浏览次数:133 分类:NETWORK
-
To become a developer of WeChat official account, you must first have a server that can be accessed externally. The development of WeChat official account does not limit the use of any language. Here we use PHP as the development language.
WeChat public account development tutorial to obtain access_token
发布时间:2025/03/16 浏览次数:75 分类:NETWORK
-
During the development of WeChat official accounts, if we want to actively push messages to the WeChat server, we must have access_token. Access_token is the only ticket for the official account. When we develop and call various WeChat inte
WeChat public account receives messages and processes ordinary messages
发布时间:2025/03/16 浏览次数:153 分类:NETWORK
-
There are two situations when WeChat users interact with public accounts: one is when WeChat users send ordinary messages to public accounts; the other is when some operations of WeChat users cause the WeChat server to notify the URL filled
WeChat public account receiving message event message processing
发布时间:2025/03/16 浏览次数:80 分类:NETWORK
-
As we know, there are two types of messages generated by the interaction between WeChat users and public accounts: one is ordinary messages, which are introduced in detail in the article "Ordinary message processing for WeChat public accoun
微信公众号接收消息 事件消息处理
发布时间:2016/10/18 浏览次数:4683 分类:网络
-
事件消息分为关注/取消关注、扫描带参数的二维码、上报地理位置、自定义菜单、点击菜单拉取消息、点击菜单跳转链接共六种事件。
微信公众号接收消息 普通消息处理
发布时间:2016/10/14 浏览次数:2391 分类:网络
-
在微信用户和公众号产生交互的过程中会分为两种情况:一种是微信用户向公众号发送普通消息;另一种是微信用户的某些操作使得微信服务器通过事件推送的形式通知到开发者填写的
微信公众号开发教程获取access_token
发布时间:2016/10/12 浏览次数:4451 分类:网络
-
在微信公众号开发过程中,如果我们想要主动向微信服务器推送消息那么我们必须要有access_token。access_token 是公众号的唯一票据。这里我们介绍获取access_token的方法及使用案例。