1.下载官方weworkapi_php-master.zip
2.解压后将callback目录改名为workchat复制到项目extend
3.在接口文件中引入WXBizMsgCrypt.php文件
include_once (__DIR__."/../../../extend/workchat/WXBizMsgCrypt.php");
4.
/*
------------验证回调URL---------------
*企业开启回调模式时,企业号会向验证url发送一个get请求
假设点击验证时,企业收到类似请求:
* GET /cgi-bin/wxpush?msg_signature=5c45ff5e21c57e6ad56bac8758b79b1d9ac89fd3×tamp=1409659589&nonce=263014780&echostr=P9nAzCzyDtyTWESHep1vC5X9xho%2FqYX3Zpb4yKa9SKld1DsH3Iyt3tP3zNdtp%2B4RPcs8TgAE7OaBO%2BFZXvnaqQ%3D%3D
* HTTP/1.1 Host: qy.weixin.qq.com
接收到该请求时,企业应
1.解析出Get请求的参数,包括消息体签名(msg_signature),时间戳(timestamp),随机数字串(nonce)以及公众平台推送过来的随机加密字符串(echostr),
这一步注意作URL解码。
2.验证消息体签名的正确性
3. 解密出echostr原文,将原文当作Get请求的response,返回给公众平台
第2,3步可以用公众平台提供的库函数VerifyURL来实现。
*/
public function notify(){
$data = request()->get();
$msg_signature = $data["msg_signature"];
$timestamp = $data["timestamp"];
$nonce = $data["nonce"];
$echostr = $data["echostr"];
// 需要返回的明文
$sEchoStr = "";
$token = "BstIH********ehw";
$encodingAesKey = "7SKg5xFLvu*********HPhGBe4VGW9gHseHdb";
$wxcpt = new \WXBizMsgCrypt($token, $encodingAesKey, config("workchat.corp_id"));
$errCode = $wxcpt->VerifyURL($msg_signature, $timestamp, $nonce, $echostr, $sEchoStr);
//trace($errCode);
//trace($sEchoStr);
if ($errCode == 0) {
echo $sEchoStr;
} else {
print("ERR: " . $errCode . "\n\n");
}
}