You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
4.4 KiB

2 months ago
<?php
// 定义命名空间,用于组织代码
2 months ago
namespace LaneWeChat\Core;
// Wechat类定义用于处理微信相关功能
class Wechat {
// 私有属性,用于调试模式和请求数据
2 months ago
private $debug;
private $request;
// 构造函数用于初始化Wechat对象
2 months ago
public function __construct($token, $debug = FALSE) {
// 检查是否是微信服务器的验证请求,并验证签名
2 months ago
if ($this->isValid() && $this->validateSignature($token)) {
// 如果验证通过返回echostr给微信服务器
2 months ago
return $_GET['echostr'];
}
// 设置调试模式
2 months ago
$this->debug = $debug;
// 解析微信发送的XML数据
2 months ago
$xml = (array) simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA'], 'SimpleXMLElement', LIBXML_NOCDATA);
// 将XML数据转换为数组并转换所有键为小写
2 months ago
$this->request = array_change_key_case($xml, CASE_LOWER);
}
// 私有方法isValid用于检查是否是微信服务器的验证请求
2 months ago
private function isValid() {
// 检查是否存在echostr参数该参数在微信服务器验证时存在
2 months ago
return isset($_GET['echostr']);
}
// 私有方法validateSignature用于验证签名
2 months ago
private function validateSignature($token) {
// 获取微信服务器发送的signature、timestamp、nonce参数
2 months ago
$signature = $_GET['signature'];
$timestamp = $_GET['timestamp'];
$nonce = $_GET['nonce'];
// 将token、timestamp、nonce组合并排序
2 months ago
$signatureArray = array($token, $timestamp, $nonce);
sort($signatureArray, SORT_STRING);
// 计算签名并比较
2 months ago
return sha1(implode($signatureArray)) == $signature;
}
// 受保护的方法getRequest用于获取请求参数
2 months ago
protected function getRequest($param = FALSE) {
// 如果没有指定参数,则返回所有请求参数
2 months ago
if ($param === FALSE) {
return $this->request;
}
// 将参数名转为小写
2 months ago
$param = strtolower($param);
// 如果请求参数存在,则返回该参数的值
2 months ago
if (isset($this->request[$param])) {
return $this->request[$param];
}
// 如果请求参数不存在则返回NULL
2 months ago
return NULL;
}
// 公共方法run用于处理微信请求
2 months ago
public function run() {
// 根据请求类型分发处理
2 months ago
return WechatRequest::switchType($this->request);
}
// 公共方法checkSignature用于检查签名
public function checkSignature() {
// 获取微信服务器发送的signature、timestamp、nonce参数
2 months ago
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
// 获取配置中的token
2 months ago
$token = WECHAT_TOKEN;
// 将token、timestamp、nonce组合并排序
2 months ago
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
// 将组合后的字符串转为sha1签名
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
2 months ago
// 比较计算出的签名和微信服务器发送的签名
if ($tmpStr == $signature) {
// 如果签名匹配返回echostr给微信服务器
2 months ago
echo $_GET['echostr'];
return true;
} else {
// 如果签名不匹配返回false
2 months ago
return false;
}
}
}
命名空间声明namespace LaneWeChat\Core; 定义了类的命名空间,用于组织代码。
类定义class Wechat 定义了一个用于处理微信相关功能的类。
私有属性private $debug; 和 private $request; 分别用于存储调试模式和请求数据。
构造函数public function __construct($token, $debug = FALSE) 初始化Wechat对象验证微信服务器的请求并解析微信发送的XML数据。
isValid方法private function isValid() 检查是否是微信服务器的验证请求。
validateSignature方法private function validateSignature($token) 验证微信服务器发送的签名。
getRequest方法protected function getRequest($param = FALSE) 获取请求参数,如果没有指定参数,则返回所有请求参数。
run方法public function run() 处理微信请求,根据请求类型分发处理。
checkSignature方法public function checkSignature() 检查签名,验证微信服务器发送的签名是否匹配。