您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
微信开放平台事件消息解密(代授权)php7
发布时间:2022-10-12 20:32:52编辑:雪饮阅读()
在微信开放平台代授权公众号业务中,有一种需求是要订阅消息,而订阅消息若用户已经订阅,则订阅组件是无法唤醒弹窗的,但是会导致前端需要点击两次后才能进行其它业务(可能是前端某些实现导致)。但这个问题由于是前端的,所以我们做后端的是可以不用管的。
但是前端提出需要判断该用户是否订阅。
那么微信对于用户订阅是有事件通知的,但是通知据我最近研究发现在那个弹窗哪里同意了订阅消息会推送过来,但是如果是长期订阅消息(一次性订阅消息即长期订阅消息,也可理解为永久订阅消息,发现后面订阅后就无法取消了,只能在对应公众号哪里不勾选(不勾选只是给你推送时候你无法收到,并且推送接口也会报错大概意思就是用户拒收消息))。
那么有两种实现用户订阅的判断,一种是基于上面的推送事件,还有就是前端判断用户同意后就请求我的一个接口进行记录。
我这里是两个都做了。
对于微信推送来说,微信官方给的php的sdk过旧,现在都是用命名空间了,并且php7里面类的构造函数已经不推荐用同类名的方法了,这些你要手动全部改过来,然后还有一些方法在php7里面不太实用了。
那么实例如:
首先是接收到微信那边的事件推送:
$input_content = file_get_contents("php://input");
Log::write("消息与事件接收消息=>密文消息体:".$input_content,'info');
$token=$this->wpp_token;
$aes_key=$this->wpp_aes_key;
$appid2=$this->wpp_appid;
$decode_params=compact("token","aes_key","appid2");
Log::write("消息与事件接收消息=>WXBizMsgCrypt入参:".var_export($decode_params,true),'info');
//开放平台token,开放平台aes_key,开放平台appid
$pc = new WXBizMsgCrypt($token,$aes_key,$this->wpp_appid);
$msg = '';
$errCode = $pc->decryptMsg($get_params["msg_signature"], $get_params["timestamp"], $get_params["nonce"], $input_content, $msg);
if ($errCode == 0) {
print("解密后: " . $msg . "\n");
Log::write("消息与事件接收消息=>解密后:".$msg,'info');
} else {
Log::write("消息与事件接收消息=>解密错误:".$errCode,'info');
print($errCode . "\n");
}
这里用到的微信sdk里面的WXBizMsgCrypt类调用了另外一个类Prpcrypt,那么我这里是Prpcrypt类里面的解密方法decrypt的修改,其它保持不变。
如:
<?php
namespace app\api\controller\h5\wechat\lib;
use app\api\controller\h5\wechat\lib\ErrorCode;
use app\api\controller\h5\wechat\lib\PKCS7Encoder;
class Prpcrypt
{
public $key;
public function __construct($k)
{
$this->key = base64_decode($k . "=");
}
/**
* 对明文进行加密
* @param string $text 需要加密的明文
* @return string 加密后的密文
*/
public function encrypt($text, $appid)
{
try {
//获得16位随机字符串,填充到明文之前
$random = $this->getRandomStr();
$text = $random . pack("N", strlen($text)) . $text . $appid;
// 网络字节序
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
$iv = substr($this->key, 0, 16);
//使用自定义的填充方式对明文进行补位填充
$pkc_encoder = new PKCS7Encoder;
$text = $pkc_encoder->encode($text);
mcrypt_generic_init($module, $this->key, $iv);
//加密
$encrypted = mcrypt_generic($module, $text);
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
//print(base64_encode($encrypted));
//使用BASE64对加密后的字符串进行编码
return array(ErrorCode::$OK, base64_encode($encrypted));
} catch (\Exception $e) {
//print $e;
return array(ErrorCode::$EncryptAESError, null);
}
}
public function decrypt($encrypted='',$appid)
{
try {
if(version_compare(PHP_VERSION, '7','>=')) {
//以下注意!!!!!
$iv = substr($this->key, 0, 16);
$encrypted=str_replace(' ','+',$encrypted);
$decrypted = openssl_decrypt($encrypted,'AES-256-CBC',substr($this->key, 0, 32),OPENSSL_ZERO_PADDING,$iv);
} else {
//使用BASE64对需要解密的字符串进行解码
$ciphertext_dec = base64_decode($encrypted);
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
$iv = substr($this->key, 0, 16);
mcrypt_generic_init($module, $this->key, $iv);
//解密
$decrypted = mdecrypt_generic($module, $ciphertext_dec);
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
}
} catch (\Exception $e) {
return array(ErrorCode::$DecryptAESError, null);
}
try {
//去除补位字符
$pkc_encoder = new PKCS7Encoder;
$result = $pkc_encoder->decode($decrypted);
//去除16位随机字符串,网络字节序和AppId
if (strlen($result) < 16)
return "";
$content = substr($result, 16, strlen($result));
$len_list = unpack("N", substr($content, 0, 4));
$xml_len = $len_list[1];
$xml_content = substr($content, 4, $xml_len);
$from_appid = substr($content, $xml_len + 4);
if(version_compare(PHP_VERSION, '7','>=')) {
if (!$appid) {
//如果传入的appid是空的,则认为是订阅号,使用数据中提取出来的appid
$appid = $from_appid;
}
}
} catch (\Exception $e) {
//print $e;
return array(ErrorCode::$IllegalBuffer, null);
}
if ($from_appid != $appid)
return array(ErrorCode::$ValidateAppidError, null);
//不注释上边两行,避免传入appid是错误的情况
if(version_compare(PHP_VERSION, '7','>=')) {
//增加appid,为了解决后面加密回复消息的时候没有appid的订阅号会无法回复
return array(0, $xml_content, $from_appid);
} else {
return array(0, $xml_content);
}
}
/**
* 随机生成16位字符串
* @return string 生成的字符串
*/
function getRandomStr()
{
$str = "";
$str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
$max = strlen($str_pol) - 1;
for ($i = 0; $i < 16; $i++) {
$str .= $str_pol[mt_rand(0, $max)];
}
return $str;
}
}
关键字词:微信,开放,平台,事件,消息,解密,代,授权,php,7