首先聲明我的程序是php程序。
問題:微信第三方平臺在進行全網發佈時,自動化測試結果返回返回普通文本信息失敗和返回Api文本信息失敗。
看一下官方手冊:
解決問題一:返回普通文本信息失敗
按照官方文檔說法,授權後會模擬粉絲髮送文本消息給他們的測試公衆號,需要注意的是模擬粉絲髮送的內容是一個固定的文本內容:TESTCOMPONENT_MSG_TYPE_TEXT並且是放在content字段中的,而我們需要做的是給他們回覆一個固定的文本消息:TESTCOMPONENT_MSG_TYPE_TEXT_callback就行了。
注:此處微信有點bug,如果按照上述方式做了後,還是檢測返回普通文本信息失敗,只管提交就行,我的就是這種情況,提交全網審覈,兩天後就審覈通過了。
解決問題二:返回Api文本信息失敗
這個檢測要求的目的是:檢測第三方平臺,是否實現了調用已授權的的公衆號的api(這裏是公衆號的客服消息接口)的功能,即第三方平臺代替公衆號回覆消息給粉絲的功能。
解析此過程:
1,微信向第三方平臺的“公衆號消息與事件接收URL” 地址轉發一條來自粉絲的發給授權公衆號的加密文本消息(xml格式)。這條文本消息 Content字段的內容固定爲: QUERY_AUTH_CODE:$query_auth_code$ (見下圖的xml信息)。
2,第三方平臺接收到這條消息後,首先要先解密,然後獲取FromUserName,Content的內容,因爲FromUserName的值就是粉絲的openid,下一步就是要回復消息給這個openid。至於Content的值,我們要從裏面截獲$query_auth_code$。(注意取值的方式,要使用xml的取值格式,我就是跳進了這個坑)
$ToUserName = $xml_tree->getElementsByTagName('ToUserName')->item(0)->nodeValue;
$openid = $xml_tree->getElementsByTagName('FromUserName')->item(0)->nodeValue;
$Content = $xml_tree->getElementsByTagName('Content')->item(0)->nodeValue;
//截取$query_auth_code
$query_auth_code = trim(str_replace("QUERY_AUTH_CODE:", "", $Content));
3,第三方平臺拿着獲取到的$query_auth_code,調用獲取公衆號授權的Api,獲取到該公衆號的授權信息——拿到公衆號的 authorizer_access_token 。注意下面要post給接口的數據裏authorization_code,就是第二步裏拿到的$query_auth_code:
//接口地址 post
https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=xxxx
POST數據示例:
{
"component_appid":"appid_value" ,
"authorization_code": $query_auth_code
}
4,第三方平臺使用第三步獲取到的authorizer_access_token來調用公衆號的“客服消息接口”(見下面的url),回覆消息給粉絲$openid。要注意發送的消息是固定的: $query_auth_code$_from_api ,也就是將第二步裏獲取的 $query_auth_code和“_from_api”拼接成字符串
//http請求方式: POST
https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=authorizer_access_token
發送消息示例:
{
"touser":$openid,
"msgtype":"text",
"text":
{
"content":$query_auth_code$_from_api
}
}
最後,附上我的代碼示例:
$xml = file_get_contents('php://input');
$xml_tree = new \DOMDocument();
$xml_tree->loadXML($xml);
$ToUserName = $xml_tree->getElementsByTagName('ToUserName')->item(0)->nodeValue;
$pc = new wxBizMsgCrypt($param->token, $param->encodingAesKey, $param->appid);
$msg = '';
$errCode = $pc->decryptMsg($msgSignature, $timestamp, $nonce, $xml, $msg);
if ($errCode == 0) {
//判斷自動化測試的專用測試公衆號
if($ToUserName == 'gh_3c884a361561'){
//利用xml取值
$xml_tree->loadXML($msg);
$openid = $xml_tree->getElementsByTagName('FromUserName')->item(0)->nodeValue;
$Content = $xml_tree->getElementsByTagName('Content')->item(0)->nodeValue;
//利用數組取值
$postObj = simplexml_load_string($msg, 'SimpleXMLElement', LIBXML_NOCDATA);
$postObj = json_encode($postObj, JSON_UNESCAPED_UNICODE);
$array = json_decode($postObj, true);
if ($array['MsgType'] == 'text') {
if ($array['Content'] == 'TESTCOMPONENT_MSG_TYPE_TEXT') { //微信自動檢測程序 - 返回普通文本消息檢測
$content = 'TESTCOMPONENT_MSG_TYPE_TEXT_callback';
$result = $this->transmitText($array, $content);
echo $result;
return;
} elseif (strpos($array['Content'], 'QUERY_AUTH_CODE') !== false) { //微信自動檢測程序 - 返回Api文本消息檢測
$query_auth_code = trim(str_replace("QUERY_AUTH_CODE:", "", $Content));
$authInfo = $this->grant->apiQueryAuth($component_access_token, $query_auth_code);
if (!array_key_exists('authorization_info', $authInfo)) {
file_put_contents($path . '/' . date('d') . ".txt", "公衆號信息獲取失敗:" . json_encode($authInfo), FILE_APPEND);
return;
}
$authInfo = $authInfo['authorization_info'];
if (!array_key_exists('authorizer_access_token', $authInfo)) {
file_put_contents($path . '/' . date('d') . ".txt", "接口調用令牌未返回:" . json_encode($authInfo), FILE_APPEND);
return;
}
$authorizer_access_token = $authInfo['authorizer_access_token'];
$data = [
'touser' => $openid,
'msgtype' => 'text',
'text' => ['content' => $query_auth_code . '_from_api'],
];
$result = json_decode($this->grant->send_message_1($authorizer_access_token, $data), true);
if ($result['errcode'] == 0) {
file_put_contents($path . '/' . date('d') . ".txt", "返回Api文本消息成功", FILE_APPEND);
return;
} else {
file_put_contents($path . '/' . date('d') . ".txt", "返回Api文本消息失敗:" . json_encode($result), FILE_APPEND);
return;
}
} else {
return;
}
}else{
return;
}
}
}