话不多说直接上代码(如果有用的话-记得打赏哦)
/**
* 从证书中获取私钥
* @param $cert_path (微信平台证书---从《获取平台证书》接口中获得)
* @return bool
*/
function certPubKey($cert_path){
$cert_path = file_get_contents($cert_path);
$pub_key = openssl_pkey_get_public($cert_path);
if($pub_key){
$keyData = openssl_pkey_get_details($pub_key);
return $keyData['key'];
}
return false;
}
/**
* 验证回调主方法
* @param $http_data header头的信息(微信回调发送的header头)
* @param $body 应答主体信息 (微信发送的数据)(未解密的数据!!!)
* @return int 验证成功返回 1 失败返回 0
*/
function verifySigns($http_data,$body){
$wechatpay_timestamp = $http_data['Wechatpay-Timestamp'];
$wechatpay_nonce = $http_data['Wechatpay-Nonce'];
$wechatpay_signature = $http_data['Wechatpay-Signature'];
$signature = base64_decode($wechatpay_signature);
$pub_key = certPubKey('wxpay_key/cert.pem');//平台证书路径
$body = str_replace('\\','',$body);
$message =
$wechatpay_timestamp . "\n" .
$wechatpay_nonce . "\n" .
$body . "\n";
$res = openssl_verify($message,$signature, $pub_key,OPENSSL_ALGO_SHA256);
if ($res == 1) {
return 1;
}
return 0;
}
|