接入微信公众号API
2017-01-19编程大全
微信开发"技术"是现在比较流行的移动开发技术,学好它对自己以后的发展都是 非常有帮助的,下面小编从比较基础的方面谈谈接入微信公众号API的三个步骤
微信开发"技术"是现在比较流行的移动开发技术,学好它对自己以后的发展都是 非常有帮助的,下面小编从比较基础的方面谈谈接入微信公众号API的三个步骤
1.填写服务器URL和token:
url 就是我们API文件在服务器的详细地址,例如我的URL就是:http://www.XXX/weixin/api.php
token就是一种凭证,例如我设置为'weixin'
2.应用接入API接口步骤,验证服务器地址的有效性,加密/校验流程如下:
以下是api.php文件
<?php
//2应用接入API接口步骤,验证服务器地址的有效性,加密/校验流程如下:
//2.1.将timestamp,nonce,token 按字典序排序
$timestamp = $_GET['timestamp'];
$nonce = $_GET['nonce'];
$token = 'weixin';
$signature = $_GET['signature'];
$array = array($timestamp,$nonce,$token);
sort($array);
//2.2将排序后的三个参数拼接后用sha1加密
$tmpstr = implode('',$array);//JDToGregorian
$tmpstr = sha1($tmpstr);
//2.3将加密后的字符串与signature 进行对比,判断该请求是否来自微信
if($tmpstr==$signature){
echo $_GET['echostr'];
exit;
}3.以上步骤完成后就是,配置环境,重构验证接口,这里以thinkPHP为例
<?php
//基于tp框架 ,这是API.php文件
//1.定义项目名称
define('APP_NAME','Application');
//2.定义项目路径 define('APP_PATH','Application/');
//3.引入tp核心文件,同时会在Application目录下生产需要的文件 indexAction.php
require('./ThinkPHP/ThinkPHP.php');<?php
class IndexAction extends Action{
public function __construct(){
}
public function index(){
//获得参数 signature nonce token timestamp echoStr
$nonce = $_GET['nonce'];
$token = 'weixin';
$timestamp = $_GET['timestamp'];
$signature = $_GET['signature'];
//形成数组,按字典排序
$array = array($nonce,$timestamp,$token);
sort($array);
//2.2将排序后的三个参数拼接后用sha1加密
$tmpstr = implode('',$array);//JDToGregorian
$tmpstr = sha1($tmpstr);
//2.3将加密后的字符串与signature 进行对比,判断该请求是否来自微信
if($tmpstr==$signature){
echo $_GET['echostr'];
exit;
}
}
} 很赞哦! ()
