版权声明:本文为原创文章,未经允许不得转载。
tp5框架下的代码片段
/**
* 验证回调URL
* 推送suite_ticket
*/
public function getSuiteTicket(){
$encodingAesKey = "";
$token = "";
$corpId = "";// 企业ID
$suiteId = '';// 应用id
$sVerifyMsgSig = $_GET['msg_signature'];
$sVerifyTimeStamp = $_GET['timestamp'];
$sVerifyNonce = $_GET['nonce'];
include_once EXTEND_PATH."Weixin/WXBizMsgCrypt.php";
if(!empty($_GET['echostr'])){
$sVerifyEchoStr = $_GET['echostr'];
$sEchoStr = "";
$wxcpt = new \WXBizMsgCrypt($token, $encodingAesKey, $corpId);
$errCode = $wxcpt->VerifyURL($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $sVerifyEchoStr, $sEchoStr);
if ($errCode == 0) {
// 验证URL成功,将sEchoStr返回
echo $sEchoStr;
exit;
} else {
print("ERR: " . $errCode . "\n\n");
}
}
file_put_contents(RUNTIME_PATH."tmp.txt", $_SERVER['REQUEST_METHOD'], FILE_APPEND);
file_put_contents(RUNTIME_PATH."tmp.txt", file_get_contents("php://input"), FILE_APPEND);
//判断企业微后台是否推送suite_ticket到该url,post形式
if($_SERVER['REQUEST_METHOD'] === 'POST' ){
//必须通过输入流方式获取post数据,,接收到的$sReqData 为xml格式,需转换成对象或其他格式
$sReqData = file_get_contents("php://input");
$xml = simplexml_load_string($sReqData,'SimpleXMLElement', LIBXML_NOCDATA); // xml格式转成对象
$ToUserName = $xml->ToUserName;
if($suiteId == $ToUserName){ //证明是企业微信后台推送
include_once EXTEND_PATH."Weixin/WXBizMsgCrypt.php";
$wxcpt = new \WXBizMsgCrypt($token, $encodingAesKey, $ToUserName);
$sMsg = ''; // 解析之后的明文
$err_code = $wxcpt->DecryptMsg($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $sReqData, $sMsg);
$xmls = simplexml_load_string($sMsg,'SimpleXMLElement', LIBXML_NOCDATA); // xml格式转成对象
if($err_code == 0){
switch ($xmls->InfoType) {
case 'suite_ticket'://推送suite_ticket协议每十分钟微信推送一次
$xmls = json_decode(json_encode($xmls), 1);
$suite_ticket = $xmls['SuiteTicket'];
if (!empty($suite_ticket)) {
// 保存下获取到数据
Cache::set("SuiteTicket", $suite_ticket);
echo 'success'; // 返回企业微信消息 success
}
else {
echo 200;//错误信息
}
break;
}
}
}
}
exit;
}
版权声明:本文为weixin_42963159原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。