微信回调处理关键这3步通过基本没问题
function checkMsgDecode($timeStamp,$nonce,$encrypt_type,$msg_sign,$encryptMsg){
$pc = new WXBizMsgCrypt($this->token, $this->encodingAesKey, $this->appId);
$xml_tree = new DOMDocument();
$xml_tree->loadXML($encryptMsg);
$array_e = $xml_tree->getElementsByTagName('Encrypt');
$encrypt = $array_e->item(0)->nodeValue;
$format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
$from_xml = sprintf($format, $encrypt);
$msg = '';
$errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
$xml = new DOMDocument();
$xml->loadXML($msg);
$Content = $xml->getElementsByTagName('Content')->item(0)->nodeValue;
$FromUserName = $xml->getElementsByTagName('FromUserName')->item(0)->nodeValue;
file_put_contents('msg.txt', '8'.$Content,FILE_APPEND);
if( strstr($Content,'QUERY_AUTH_CODE') ){
file_put_contents('QUERY_AUTH_CODE.txt', $Content);
$auth_code = str_replace('QUERY_AUTH_CODE:','',$Content);
$access_token = self::getComponentAccessToken();
$post_url = $this->authorizer_access_token_url.$access_token;
$post_data = array(
'component_appid'=>$this->appId,
'authorization_code'=>$auth_code
);
$post_data = json_encode($post_data);
$return_data = $this->curl($post_url,$post_data,'POST');
$data_dejson = $this->doJson($return_data);
$authorizer_access_token = $data_dejson['authorization_info']['authorizer_access_token'];
$this->cache->Add('check_authorizer_access_token',$authorizer_access_token,7000);
file_put_contents('authorizer_access_token.txt',$authorizer_access_token);
$post_url="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$authorizer_access_token;
$new_Content = $auth_code. '_from_api';
//$res = json_decode(self::https_request($new_url,$new_Content));
$post_data = array(
"touser" => $FromUserName,
"msgtype" => "text",
"text" => array("content"=>$new_Content)
);
$post_data = json_encode($post_data);
$return_data = $this->curl($post_url,$post_data,'POST');
//$data_dejson = $this->doJson($return_data);
file_put_contents('touser.txt',$return_data);
file_put_contents('return_data.txt',$return_data);
file_put_contents('new_url.txt',$post_url);
file_put_contents('new_Content.txt',$new_Content);
return true;
}
if( strstr($Content,'TESTCOMPONENT_MSG_TYPE_TEXT') ){
/*
file_put_contents('msg2.txt', $msg);
$xml_format = $this->xmlFormat($msg,'TESTCOMPONENT_MSG_TYPE_TEXT_callback');
file_put_contents('msg22.txt', $xml_format);
$res = $this->msgEncode($timeStamp,$nonce,$xml_format);
file_put_contents('msg222.txt', $res);
*/
$authorizer_access_token = $this->cache->Get('check_authorizer_access_token');
$post_url2="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$authorizer_access_token;
$post_data2 = array(
"touser" => $FromUserName,
"msgtype" => "text",
"text" => array("content"=>"TESTCOMPONENT_MSG_TYPE_TEXT_callback")
);
$post_data2 = json_encode($post_data2);
file_put_contents('post_data.txt',$post_data2);
$return_data = $this->curl($post_url2,$post_data2,'POST');
file_put_contents('return_data2.txt',$return_data);
return '';
}
$Event = $xml->getElementsByTagName('Event')->item(0)->nodeValue;
if( isset($Event) ){
file_put_contents('ori_msg.txt', $msg);
$authorizer_access_token = $this->cache->Get('check_authorizer_access_token');
$post_url="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$authorizer_access_token;
$post_data = array(
"touser" => $FromUserName,
"msgtype" => "text",
"text" => array("content"=>$Event.'from_callback')
);
$post_data = json_encode($post_data);
$return_data = $this->curl($post_url,$post_data,'POST');
file_put_contents('return_data3.txt',$return_data);
return $return_data;
}
}