企業(yè)號(hào)微信對(duì)接自己服務(wù)器
1.本地服務(wù)器與微信服務(wù)器的信任
- 本地具有獨(dú)立外網(wǎng)ip服務(wù)器獲取微信服務(wù)器的ip段
#CropID、Secret微信企業(yè)號(hào)中“設(shè)置--功能--權(quán)限管理”可以找到
CropID='*******'
Secret='*******'
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"
Gtoken=$(/usr/bin/curl -s -G "$GURL" | awk -F\" '{print $4}')
/usr/bin/curl "https://qyapi.weixin.qq.com/cgi-bin/getcallbackip?access_token=$Gtoken"
- 在iptables 加入如下例子:
#將取得的ip加入信任中(使其可以訪問到這個(gè)服務(wù)器上的資源)
-A INPUT -s 101.226.103.0/24 -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
2.本地部署微信加密文件
在本地服務(wù)器(具有外網(wǎng)ip)部署php+nginx,使其可以在公網(wǎng)訪問
- 下載加解密庫(kù)部署在自己服務(wù)器上,使其可以在網(wǎng)絡(luò)上訪問,本文以php為例
//filename:index.php
//回調(diào)驗(yàn)證
<?php
//添加騰訊提供的接口文件WXBizMsgCrypt.php由上文連接官方下載
include_once "inc/WXBizMsgCrypt.php";
//設(shè)置自己企業(yè)號(hào)的相關(guān)參數(shù)
$encodingAesKey="";//企業(yè)微信中應(yīng)用中心開回調(diào)模式時(shí)自定義
$corpId="";//企業(yè)微信中設(shè)置找
$token="";//企業(yè)微信中應(yīng)用中心開回調(diào)模式時(shí)自定義
//獲取待驗(yàn)證的參數(shù)
$sVerifyMsgSig = $_GET["msg_signature"];
$sVerifyTimeStamp = $_GET["timestamp"];
$sVerifyNonce = $_GET["nonce"];
$sVerifyEchoStr = $_GET["echostr"];
//以企業(yè)號(hào)參數(shù)為參數(shù)生成解碼對(duì)象
$wxcpt = new WXBizMsgCrypt($token, $encodingAesKey, $corpId);
//進(jìn)行地址解析
$errCode = $wxcpt->VerifyURL($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $sVerifyEchoStr, $sEchoStr);
if ($errCode == 0) {
//如果沒有異常就返回加密的echostr的明文
echo $sEchoStr;
} else {
//出現(xiàn)異常就返回異常編碼
echo $errCode;
}
?>
- 微信所需文件路徑結(jié)構(gòu)
├── inc
│ ├── errorCode.php
│ ├── pkcs7Encoder.php
│ ├── sha1.php
│ ├── WXBizMsgCrypt.php
│ └── xmlparse.php
├── index.php
3.開啟回調(diào)模式
-
在自己創(chuàng)建的應(yīng)用上開啟回調(diào)模式填入U(xiǎn)RL
開啟回調(diào)模式 驗(yàn)證成功后將index.php改成如下可實(shí)現(xiàn)接收信息并做出反饋
<?php
include_once "inc/WXBizMsgCrypt.php";
//用戶參數(shù)
$encodingAesKey = "";
$token = "";
$corpId = "";
//回調(diào)認(rèn)證
$sVerifyMsgSig =$_GET["msg_signature"];
$sVerifyTimeStamp = $_GET["timestamp"];
$sVerifyNonce = $_GET["nonce"];
$sVerifyEchoStr =$_GET["echostr"];
$EchoStr = "";
$wxcpt = new WXBizMsgCrypt($token, $encodingAesKey, $corpId);
$errCode = $wxcpt->VerifyURL($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $sVerifyEchoStr, $sEchoStr);
if ($errCode == 0) {
print $sEchoStr;
} else {
print($errCode . "\n\n");
}
//接收信息
$sReqData = file_get_contents("php://input");
$sMsg = ""; // 解析之后的明文
$errCode = $wxcpt->DecryptMsg($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $sReqData, $sMsg);
if ($errCode == 0) {
file_put_contents('msg/'.$sVerifyTimeStamp.'.txt', $sMsg);//明文輸出收到的xml
$xml = new DOMDocument();
$xml->loadXML($sMsg);
$content = $xml->getElementsByTagName('Content')->item(0)->nodeValue;//文字信息的內(nèi)容
$FromUserName = $xml->getElementsByTagName('FromUserName')->item(0)->nodeValue; //發(fā)送用戶的id
$MsgType = $xml->getElementsByTagName('MsgType')->item(0)->nodeValue; //消息類型
$Event = $xml->getElementsByTagName('Event')->item(0)->nodeValue; //事件類型
$ScanResult = $xml->getElementsByTagName('ScanResult')->item(0)->nodeValue; //掃描結(jié)果
$PicUrl = $xml->getElementsByTagName('PicUrl')->item(0)->nodeValue;//圖片鏈接
$Latitude = $xml->getElementsByTagName('Latitude')->item(0)->nodeValue; //經(jīng)度
$Longitude = $xml->getElementsByTagName('Longitude')->item(0)->nodeValue;//緯度
$Precision = $xml->getElementsByTagName('Precision')->item(0)->nodeValue; //精度
$MediaId = $xml->getElementsByTagName('MediaId')->item(0)->nodeValue;//資源ID
} else {
print($errCode . "\n\n");
}
//根據(jù)接收信息做反饋-文字
if($MsgType == "text") {
switch($content){
case "1":
$mycontent="我收到了數(shù)字1";
$MsgType=text;
break;
case "2":
$mycontent="我收到了數(shù)字2";
$MsgType=text;
break;
case "3":
$mycontent="我收到了數(shù)字3";
$MsgType=text;
break;
default :
$mycontent="輸入123試試";//默認(rèn)回復(fù)
$MsgType=text;
break;
}
}
//返回文字信息
/*$sRespData =
"<xml>
<ToUserName><![CDATA[".$reqFromUserName."]]></ToUserName>
<FromUserName><![CDATA[".$corpId."]]></FromUserName>
<CreateTime>".sReqTimeStamp."</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[".$mycontent."]]></Content>
</xml>";'*/
//返回圖文新聞信息,更多方法參考官方接口文檔,被動(dòng)響應(yīng)消息里邊的代碼更換XML文件
$sRespData =
"<xml>
<ToUserName><![CDATA[".$reqFromUserName."]]></ToUserName>
<FromUserName><![CDATA[".$corpId."]]></FromUserName>
<CreateTime>".sReqTimeStamp."</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title><![CDATA[測(cè)試]]></Title>
<Description><![CDATA[詳細(xì)內(nèi)容]]></Description>
<PicUrl><![CDATA[http://www.baidu.com/images/logo.jpg]]></PicUrl>
<Url><![CDATA[http://www.baidu.com]]></Url>
</item>
</Articles>
</xml>";
$sEncryptMsg = ""; //xml格式的密文
$errCode = $wxcpt->EncryptMsg($sRespData, $sReqTimeStamp, $sReqNonce, $sEncryptMsg);
if ($errCode == 0) {
print($sEncryptMsg);
} else {
print($errCode . "\n\n");
}
?>
-
自己可以根據(jù)業(yè)務(wù)需求,接收到相應(yīng)信息后,執(zhí)行本地腳本并返回操作結(jié)果實(shí)現(xiàn)微信控制zabbix。
最終效果

