微信公众平台开发接口PHP SDK完整版

官方提供的SDK只有一个文本消息功能,我们将所有消息的消息类型及事件响应都整理了进来,并且加入日志记录,代码如下:

 

更新日志:

2013-01-01 版本1.0
2014-03-15 增加图片、视频、语音的内容回复
2014-04-09 增加菜单链接事件
2014-04-10 修改文本回复的判定方法

<?php
/*方倍工作室CopyRight 2014 All Rights Reserved
*/define("TOKEN", "weixin");$wechatObj = new wechatCallbackapiTest();
if (!isset($_GET['echostr'])) {$wechatObj->responseMsg();
}else{$wechatObj->valid();
}class wechatCallbackapiTest
{//验证消息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;case "VIEW":$content = "跳转链接 ".$object->EventKey;break;default:$content = "receive a new event: ".$object->Event;break;}$result = $this->transmitText($object, $content);return $result;}//接收文本消息private function receiveText($object){switch ($object->Content){case "文本":$content = "这是个文本消息";break;case "图文":case "单图文":$content = array();$content[] = array("Title"=>"单图文标题",  "Description"=>"单图文内容", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");break;case "多图文":$content = array();$content[] = array("Title"=>"多图文1标题", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");$content[] = array("Title"=>"多图文2标题", "Description"=>"", "PicUrl"=>"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");$content[] = array("Title"=>"多图文3标题", "Description"=>"", "PicUrl"=>"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");break;case "音乐":$content = array("Title"=>"最炫民族风", "Description"=>"歌手:凤凰传奇", "MusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3", "HQMusicUrl"=>"http://121.199.4.61/music/zxmzf.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 (isset($object->Recognition) && !empty($object->Recognition)){$content = "你刚才说的是:".$object->Recognition;$result = $this->transmitText($object, $content);}else{$content = array("MediaId"=>$object->MediaId);$result = $this->transmitVoice($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'])){   //SAEsae_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);}}
}?>

 

转载于:https://www.cnblogs.com/joshua317/articles/3713376.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/273759.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

BZOJ3570 : DZY Loves Physics I

考虑两个质量均为m&#xff0c;速度分别v1、v2的小球发生完全弹性碰撞的影响&#xff1a; 由动能守恒得&#xff1a; $\frac{1}{2}mv_1^2\frac{1}{2}mv_2^2\frac{1}{2}mv_1^2\frac{1}{2}mv_2^2$$v_1^2v_2^2v_1^2v_2^2$ 由动量守恒得&#xff1a; $mv_1mv_2mv_1mv_2$$v_1v_2v_1v…

数据库---主键约束

1、设置主键约束(1)、方式一&#xff1a;创建表时&#xff0c;在字段在描述处声明指定字段为主键&#xff1b; CREATE TABLE 表名(字段 类型(长度) PRIMARY KEY,字段 类型(长度) );CREATE TABLE STUDENT(STU_ID INT PAIMARY KEY,STU_NAME VARCHAR(255) );(2)、方式二&#xff1…

关于VS2010帮助文档的使用和VC6.0在Win7 64位下的使用

由于购置了新的电脑&#xff0c;安装的是Win7 64位的操作系统&#xff0c;这两天我在重新安装编程环境的时候遇到一些问题&#xff0c;现在都解决掉了&#xff0c;分享出来以供需要的人参考。 一、以前使用的是VS2008&#xff0c;从VC6到2008这么多年了一只使用的MSDN是带索引的…

数据库---聚合查询

聚合查询&#xff1a;纵向查询&#xff0c;它是对一列的值进行计算&#xff0c;然后返回一个单一的值&#xff1b;另外聚合查询是忽略空值。 count&#xff1a;统计指定列不为NULL的记录行数&#xff1b;sum&#xff1a;计算指定列的数值和&#xff0c;如果指定列类型不是数…

【记忆法】心智绘图

心智绘图方法 1.提出具体、明确的记忆任务(以30min为单位) 记忆25min休息5min2.及时复习&#xff0c;减少遗忘(记忆关键字) 看到关键词能够回想起全部的内容。看到关键词能够产生生动的图像。3.平时多背诵 有时间多记一些小东西、小片段4.复述和再现 听到或看到什么好的故事要及…

数据库---分组查询

一、分组查询&#xff1a;指使用group by字句对查询信息进行分组。格式&#xff1a; SELECT 字段1,字段2... FROM 表名 GROUP BY 分组字段 HAVING 分组条件; 分组操作中的having子语句&#xff0c;是用于在分组后对数据进行过滤的&#xff0c;作用类似于where条件。 1、having与…

centos安装coreseek

安装依赖 yum install make gcc g gcc-c libtool autoconf automake imake mysql-devel libxml2-devel expat-devel 下载coreseek 4.1 $ wget http://www.coreseek.cn/uploads/csft/4.0/coreseek-4.1-beta.tar.gz $ tar xzvf coreseek-4.1-beta.tar.gz $ cd coreseek-4.1-beta…

HTML---HTML简介

1、HTML简介&#xff1a;*什么事HTML&#xff1f; -HypperText Markup Language&#xff1a;超文本标记语言&#xff0c;网页语言。**超文本&#xff1a;超出文本的范畴&#xff0c;使用HTML可以轻松实现简单操作。**标记&#xff1a;HTML所有的操作都是通过标记实现的&…

谷歌Android各版本的代号变迁

简单回顾下Android发展历程2003年10月&#xff0c;Andy Rubin&#xff08;安迪鲁宾&#xff09;等人创建Android公司&#xff0c;并组建Android团队。2005年8月17日&#xff0c;Google低调收购了成立仅22个月的高科技企业Android及其团队。安迪鲁宾成为Google公司工程部副总裁&…

HTMLL---HTML中常用标签(文字、注释标签)

1、文字标签和注释标签*文字标签和注释标签- <font></font>-属性* size:文字的大小&#xff0c;取值范围1-7&#xff0c;超出7默认为7* color:文字的颜色-两种表示方式**英文单词&#xff1a; red, green, blue, black, white, yellow, gray**使用十六进制数表示&a…

Map.Entry

如何简便的遍历Map 你是否已经对每次从Map中取得关键字然后再取得相应的值感觉厌倦&#xff1f; 使用JDK5的增强for循环&#xff0c;来遍历Map,简单多了&#xff0c;比Map.Entry还方便。 看代码&#xff1a; Java代码 for (String key : map.keySet()) { System.out.pri…

HTML---HTML中常用的标签(标题,水平,特殊标签)

1、标题标签、水平标签和特殊字符*标题标签- <h1>... </h1>、 <h2>... </h2>、 <h3>... </h3>、... <h6>... </h6>-特点&#xff1a;从h1到h6字体由大到小、同时 自动换行。*水平标签- <hr/>-属性** size&#xff1a;水…

图解SQL的inner join(join)、left join、right join、full outer join、union、union all的区别...

对于SQL的Join&#xff0c;在学习起来可能是比较乱的。我们知道&#xff0c;SQL的Join语法有很多inner的&#xff0c;有outer的&#xff0c;有left的&#xff0c;有时候&#xff0c;对于Select出来的结果集是什么样子有点不是很清楚。Coding Horror上有一篇文章,通过文氏图 Ven…

数据库---四中连接查询(交叉、左连接、右连接、完整查询)

个人博客 &#xff1a;https://www.siyuan.run CSDN&#xff1a;https://blog.csdn.net/siyuan 微信小程序&#xff1a;思远Y 1、交叉连接查询 : (基本不适用---得到的是两张表数据的乘积) 语法&#xff1a;SELECT * FROM 表1,表2; PS&#xff1a;与表关系无关 示例&#xff…

如何用C#语言构造蜘蛛程序

"蜘蛛"&#xff08;Spider&#xff09;是Internet上一种很有用的程序&#xff0c;搜索引擎利用蜘蛛程序将Web页面收集到数据库&#xff0c;企业利用蜘蛛程序监视竞争对手的网站并跟踪变动&#xff0c;个人用户用蜘蛛程序下载Web页面以便脱机使用&#xff0c;开发者利…

数据库---练习题(45道)

准备工作 CREATE DATABASE STUDENTS; CREATE TABLE STUDENT( SNO VARCHAR(32) PRIMARY KEY NOT NULL, SNAME VARCHAR(32) NOT NULL, SSEX VARCHAR(32) NOT NULL, SBIRTHDAY DATETIME, CLASS VARCHAR(20) ); CREATE TABLE COURSE( CNO VARCHAR(20) PRIMARY KEY NOT NULL, CNAM…

LeetCode OJ - Populating Next Right Pointers in Each Node II

题目&#xff1a; Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space.For example,Given the fo…

数据库---JDBC

1.1 JDBC概述JDBC&#xff08;Java DataBase Connectivity,java数据库连接&#xff09;是一种用于执行SQL语句的Java API。JDBC是Java访问数据库的标准规范&#xff0c;可以为不同的关系型数据库提供统一访问&#xff0c;它由一组用Java语言编写的接口和类组成。 JDBC需要连接驱…

23种设计模式之简单工厂

简单工厂模式描述的是&#xff0c;通过类的继承关系&#xff0c;父类&#xff08;工厂类&#xff09;与子类&#xff08;产品类&#xff09;&#xff0c;调用父类中的方法&#xff0c;实际干活儿的是子类中的方法&#xff1b;封装需求的不确定性&#xff0c;做出通用的编程&…

原生JDBC操作数据库流程

1、class.forName()加载数据驱动 2、DriverManager.getConnection()获取数据库连接对象。 3、根据SQL或sql会话对象&#xff0c;有两种方式Statement、PreparedStatement。 4、执行sql处理结果集&#xff0c;如果有参数就设置参数。 5、关闭结果集&#xff0c;关闭会话&#xf…