首先声明我的程序是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;
}
}
}