一、开始开发
1.接入
(1)填写服务器配置
(2)验证服务器地址的有效性
<?php
namespace app\index\controller;
class Index
{
public function index()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = 'wx_token';
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return $_GET["echostr"];
}else{
return false;
}
}
}
(3)依据接口文档实现业务逻辑
2.获取Access token
public function get_access_token($appid,$secret){
$token_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
$token = file_get_contents($token_url);
return $token;
}
public function cacha_token(){
$appid = '123456test';
$secret = 'test1234567891011';
$token = json_decode($this->get_access_token($appid,$secret));
$expires_in = $token->expires_in;
cache('app_access_token',$token->access_token,$expires_in-60);
}
返回说明
正常情况下,微信会返回下述JSON数据包给公众号:
{"access_token":"ACCESS_TOKEN","expires_in":7200}
参数说明
参数 | 说明 |
---|---|
access_token | 获取到的凭证 |
expires_in | 凭证有效时间,单位:秒 |