溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PHP中微信公眾平臺企業(yè)號驗證接口的示例分析

發(fā)布時間:2021-08-25 14:10:04 來源:億速云 閱讀:219 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下PHP中微信公眾平臺企業(yè)號驗證接口的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

具體如下:

微信公眾平臺企業(yè)號驗證接口、回調(diào) PHP版,本人為了解決這個企業(yè)號的驗證和發(fā)送消息的問題,整整研究了幾天時間,因為微信企業(yè)號剛推出來的時候網(wǎng)上資料比較少!后來在一些朋友的幫助下和本人反復(fù)調(diào)試完善下,終于整理得到了比較理想的文檔,經(jīng)親測,實驗成功。

include_once "WXBizMsgCrypt.php";
// 第三方發(fā)送消息給公眾平臺
$encodingAesKey = "rpJmhCphnndiCLIcNKcUmhTn2GQBNjISPU9GfsfOlxx";
$token = "xxxxxxx";
$corpId ="wxa9a0031f24631f9x"; //這里已正確填寫自己的corpid
//公眾號服務(wù)器數(shù)據(jù)
$sReqMsgSig = $sVerifyMsgSig = $_GET['msg_signature'];
$sReqTimeStamp = $sVerifyTimeStamp = $_GET['timestamp'];
$sReqNonce = $sVerifyNonce = $_GET['nonce'];
$sReqData = file_get_contents("php://input");;
$sVerifyEchoStr = $_GET['echostr'];
$wxcpt = new WXBizMsgCrypt($token, $encodingAesKey, $corpId);
if($sVerifyEchoStr){
$sEchoStr = "";
$errCode = $wxcpt->VerifyURL($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $sVerifyEchoStr, $sEchoStr);
if ($errCode == 0) {
print($sEchoStr);
} else {
print($errCode . "\n\n");
}
exit;
}
//decrypt
$sMsg = ""; //解析之后的明文
$errCode = $wxcpt->DecryptMsg($sReqMsgSig, $sReqTimeStamp, $sReqNonce, $sReqData, $sMsg);
if ($errCode == 0) {
$xml = new DOMDocument();
$xml->loadXML($sMsg);
$reqToUserName = $xml->getElementsByTagName('ToUserName')->item(0)->nodeValue;
$reqFromUserName = $xml->getElementsByTagName('FromUserName')->item(0)->nodeValue;
$reqCreateTime = $xml->getElementsByTagName('CreateTime')->item(0)->nodeValue;
$reqMsgType = $xml->getElementsByTagName('MsgType')->item(0)->nodeValue;
$reqContent = $xml->getElementsByTagName('Content')->item(0)->nodeValue;
$reqMsgId = $xml->getElementsByTagName('MsgId')->item(0)->nodeValue;
$reqAgentID = $xml->getElementsByTagName('AgentID')->item(0)->nodeValue;
switch($reqContent){
case "馬云":
$mycontent="您好,馬云!我知道您創(chuàng)建了阿里巴巴!";
break;
case "馬化騰":
$mycontent="您好,馬化騰!我知道創(chuàng)建了企鵝帝國!";
break;
case "史玉柱":
$mycontent="您好,史玉柱!我知道您創(chuàng)建了巨人網(wǎng)絡(luò)!";
break;
default :
$mycontent="你是誰?。?!一邊涼快去!";
break;
}
$sRespData =
"<xml>
<ToUserName><![CDATA[".$reqFromUserName."]]></ToUserName>
<FromUserName><![CDATA[".$corpId."]]></FromUserName>
<CreateTime>".sReqTimeStamp."</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[".$mycontent."]]></Content>
</xml>";
$sEncryptMsg = ""; //xml格式的密文
$errCode = $wxcpt->EncryptMsg($sRespData, $sReqTimeStamp, $sReqNonce, $sEncryptMsg);
if ($errCode == 0) {
//file_put_contents('smg_response.txt', $sEncryptMsg); //debug:查看smg
print($sEncryptMsg);
} else {
print($errCode . "\n\n");
}
} else {
print($errCode . "\n\n");
}
?>

以上是“PHP中微信公眾平臺企業(yè)號驗證接口的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI