申请第三方平台,这个很简单直接按照提示填写,下面的授权事件接收URL中获取的component_verify_ticket是会过期的,所以不要只获取一次,缓存起来每10分钟会发送一次,每1小时会更新一次。
附上我的获取component_verify_ticket的代码:我是缓存和数据库都保存了
微信有提供wxBizMsgCrypt.php加解密核心代码
/**
* 授权事件接收URL
* @access public
*
*/
public function sysMessage()
{
$wxComponentTicketModel = new \app\diuber\model\WxComponentTicket();
$encodingAesKey = $this->encodingAesKey;
$token = $this->token;
$appId = $this->appId;
$timeStamp = empty($_GET['timestamp']) ? "" : trim($_GET['timestamp']) ;
$nonce = empty($_GET['nonce']) ? "" : trim($_GET['nonce']) ;
$msg_sign = empty($_GET['msg_signature']) ? "" : trim($_GET['msg_signature']) ;
$encryptMsg = file_get_contents('php://input', 'r');
libxml_disable_entity_loader(true);
$result = json_decode(json_encode(simplexml_load_string($encryptMsg, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
if(config('redis_set')['use_status']){
$redis = $wxComponentTicketModel->initializeRedis();
$redis->set('wx_component_verify_ticket_xml',$encryptMsg);
}
$pc = new \WXBizMsgCrypt($token, $encodingAesKey, $appId);
$xml_tree = new \DOMDocument();
$xml_tree->loadXML($encryptMsg);
$array_e = $xml_tree->getElementsByTagName('Encrypt');
$encrypt = $array_e->item(0)->nodeValue;
$format = "";
$from_xml = sprintf($format, $encrypt);
// 第三方收到公众号平台发送的消息
$msg = '';
$errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
if ($errCode == 0) {
//print("解密后: " . $msg . "\n");
$xml = new \DOMDocument();
$xml->loadXML($msg);
$array_e = $xml->getElementsByTagName('ComponentVerifyTicket');
$component_verify_ticket = $array_e->item(0)->nodeValue;
//logResult('解密后的component_verify_ticket是:'.$component_verify_ticket);
$dateline = time();
$data = array(
'app_id' => $result['AppId'],
'encrypt' => $result['Encrypt'],
'create_time' => $dateline + 600,
//'info_type' => $result['AppId'],
'component_verify_ticket' => $component_verify_ticket,
'time' => date('Y-m-d H:i:s')
);
if(config('redis_set')['use_status']){
$redis = $wxComponentTicketModel->initializeRedis();
$redis->set('wx_component_verify_ticket',json_encode($data));
}
$existComponentTicke = $wxComponentTicketModel->get(array('component_verify_ticket'=>$component_verify_ticket));
if(!$existComponentTicke){
$wx = $wxComponentTicketModel->create($data);
if($wx){
echo 'success';
exit;
}else{
echo 'fail';
exit;
}
}else{
echo 'success';
exit;
}
}else{
echo 'fail';
exit;
}
}
地址:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318479&token=bc7c35421376903886e43747e4bf165c9be36663&lang=zh_CN
公众号消息与事件接收URL,直接上代码:
这个方法处理3中不同的事件推送,
1.MsgType为text类型,并且Content信息为‘QUERY_AUTH_CODE:xxxxxxxxx’:需要通过接口’https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=xxx ’POST回复过去。
2.MsgType为text类型,并且Content信息为‘TESTCOMPONENT_MSG_TYPE_TEXT’:需要把Content信息改为‘TESTCOMPONENT_MSG_TYPE_TEXT_callback’然后加密return
3.MsgType为event类型:需要注意的是当MsgType为event类型时是没有Content信息的会增加一个Event的字段,这里只要获取到Event然后在后面加上‘from_callback’保存为Content字段信息,统一加密return。
具体逻辑看代码
/**
* 公众号消息与事件接收URL
* @access public
*
*/
public function callback()
{
$wxComponentTicketModel = new \app\diuber\model\WxComponentTicket();
$wxCallbackModel = new \app\diuber\model\WxCallback();
$wxAccessTokenModel = new \app\diuber\model\WxAccessToken();
$encodingAesKey = $this->encodingAesKey;
$token = $this->token;
$appId = $this->appId;
$timeStamp = empty($_GET['timestamp']) ? "" : trim($_GET['timestamp']) ;
$nonce = empty($_GET['nonce']) ? "" : trim($_GET['nonce']) ;
$msg_sign = empty($_GET['msg_signature']) ? "" : trim($_GET['msg_signature']) ;
$encryptMsg = file_get_contents('php://input');
$pc = new \WXBizMsgCrypt($token, $encodingAesKey, $appId);
$xml_tree = new \DOMDocument();
$xml_tree->loadXML($encryptMsg);
$array_e = $xml_tree->getElementsByTagName('Encrypt');
$encrypt = $array_e->item(0)->nodeValue;
$format = "";
$from_xml = sprintf($format, $encrypt);
// 第三方收到公众号平台发送的消息
$msg = '';
$errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
if ($errCode == 0) {
$xml = new \DOMDocument();
$xml->loadXML($msg);
$array_e2 = $xml->getElementsByTagName('ToUserName');
$ToUserName = $array_e2->item(0)->nodeValue;
$array_e3 = $xml->getElementsByTagName('FromUserName');
$FromUserName = $array_e3->item(0)->nodeValue;
$array_e5 = $xml->getElementsByTagName('MsgType');
$MsgType = $array_e5->item(0)->nodeValue;
$nowTime = date('Y-m-d H:i:s');
$contentx = '';
if($MsgType=="text") {
$array_e = $xml->getElementsByTagName('Content');
$content = $array_e->item(0)->nodeValue;
$needle ='QUERY_AUTH_CODE:';
$tmparray = explode($needle,$content);
if(count($tmparray) > 1){
//3、模拟粉丝发送文本消息给专用测试公众号,第三方平台方需在5秒内返回空串
//表明暂时不回复,然后再立即使用客服消息接口发送消息回复粉丝
$contentx = str_replace ($needle,'',$content);
$info = $wxAccessTokenModel->getMiniAppInfo($contentx);
$test_token = $info['info']['authorizer_access_token'];
$content_re = $contentx."_from_api";
echo '';
$data = '{
"touser":"'.$FromUserName.'",
"msgtype":"text",
"text":
{
"content":"'.$content_re.'"
}
}';
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$test_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
curl_close($ch);
}else{
//2、模拟粉丝发送文本消息给专用测试公众号
$contentx = "TESTCOMPONENT_MSG_TYPE_TEXT_callback";
}
}elseif($MsgType == "event"){ //1、模拟粉丝触发专用测试公众号的事件
$array_e4 = $xml->getElementsByTagName('Event');
$event = $array_e4->item(0)->nodeValue;
$contentx = $event.'from_callback';
}
$text = "
$nowTime
";
//加密消息
$encryptMsg = '';
$errCode = $pc->encryptMsg($text, $timeStamp, $nonce, $encryptMsg);
$wxCallbackModel->create(array('from_user_name'=>$FromUserName,'to_user_name'=>$ToUserName,'msg_type'=>$MsgType,'content'=>$contentx,'create_time'=>$timeStamp));
echo $encryptMsg;
exit();
} else {
if(config('redis_set')['use_status']){
$redis = $wxComponentTicketModel->initializeRedis();
$redis->set('wx_call_back_err',$errCode);
}
exit();
}
}
到这里已经全部完成了全网发布的准备了。可以直接点击全网发布监测,通过以后一般会在2-3之内通过审核。