微信第三方平台开发 消息与事件接收URL PHP源码 ThinkPHP
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了微信第三方平台开发 消息与事件接收URL PHP源码 ThinkPHP,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6055字,纯文字阅读大概需要9分钟。
内容图文
![微信第三方平台开发 消息与事件接收URL PHP源码 ThinkPHP](/upload/InfoBanner/zyjiaocheng/600/16e3b8ae394b4756a026959e82262607.jpg)
申请第三方平台(https://open.weixin.qq.com/)过程中,需要在开发资料中填写 消息与事件接收URL 。
该URL的作用:当关注者与已授权公众号/小程序进行交互时,第三方平台将接收到相应的消息推送、事件推送。https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/Message_Encryption/Technical_Plan.html
消息与事件接收URL需要实现的逻辑功能
这个方法处理3中不同的事件推送:
- MsgType为text类型,并且Content信息为‘QUERY_AUTH_CODE:xxxxxxxxx’:需要通过接口’https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=xxx ’POST回复过去。
- MsgType为text类型,并且Content信息为‘TESTCOMPONENT_MSG_TYPE_TEXT’:需要把Content信息改为‘TESTCOMPONENT_MSG_TYPE_TEXT_callback’然后加密return。
- MsgType为event类型:需要注意的是当MsgType为event类型时是没有Content信息的会增加一个Event的字段,这里只要获取到Event然后在后面加上‘from_callback’保存为Content字段信息,统一加密return。
PHP源码
/**
* 消息与事件接收URL
*/
public function wechat_msg_recieve(){
var_dump('消息与事件接收URL');
$access_token = C('WechatThirdParty.access_token');
$encodingAesKey = C('WechatThirdParty.EncodingAESKey');
$third_appid = C('WechatThirdParty.appid');
//解密信息
$pc = new \Org\Com\WXBizMsgCrypt\WXBizMsgCrypt($access_token,$encodingAesKey,$third_appid);
$pc->WXBizMsgCrypt($access_token,$encodingAesKey,$third_appid);
$nonce = I('get.nonce');
$timeStamp = I('get.timestamp');
$msg_sign = I('get.msg_signature');
$from_xml = $GLOBALS["HTTP_RAW_POST_DATA"];
//var_dump($msg_sign);
$msg = '';
$errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
//var_dump($errCode);
$xml_tree = new DOMDocument();
$xml_tree->loadXML($msg);
$array_e = $xml_tree->getElementsByTagName('ToUserName');
$ToUserName = $array_e->item(0)->nodeValue;
//$array_e = $xml_tree->getElementsByTagName('SuccTime');
$array_e = $xml_tree->getElementsByTagName('MsgType');
// Author: yi start
$MsgType = $array_e->item(0)->nodeValue;
sae_debug('[消息与事件接收URL]:'.$msg);
if($MsgType == "text"){
$FromUserName = $xml_tree->getElementsByTagName('FromUserName')->item(0)->nodeValue;
$content = $xml_tree->getElementsByTagName('Content')->item(0)->nodeValue;
$needle ='QUERY_AUTH_CODE:';
$tmparray = explode($needle,$content);
if(count($tmparray)>1){
//模拟粉丝发送文本消息给专用测试公众号,第三方平台方需在5秒内返回空串表明暂时不回复,然后再立即使用客服消息接口发送消息回复粉丝
echo '';
$query_auth_code = trim(str_replace("QUERY_AUTH_CODE:", "", $content));
sae_debug('[返回API文本消息]content:'.json_encode($content));
sae_debug('[返回API文本消息]query_auth_code:'.json_encode($query_auth_code));
$WechatThirdParty = A('API/WechatThirdParty');
$component_access_token = $WechatThirdParty->get_WechatThirdParty_access_token();
//获取authorizer_access_token
$url = 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token='.$component_access_token;
$post_data = array(
'component_appid' => $third_appid,
'authorization_code' => $query_auth_code,
);
$post_data = json_encode($post_data);
$res = $WechatThirdParty->http_request($url, $post_data);
$res = json_decode($res,true);
$authorizer_access_token = $res['authorization_info']['authorizer_access_token'];
$content_re=$query_auth_code."_from_api";
sae_debug('[返回API文本消息]res:'.json_encode($res));
sae_debug('[返回API文本消息]component_access_token:'.json_encode($component_access_token));
sae_debug('[返回API文本消息]authorizer_access_token:'.json_encode($authorizer_access_token));
//发送客服请求
$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" => "".$content_re."",
),
);
$post_data2 = json_encode($post_data2);
$res2 = $WechatThirdParty->http_request($url2, $post_data2);
$res2 = json_decode($res2,true);
sae_debug('[返回API文本消息]发送:'.json_encode($post_data2));
sae_debug('[返回API文本消息]接收:'.json_encode($res2));
$contentx = $query_auth_code;
}else{
//模拟粉丝发送文本消息给专用测试公众号
$contentx = "TESTCOMPONENT_MSG_TYPE_TEXT_callback";
}
}elseif ($MsgType == "event") {
//模拟粉丝触发专用测试公众号的事件
$event = $xml_tree->getElementsByTagName('Event')->item(0)->nodeValue;
$contentx = $event.'from_callback';
}
$text = "<xml>
<ToUserName><![CDATA[$FromUserName]]></ToUserName>
<FromUserName><![CDATA[$ToUserName]]></FromUserName>
<CreateTime>$nowTime</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[$contentx]]></Content>
</xml>";
//加密消息
$encryptMsg = '';
$errCode = $pc->encryptMsg($text, $timeStamp, $nonce, $encryptMsg);
echo $encryptMsg;
// Author: yi end
sae_debug('[返回API文本消息]接收:'.json_encode($encryptMsg));
$data = array();
if($array_e->length != 0)
{
//成功
$data['is_success_verified_by_wechat'] = true;
$succTime = $array_e->item(0)->nodeValue;
$data['verified_timestamp_by_wechat'] = $succTime;
}else
{
//失败
$data['is_success_verified_by_wechat'] = false;
$array_e = $xml_tree->getElementsByTagName('FailTime');
$fallTime = $array_e->item(0)->nodeValue;
$array_e = $xml_tree->getElementsByTagName('failed_reason_by_wechat');
$failed_reason_by_wechat = $array_e->item(0)->nodeValue;
$data['verified_timestamp_by_wechat'] = $fallTime;
$data['failed_reason_by_wechat'] = $failed_reason_by_wechat;
}
$appid = I('get.appid');
//立即发布小程序
$WechatThirdParty = A('API/WechatThirdParty');
$Company = M('Company');
$where = array();
$where['appid'] = $appid;
$Company = $Company->where($where)->find();
$WechatThirdParty->release_minapp($Company['id']);
//var_dump($Company);
$Company = M('Company');
$where = array();
$where['minapp_user_name'] = $ToUserName;
$Company->where($where)->save($data);
}
注意事项
1.代码使用的是ThinkPHP架构,部分语句需要进行适配;
2.注意函数名称和函数所属类,决定了url的具体地址;
3. sae_debug 用于在新浪云sae中打印调试日志,可以删去;
4.相关参数保存在了Mysql数据库表中;
5.实例化的 WXBizMsgCrypt 使用了微信官方提供的库,下载地址为:https://download.csdn.net/download/Ximerr/15651996(如果积分不够可以一键三连后站内信)
实现效果
使用上述代码实现 消息与事件接收URL 并按照 https://blog.csdn.net/Ximerr/article/details/114528516 实现 消息与事件接收URL 可以通过微信第三方平台的全网发布检测。
内容总结
以上是互联网集市为您收集整理的微信第三方平台开发 消息与事件接收URL PHP源码 ThinkPHP全部内容,希望文章能够帮你解决微信第三方平台开发 消息与事件接收URL PHP源码 ThinkPHP所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。