private $_msg_template = array(
'text' => '<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%s]]></Content></xml>',//文本回复XML模板
'image' => '<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[image]]></MsgType><Image><MediaId><![CDATA[%s]]></MediaId></Image></xml>',//图片回复XML模板
'music' => '<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[music]]></MsgType><Music><Title><![CDATA[%s]]></Title><Description><![CDATA[%s]]></Description><MusicUrl><![CDATA[%s]]></MusicUrl><HQMusicUrl><![CDATA[%s]]></HQMusicUrl><ThumbMediaId><![CDATA[%s]]></ThumbMediaId></Music></xml>',//音乐模板
'news' => '<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>%s</ArticleCount><Articles>%s</Articles></xml>',// 新闻主体
'news_item' => '<item><Title><![CDATA[%s]]></Title><Description><![CDATA[%s]]></Description><PicUrl><![CDATA[%s]]></PicUrl><Url><![CDATA[%s]]></Url></item>',//某个新闻模板
);
public function index(Request $request)
{
// $xml_str = $_GLOBALS['HTTP_RAW_POST_DATA'];
$this->respomseMsg();//接收推送事件
$this->CuStOmIzEmEnUs();//自定义菜单
$this->valid();//验证
}
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
//检查签名
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if($tmpStr == $signature){
return true;
}else{
return false;
}
}
public function respomseMsg(){
$xml_str = file_get_contents('php://input');
if(empty($xml_str)){
die('');
}
if(!empty($xml_str)){
// 解析该xml字符串,利用simpleXML
libxml_disable_entity_loader(true);
//禁止xml实体解析,防止xml注入
$request_xml = simplexml_load_string($xml_str, 'SimpleXMLElement', LIBXML_NOCDATA);
//判断该消息的类型,通过元素MsgType.//
switch ($request_xml->MsgType){
case 'event':
//判断具体的时间类型(关注、取消、点击)
$event = $request_xml->Event;
if ($event=='subscribe') { // 关注事件
$this->_doSubscribe($request_xml);
}elseif ($event=='CLICK') {//菜单点击事件
$this->_doClick($request_xml);
}elseif ($event=='VIEW') {//连接跳转事件
$this->_doView($request_xml);
}else{
}
break;
case 'text'://文本消息
$this->_doText($request_xml);
break;
case 'image'://图片消息
$this->_doImage($request_xml);
break;
case 'voice'://语音消息
$this->_doVoice($request_xml);
break;
case 'video'://视频消息
$this->_doVideo($request_xml);
break;
case 'shortvideo'://短视频消息
$this->_doShortvideo($request_xml);
break;
case 'location'://位置消息
$this->_doLocation($request_xml);
break;
case 'link'://链接消息
$this->_doLink($request_xml);
break;
}
}
}
private function _doSubscribe($request_xml){
$item_list = [
[
"title" => "title","desc" => "desc","picurl" =>"picurl","url" => "url"
],[
"title" => "title","desc" => "desc","picurl" =>"picurl","url" => "url"
],[
"title" => "title","desc" => "desc","picurl" =>"picurl","url" => "url"
]
];
$this->_msgNews($request_xml->FromUserName,$request_xml->ToUserName,$item_list);
}
private function _msgNews($to,$from,$item_list=array()){
//拼凑文章部分
$item_str = '';
foreach ($item_list as $item) {
$item_str .= sprintf($this->_msg_template['news_item'],$item['title'],$item['desc'],$item['picurl'],$item['url']);
}
//拼凑主体部分
$response = sprintf($this->_msg_template['news'], $to, $from, time(), count($item_list), $item_str);
die($response);
}
//自定义菜单
public function CuStOmIzEmEnUs(){
if(!Session::has('key')){
$this->get_access_token();
}
$url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.Session::get('key');
$data = '{
"button":[
{
"type":"view",
"name":"",
"url":""
},
{
"type":"view",
"name":"",
"url":""
},
{
"name":"谁看过我",
"sub_button":[
{
"type":"view",
"name":"",
"url":"url"
},
{
"type":"click",
"name":"",
"key":""
}]
}]
}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$curl = curl_exec($ch);
curl_close($ch);
die(json_decode($curl));
}
微信公众平台自定义菜单加关注推送
最新推荐文章于 2020-10-14 14:57:08 发布