<?php /* * 云梯数据 * (c)2014 www.iyunti.com All Rights Reserved */ define("TOKEN", "myweixin"); // 改成自己的,和微信平台一致 $wechatObj = new myweixin(); if (!isset($_GET['echostr'])) { $wechatObj->responseMsg(); }else{ $wechatObj->valid(); } class myweixin { 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 responseMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)){ $this->logger("R ".$postStr); $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); switch ($RX_TYPE) { case "event": $result = $this->receiveEvent($postObj); break; case "text": $result = $this->receiveText($postObj); break; case "image": $result = $this->receiveImage($postObj); break; case "location": $result = $this->receiveLocation($postObj); break; case "voice": $result = $this->receiveVoice($postObj); break; case "video": $result = $this->receiveVideo($postObj); break; case "link": $result = $this->receiveLink($postObj); break; default: $result = "unknow msg type: ".$RX_TYPE; break; } $this->logger("T ".$result); echo $result; }else { echo ""; exit; } } private function receiveEvent($object) { $content = ""; switch ($object->Event) { case "subscribe": $content = "欢迎关注云梯数据 "; $content .= (!empty($object->EventKey))?("\n来自二维码场景 ".str_replace("qrscene_","",$object->EventKey)):""; break; case "unsubscribe": $content = "取消关注"; break; case "SCAN": $content = "扫描场景 ".$object->EventKey; break; case "CLICK": switch ($object->EventKey) { case "COMPANY": $content = "云梯数据提供电子商务技术服务。"; break; default: $content = "点击菜单:".$object->EventKey; break; } break; case "LOCATION": $content = "上传位置:纬度 ".$object->Latitude.";经度 ".$object->Longitude; break; default: $content = "receive a new event: ".$object->Event; break; } $result = $this->transmitText($object, $content); return $result; } private function receiveText($object) { $keyword = trim($object->Content); switch ($keyword) { case "文本": $content = "这是个文本消息"; break; case "图文": case "单图文": $content[] = array("Title"=>"单图文标题", "Description"=>"单图文内容", "PicUrl"=>"http://www.iyunti.com/images/logo.png", "Url" =>"http://m.iyunti.com"); break; case "多图文": $content[] = array("Title"=>"多图文1标题", "Description"=>"", "PicUrl"=>"http://www.iyunti.com/images/logo.png", "Url" =>"http://m.iyunti.com"); $content[] = array("Title"=>"多图文2标题", "Description"=>"", "PicUrl"=>"http://www.iyunti.com/images/logo.png", "Url" =>"http://m.iyunti.com"); $content[] = array("Title"=>"多图文3标题", "Description"=>"", "PicUrl"=>"http://www.iyunti.com/images/logo.png", "Url" =>"http://m.iyunti.com"); break; case "音乐": $content = array("Title"=>"荷塘月色", "Description"=>"歌手:凤凰传奇", "MusicUrl"=>"http://www.iyunti.com/coder/weixin/hetangyuese.mp3", "HQMusicUrl"=>"http://www.iyunti.com/coder/weixin/hetangyuese.mp3"); break; default: $content = "当前时间:".date("Y-m-d H:i:s",time()); break; } if(is_array($content)){ if (isset($content[0]['PicUrl'])){ $result = $this->transmitNews($object, $content); }else if (isset($content['MusicUrl'])){ $result = $this->transmitMusic($object, $content); } }else{ $result = $this->transmitText($object, $content); } return $result; } private function receiveImage($object) { $content = array("MediaId"=>$object->MediaId); $result = $this->transmitImage($object, $content); return $result; } private function receiveLocation($object) { $content = "你发送的是位置,纬度为:".$object->Location_X.";经度为:".$object->Location_Y.";缩放级别为:".$object->Scale.";位置为:".$object->Label; $result = $this->transmitText($object, $content); return $result; } private function receiveVoice($object) { if (empty($object->Recognition)){ $content = array("MediaId"=>$object->MediaId); $result = $this->transmitVoice($object, $content); }else{ $content = "你刚才说的是:".$object->Recognition; $result = $this->transmitText($object, $content); } return $result; } private function receiveVideo($object) { $content = array("MediaId"=>$object->MediaId, "ThumbMediaId"=>$object->ThumbMediaId, "Title"=>"", "Description"=>""); $result = $this->transmitVideo($object, $content); return $result; } private function receiveLink($object) { $content = "你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url; $result = $this->transmitText($object, $content); return $result; } private function transmitText($object, $content) { $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content); return $result; } private function transmitImage($object, $imageArray) { $itemTpl = "<Image> <MediaId><![CDATA[%s]]></MediaId> </Image>"; $item_str = sprintf($itemTpl, $imageArray['MediaId']); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[image]]></MsgType> $item_str </xml>"; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } private function transmitVoice($object, $voiceArray) { $itemTpl = "<Voice> <MediaId><![CDATA[%s]]></MediaId> </Voice>"; $item_str = sprintf($itemTpl, $voiceArray['MediaId']); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[voice]]></MsgType> $item_str </xml>"; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } private function transmitVideo($object, $videoArray) { $itemTpl = "<Video> <MediaId><![CDATA[%s]]></MediaId> <ThumbMediaId><![CDATA[%s]]></ThumbMediaId> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> </Video>"; $item_str = sprintf($itemTpl, $videoArray['MediaId'], $videoArray['ThumbMediaId'], $videoArray['Title'], $videoArray['Description']); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[video]]></MsgType> $item_str </xml>"; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } private function transmitNews($object, $newsArray) { if(!is_array($newsArray)){ return; } $itemTpl = " <item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item> "; $item_str = ""; foreach ($newsArray as $item){ $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']); } $newsTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[news]]></MsgType> <Content><![CDATA[]]></Content> <ArticleCount>%s</ArticleCount> <Articles> $item_str</Articles> </xml>"; $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray)); return $result; } private function transmitMusic($object, $musicArray) { $itemTpl = "<Music> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <MusicUrl><![CDATA[%s]]></MusicUrl> <HQMusicUrl><![CDATA[%s]]></HQMusicUrl> </Music>"; $item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[music]]></MsgType> $item_str </xml>"; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } private function logger($log_content) { if(isset($_SERVER['HTTP_APPNAME'])){ //SAE sae_set_display_errors(false); sae_debug($log_content); sae_set_display_errors(true); }else if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ //LOCAL $max_size = 10000; $log_filename = "log.xml"; if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);} file_put_contents($log_filename, date('H:i:s')." ".$log_content."\r\n", FILE_APPEND); } } } ?>
相关推荐
PHP微信公众平台开发,内容丰富,涵盖了多个PHP微信公众平台开发的方面,适合于初学者来学习借鉴。
NULL 博文链接:https://runthu.iteye.com/blog/2033715
微信公众号平台发开书籍与实例文档 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
根据最新微信5.3版撰写,切合微信公众平台的最新技术发展,全面解读微信公众平台的消息接口及九大高级接口、微信内置浏览器特性及Weixin JS接口系统地讲解了开发微信公众号的流程、方法、技巧与注意事项以实际项目为...
自从方倍工作室在博客园推出微信公众平台开发系列教程后,受到广大微信开发人员及爱好者的热情关注,相头文章的日访问量高达上万人次,而《微信公众平台开发入门教程》一门阅读量早已超出20万,很多博文被很多有影响...
第1章 微信公众平台介绍 1 1.1 微信及其两大平台 1 1.2 微信公众平台 2 1.2.1 功能 2 1.2.2 管理 5 1.2.3 服务 6 1.2.4 统计 7 1.2.5 设置 9 第2章 获取服务器资源 11 2.1 新浪SAE 11 2.1.1 申请账号 11 ...
仿微信公共平台自动回复源码是一个仿微信公众平台的自动回复部分单独做的一个小应用。和微信公众平台所实现的自动回复功能基本一致, 和普通的Android程序一样,分为activity、domain、adapter、和util四个包,通过...
微信公众平台功能实现 周边酒店查询 php开发 xml封装数据
php微信公众营销平台源码,亲测可用
提供了用户接入企业微信的两个接口及如何使用这两个接口的示例。分别用于收到用户回复消息的解密以及开发者回复消息的加密过程。关于加解密协议请参考微信公众平台官方文档。
PHP实例开发源码—CakeWX微信公众平台开发框架 php版.zip PHP实例开发源码—CakeWX微信公众平台开发框架 php版.zip PHP实例开发源码—CakeWX微信公众平台开发框架 php版.zip
案例程序采用广泛流行的PHP、MySQL、XML...系统完整地介绍微信公众平台基础接口、自定义菜单、高级接口、微信支付、分享转发等所有相关技术,以生活类、娱乐类、企业类微信开发为切入点,讲解了30多个功能或应用案例。
微信公众平台PHP源码
10. **调试与发布**:利用微信开发者工具进行预览、调试和性能优化,最终将代码上传至微信开发者平台进行发布。 通过学习和应用这个自定义导航源码,开发者可以更好地理解和实践微信小程序的组件化、样式定制、事件...
本压缩包是传智播客“PHP微信公众平台开发”课程的官方笔记和源码,学习视频下载链接为http://yun.itheima.com/course/163.html
多用户微信开发接口公众平台营销系统源码. 仿乐享微信365微信管家最新版 a、将文件夹内的所有文件上传到网站根目录 b、利用phpmyadmin或其他方法导入文件夹内的wechat.sql文件 c、打开网站的数据库配置文件...
基于PHP的N168多用户微信公众平台php版源码.zip
该书系统讲解了微信公众平台应用开发的流程、方法和技巧,并配有若干完整的案例,并提供源码下载。
微信公众 平台开发 消息回复功 能源代码 单图文回复 多图文回复 文本回复 音乐回复 music 微信公众平台开发 多个功能整合
本书共分10章,程序案例采用广泛流行的PHP、MySQL、XML、CSS、JavaScript、HTML5等程序语言及数据库实现...本书可作为微信公众平台开发实践的基础教材,还可以作为热爱互联网及微信公众平台的相关从业人员的参考资料。