微信公众号后台PHP源代码

神经质
742次浏览
2020年03月21日 18:21
最佳经验
本文由作者推荐
微信网站开发源代码

responseMsg(); class wechatCallbackapiTest { private $$app_id; private $$app_secret; public function valid() { $$echoStr = $$_GET["echostr"]; //valid signature , option if($$this->checkSignature()){ ob_clean(); echo $$echoStr; exit; } } function txt($$fu,$$tu,$$content,$$flag = 0){ $$tpl = " ".$$_SERVER['REQUEST_TIME']." ".$$flag." "; echo $$tpl; } function news($$fu,$$tu,$$data,$$flag = 0){ $$num = count($$data); if($$num > 1){ $$add = $$this->news_add($$data); $$tpl = "

".$$_SERVER['REQUEST_TIME']." ".$$num." ".$$add." ".$$flag." "; echo $$tpl; }else{ $$tpl = " ".$$_SERVER['REQUEST_TIME']." 1 <![CDATA[".$$data[0]['title']."]]> ".$$flag." "; echo $$tpl; } } function news_add($$data){ $$add = ""; foreach ($$data as $$k){ $$add .= " <![CDATA[".$$k['title']."]]> "; }

return $$add; } /* function audio($$fu,$$tu,$$data,$$flag = 0){ $$tpl = " ".$$_SERVER['REQUEST_TIME']." <![CDATA[".$$data['title']."]]> ".$$flag." "; echo $$tpl; } */ function audio($$fu,$$tu,$$data,$$flag = 0){ $$num = count($$data); if($$num > 1){ $$add = $$this->audio_add($$data); $$tpl = " ".$$_SERVER['REQUEST_TIME']." ".$$num." ".$$add." ".$$flag." "; echo $$tpl; }else{ $$tpl = " ".$$_SERVER['REQUEST_TIME']."

1 <![CDATA[".$$data['title']."]]> ".$$flag." "; echo $$tpl; } } function audio_add($$data){ $$add = ""; foreach ($$data as $$k){ $$add .= " <![CDATA[".$$data['title']."]]> "; } return $$add; } public function receiveEvent($$object) { $$contentStr = "ok!"; $$fu = (string)$$object->FromUserName; //取出用户的账号 $$tu = (string)$$object->ToUserName; //公众账号 txt($$fu,$$tu,'begin receiveEvent',0); /* switch ($$object->Event) { case "subscribe": $$contentStr = "欢迎关注清华纸飞机传媒"; break; case "unsubscribe": break; case "CLICK":// $$postObj->EventKey; txt($$fu,$$tu,'回复数字0~50课程号,收听机灵狗英文启蒙课程'); switch ($$object->EventKey)

{ case "leve0": txt($$fu,$$tu,'回复数字0~50课程号,收听机灵狗英文启蒙课程'); //$$contentStr[] = array("Title" =>"机灵狗启蒙英文读物", //"Description" =>"输入数字1~50,听取课程", //"PicUrl" =>"/weixin/weather/icon/", //"Url" =>"weixin://addfriend/pondbaystudio"); break; case "leve1": $$contentStr[] = array("Title" =>"机灵狗启蒙英文读物", "Description" =>"输入数字1~50,听取课程", "PicUrl" =>"/weixin/weather/icon/", "Url" =>""); break; default: $$contentStr[] = array("Title" =>"默认菜单回复", "Description" =>"您正在使用的是自定义菜单测试接口", "PicUrl" =>"/weixin/weather/icon/", "Url" =>""); break; } break; default: txt($$fu,$$tu,'receiveEvent default'); break; } if (is_array($$contentStr)){ $$resultStr = news($$fu,$$tu, $$contentStr); }else{ $$resultStr = txt($$fu,$$tu, $$contentStr,0); //txt($$fu,$$tu,'你发送了数字 0 ,所以你就看到了这么多 000000000000。'); //txt($$fu,$$tu,'你发送的内容是。'.$$content); } */ return $$resultStr; } private function transmitText($$object, $$content, $$funcFlag = 0) { $$textTpl = " %s

%d "; $$resultStr = sprintf($$textTpl, $$object->FromUserName, $$object->ToUserName, time(), $$content, $$funcFlag); return $$resultStr; } private function transmitNews($$object, $$arr_item, $$funcFlag = 0) { //首条标题28字,其他标题39字 if(!is_array($$arr_item)) return; $$itemTpl = " <![CDATA[%s]]> "; $$item_str = ""; foreach ($$arr_item as $$item) $$item_str .= sprintf($$itemTpl, $$item['Title'], $$item['Description'], $$item['PicUrl'], $$item['Url']); $$newsTpl = " %s %s $$item_str %s "; $$resultStr = sprintf($$newsTpl, $$object->FromUserName, $$object->ToUserName, time(), count($$arr_item), $$funcFlag); return $$resultStr; }

public function responseMsg() { //获取post数据 //$$postStr = $$GLOBALS["HTTP_RAW_POST_DATA"]; $$postStr = isset($$GLOBALS["HTTP_RAW_POST_DATA"]) ? $$GLOBALS["HTTP_RAW_POST_DATA"] : "" //运用三目运算符,使用isset函数检测变量是否已经设置或者存在(使用数组时,其索引有可能不存在,比如此例的HTTP_RAW_POST_DATA可能不存在) //解析post数据 if (!empty($$postStr)){ //$$access_token = $$this->get_access_token();//获取access_token //$$this->createmenu($$access_token);//创建菜单 $$postObj = simplexml_load_string($$postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $$type= $$postObj->MsgType; $$fu = (string)$$postObj->FromUserName; //取出用户的账号 $$tu = (string)$$postObj->ToUserName; //公众账号 switch($$type) { case "voice"; $$resultStr= $$this->getMusicContent($$postObj); echo $$resultStr; exit; break; case "event": //$$this->txt($$fu,$$tu,'回复数字0~50课程号,收听机灵狗英文启蒙课程'); //$$resultStr = $$this->receiveEvent($$postObj); switch ($$postObj->Event) { case "subscribe": $$contentStr = "欢迎关注跨界交流"; break; case "unsubscribe": break; case "CLICK":// $$postObj->EventKey; //$$this->txt($$fu,$$tu,'回复数字0~50课程号,收听机灵狗英文启蒙课程'); switch ($$postObj->EventKey) { case "introduce": //$$this->txt($$fu,$$tu,'回复数字0~50课程号,收听机灵狗英文启蒙课程

'); //$$this->txt($$fu,$$tu,'leve0'); //$$contentStr[] = array("Title" =>"机灵狗启蒙英文读物", //"Description" =>"输入数字1~50,听取课程", //"PicUrl" =>"/weixin/weather/icon/", //"Url" =>"weixin://addfriend/pondbaystudio"); $$msg[0]['title'] = '这里是标题'; $$msg[0]['intro'] = '简介'; $$msg[0]['pic'] = '/weixin/weather/icon/'; //图片URL 这里是返回发送的原图 URL 要使用绝对完整地址 $$msg[0]['url'] = '/s?__biz=MzAwNTI5NTQwMw==&mid=203760231&idx=1&sn=2dfbe17ad8aa46fc10fd32d088aeb428&scene=4#wechat_redirect'; //这里是超链接 $$this->news($$fu,$$tu,$$msg); break; case "game": $$this->txt($$fu,$$tu,'语音输入进行点歌游戏 \n\r 输入课程数字号听英文录音'); break; default: //$$this->txt($$fu,$$tu,'default-0'); $$contentStr[] = array("Title" =>"默认菜单回复", "Description" =>"您正在使用的是自定义菜单测试接口", "PicUrl" =>"/weixin/weather/icon/", "Url" =>"weixin://addfriend/pondbaystudio"); break; } break; default: //$$this->txt($$fu,$$tu,'receiveEvent default-1'); break; } if (is_array($$contentStr)){ $$resultStr = $$this->transmitNews($$postObj, $$contentStr); }else{ //$$resultStr = $$this->transmitText($$postObj, $$contentStr); $$this->txt($$fu,$$tu,'回复数字0~50课程号,收听机灵狗英文启蒙课程'); } break;

case 'image': //图像消息类型 $$pic = (string)$$postObj->PicUrl; //取出图片url //回复一个图文开始定义数组 $$msg[0]['title'] = '这里是标题'; $$msg[0]['intro'] = '简介'; $$msg[0]['pic'] = $$pic; //图片URL 这里是返回发送的原图 URL 要使用绝对完整地址 $$msg[0]['url'] = ''; //这里是超链接 $$this->news($$fu,$$tu,$$msg); //如要回复多条图文,可对该二维数组赋值多个元素如 $$msg[0] $$msg[1] $$msg[2] 。 break; case 'location': //地理位置消息类型 $$l_x = $$postObj->Location_X; //取出 x 坐标 $$l_y = $$postObj->Location_Y; //取出 y 坐标 $$scale = $$postObj->Scale; //取出缩放等级 $$lable = $$postObj->Label; //取出位置信息 //回复文字消息 $$this->txt($$fu,$$tu,'你所处位置是:'.$$lable.'坐标为 X:'.$$l_x.'Y:'.$$l_y); break; case "text"; //$$resultStr= $$this->getMenu($$postObj); //echo $$resultStr; //exit; //break; $$content = trim($$postObj->Content); // 取出消息内容 //根据发送的内容回复相应的内容 switch($$content){ case '0': //如果发送的是数字0则回复以下内容 $$this->txt($$fu,$$tu,'你发送了数字 0 ,所以你就看到了这么多 000000000000。'); break; case '1': //如果发送的是数字1则回复以下内容 $$this->txt($$fu,$$tu,'你发送了数字 1 ,所以你就看到了这么多 111111111111。'); break; case '2': //如果发送的是数字2 则回复以下内容 $$msg['title'] = '机灵狗英文启蒙教育'; $$msg['intro'] = '清华出版社亲情推荐'; $$msg['url'] = '/3'; //音频文件的绝对完整 url $$msg['hqurl'] = '/3'; //音频文件的高清绝对完整 url wifi下优先播放此url. $$this->audio($$fu,$$tu,$$msg); //回复一个可播放的音频消息 break; default: //如果是其他内容则回复 //$$this->txt($$fu,$$tu,'你发送的内容是。'.$$content);

$$resultStr= $$this->inputgetMusicContent($$postObj); echo $$resultStr; break; } } } } private function checkSignature() { $$signature = $$_GET["signature"]; $$timestamp = $$_GET["timestamp"]; $$nonce = $$_GET["nonce"]; $$token = TOKEN; $$tmpArr = array($$token, $$timestamp, $$nonce); sort($$tmpArr); $$tmpStr = implode( $$tmpArr ); $$tmpStr = sha1( $$tmpStr ); if( $$tmpStr == $$signature ){ return true; }else{ return false; } } public function getMusicContent($$postObj) { $$ret= " %s <![CDATA[%s]]>

"; $$recognition=$$postObj->Recognition; $$keywordc= urlencode($$recognition); $$musicapi "/x?op=12&count=1&title={$$keywordc}\$$\$$"; $$simstr=file_get_contents($$musicapi); $$musicobj=simplexml_load_string($$simstr); $$i=0; $$musicurl; foreach($$musicobj->url as $$itemobj) { $$encode = $$itemobj->encode; = //处理decode ,发现微信在处理音乐的时候有个问题,所以这里删除一个参数 $$decode = $$itemobj->decode; $$removedecode = end(explode('&', $$decode)); if($$removedecode<>"") { $$removedecode="&".$$removedecode; } $$decode = str_replace($$removedecode,"", $$decode); $$musicurl= str_replace(end(explode('/', $$encode)) ,$$decode,$$encode); break; } $$resultStr = sprintf($$ret, $$postObj->FromUserName, $$postObj->ToUserName,time(), $$recognition,$$musicurl,$$musicurl); return $$resultStr; } public function inputgetMusicContent($$postObj) { $$ret= " %s <![CDATA[%s]]>

"; $$recognition=$$postObj->Content; $$keywordc= $$postObj->Content; $$musicapi "/x?op=12&count=1&title={$$keywordc}\$$\$$"; $$simstr=file_get_contents($$musicapi); $$musicobj=simplexml_load_string($$simstr); $$i=0; $$musicurl; foreach($$musicobj->url as $$itemobj) { $$encode = $$itemobj->encode; = //处理decode ,发现微信在处理音乐的时候有个问题,所以这里删除一个参数 $$decode = $$itemobj->decode; $$removedecode = end(explode('&', $$decode)); if($$removedecode<>"") { $$removedecode="&".$$removedecode; } $$decode = str_replace($$removedecode,"", $$decode); $$musicurl= str_replace(end(explode('/', $$encode)) ,$$decode,$$encode); break; } $$resultStr = sprintf($$ret, $$postObj->FromUserName, $$postObj->ToUserName,time(), $$recognition,$$musicurl,$$musicurl); return $$resultStr; } /* public function getMenu($$postObj) { $$fromUsername = $$postObj->FromUserName; $$toUsername = $$postObj->ToUserName; $$keyword = trim($$postObj->Content); $$time = time(); $$textTpl = " %s

0 "; if(!empty( $$keyword )) { $$contentStr = "语音输入进行点歌游戏 \n\r 输入课程数字号听英文录音"; $$msgType = "text"; //$$size = readfile('./3'); //$$contentStr = $$size; $$resultStr = sprintf($$textTpl, $$fromUsername, $$toUsername, $$time, $$msgType, $$contentStr); echo $$resultStr; }else{ echo "请输入歌曲名称,或语音输入歌曲名称\n"; } } */ /** * 获取access_token */ private function get_access_token() { $$url = "/cgi-bin/token?grant_type=client_credential&appid=APP_ID&secret=APP_SECRET"; $$data = json_decode(file_get_contents($$url),true); if($$data['access_token']){ return $$data['access_token']; }else{ return "获取access_token错误"; } } /** * 创建菜单 * @param $$access_token 已获取的ACCESS_TOKEN */ public function createmenu($$access_token) { $$url = "/cgi-bin/menu/create?access_token=".$$access_token;

$$arr = array( 'button' =>array( array( ), array( ), array( 'name'=>urlencode("生活查询"), 'sub_button'=>array( array( 'name'=>urlencode("天气查询"), 'type'=>'click', 'key'=>'VCX_WEATHER' ), array( 'name'=>urlencode("身份证查询"), 'type'=>'click', 'key'=>'VCX_IDENT' ) ) 'name'=>urlencode("轻松娱乐"), 'sub_button'=>array( array( 'name'=>urlencode("刮刮乐"), 'type'=>'click', 'key'=>'VCX_GUAHAPPY' ), array( 'name'=>urlencode("幸运大转盘"), 'type'=>'click', 'key'=>'VCX_LUCKPAN' ) ) 'name'=>urlencode("我的信息"), 'sub_button'=>array( array( 'name'=>urlencode("关于我"), 'type'=>'click', 'key'=>'VCX_ABOUTME' ), array( 'name'=>urlencode("工作信息"), 'type'=>'click', 'key'=>'VCX_JOBINFORMATION'

) ) ) ) ); $$jsondata = urldecode(json_encode($$arr)); $$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,$$jsondata); curl_exec($$ch); curl_close($$ch); } } ?>

微信网站开发源代码