从微信官网下载示例代码,把下面代码覆盖demo.php即可,如有疑惑欢迎私密。

/*

椰子园 http://www.yeziyuan.com

email coconutgarden@sina.com

CopyRight 2014 All Rights Reserved

*/

define("TOKEN", "thisisyourselftoken");

define("AppID", "nowisyourappid");

define("EncodingAESKey", "hereisyourselfencodekay43bit");

require_once('wxBizMsgCrypt.php');

$wechatObj = new wechatCallbackapiTest();

if (!isset($_GET['echostr'])) {

$wechatObj->responseMsg();

}else{

$wechatObj->valid();

}

class wechatCallbackapiTest

{

//验证签名

public function valid()

{

$echoStr = $_GET["echostr"];

$signature = $_GET["signature"];

$timestamp = $_GET["timestamp"];

$nonce = $_GET["nonce"];

$tmpArr = array(TOKEN, $timestamp, $nonce);

sort($tmpArr);

$tmpStr = implode($tmpArr);

$tmpStr = sha1($tmpStr);

if($tmpStr == $signature){

echo $echoStr;

exit;

}

}

//响应消息

public function responseMsg()

{

$timestamp = $_GET['timestamp'];

$nonce = $_GET["nonce"];

$msg_signature = $_GET['msg_signature'];

$encrypt_type = (isset($_GET['encrypt_type']) && ($_GET['encrypt_type'] == 'aes')) ? "aes" : "raw";

$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

if (!empty($postStr)){

//解密

if ($encrypt_type == 'aes'){

$pc = new WXBizMsgCrypt(TOKEN, EncodingAESKey, AppID);

$this->logger(" D \r\n".$postStr);

$decryptMsg = ""; //解密后的明文

$errCode = $pc->DecryptMsg($msg_signature, $timestamp, $nonce, $postStr, $decryptMsg);

$postStr = $decryptMsg;

}

$this->logger(" R \r\n".$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;

}

$this->logger(" R \r\n".$result);

//加密

if ($encrypt_type == 'aes'){

$encryptMsg = ''; //加密后的密文

$errCode = $pc->encryptMsg($result, $timeStamp, $nonce, $encryptMsg);

$result = $encryptMsg;

$this->logger(" E \r\n".$result);

}

echo $result;

}else {

echo "";

exit;

}

}

//接收事件消息

private function receiveEvent($object)

{

$content = "";

switch ($object->Event)

{

case "subscribe":

$content = "欢迎关注椰子园,官网http://www.yeziyuan.com ";

break;

}

$result = $this->transmitText($object, $content);

return $result;

}

//接收文本消息

private function receiveText($object)

{

$keyword = trim($object->Content);

if (strstr($keyword, "文本")){

$content = "这是个文本消息";

}else if (strstr($keyword, "单图文")){

$content = array();

$content[] = array("Title"=>"单图文标题", "Description"=>"单图文内容", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");

}else if (strstr($keyword, "图文") || strstr($keyword, "多图文")){

$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");

}else if (strstr($keyword, "音乐")){

$content = array();

$content = array("Title"=>"最炫民族风", "Description"=>"歌手:凤凰传奇", "MusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3", "HQMusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3");

}else{

$content = date("Y-m-d H:i:s",time())."\n".$object->FromUserName."\n技术支持 椰子园";

}

if(is_array($content)){

if (isset($content[0])){

$result = $this->transmitNews($object, $content);

}else if (isset($content['MusicUrl'])){

$result = $this->transmitMusic($object, $content);

}

}else{

$result = $this->transmitText($object, $content);

}

$content = "本服务号暂未开发,目前仅供活动测试用,感谢您长期以来对椰子园的支持。";

$result = $this->transmitText($object, $content);

return $result;

}

//回复文本消息

private function transmitText($object, $content)

{

$xmlTpl = "

%s

";

$result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $content);

return $result;

}

//回复图文消息

private function transmitNews($object, $newsArray)

{

if(!is_array($newsArray)){

return;

}

$itemTpl = "

";

$item_str = "";

foreach ($newsArray as $item){

$item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);

}

$xmlTpl = "

%s

%s

$item_str

";

$result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray));

return $result;

}

//回复音乐消息

private function transmitMusic($object, $musicArray)

{

$itemTpl = "

";

$item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']);

$xmlTpl = "

%s

$item_str

";

$result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time());

return $result;

}

//日志记录

public 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 = 500000;

$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('Y-m-d H:i:s').$log_content."\r\n", FILE_APPEND);

}

}

}

?>

微信php签名验证_微信公众平台安全模式消息体签名及加解密PHP代码示例相关推荐

  1. 微信公众平台安全模式消息体签名及加解密PHP代码示例

    从微信官网下载示例代码,把下面代码覆盖demo.php即可,如有疑惑欢迎私密. <?php /*椰子园 http://www.yeziyuan.comemail coconutgarden@si ...

  2. C#微信公众号开发系列教程三(消息体签名及加解密)

    http://www.cnblogs.com/zskbll/p/4139039.html C#微信公众号开发系列教程一(调试环境部署) C#微信公众号开发系列教程一(调试环境部署续:vs远程调试) C ...

  3. java 消息签名_微信公众平台消息体签名及加解密实例(Java)

    前言: 最近在研究微信公众平台的开发,玩得不亦乐乎.基本的回复功能已经实现了,而且回复用到了图灵机器人的接口.其实图灵机器人已经有微信接口可以直接调用.如果项目的需要,想要做个性化需求的话,用这种方式 ...

  4. 微信php签名验证_微信小程序API 用户数据的签名验证和加解密

    微信小程序API 用户数据的签名验证和加解密 用户数据的签名验证和加解密 数据签名校验 为了确保 开放接口 返回用户数据的安全性,微信会对明文数据进行签名.开发者可以根据业务需要对数据包进行签名校验, ...

  5. 企业微信加密消息体_微信公众平台开发者中心安全模式消息体加解密实现

    关键字:微信公众平台 消息体签名 消息体加解密 EncodingAESKey 安全模式 一.消息体加解密 微信公众平台在配置服务器时,提供了3种加解密的模式供开发者选择,即明文模式.兼容模式.安全模式 ...

  6. 公众平台模板消息所在行业_第三方工具微信公众号模板消息群发如何操作?

    当下,公众平台模板消息功能仅支持添加模板,修改所在行业,如果想要群发模板消息,可以自己根据公众平台的接口编程实现,也可通过微号帮平台的模板消息群发功能实现,均可以让微信公众号群发模板消息,模板消息即按 ...

  7. 微信公众 mysql回复图片_微信公众平台开发消息回复总结介绍

    一.简介 微信公众平台提供了三种消息回复的格式,即文本回复.音乐回复和图文回复,在这一篇文章中,我们将对这三种消息回复的格式做一下简单讲解,然后封装成函数,以供读者使用. 二.思路分析 对于每一个PO ...

  8. 公众平台模板消息所在行业_如何使用微信公众号第三方平台群发模板消息助手?...

    对于微信公众号群发模板消息助手的实现,公众号后台提供了接口编程实现,微号帮平台提供了模板消息群发功能实现,均可以让微信公众号群发模板消息,模板消息即按固定格式的文本模块消息,没有图文形式,纯固定格式的 ...

  9. 另类保存微信公众平台历史消息的方法 - 星标消息

    前面怎样把微信聊天记录导出备份到电脑[微信公众平台技巧]介绍的通过复制源代码来保存微信公众平台历史消息的方法,有网友反映说不会用批量替换.不会批量换行保存,一头雾水.这里我们就说个简单的方法,直接用星 ...

最新文章

  1. 漫画:禅道程序员的一天
  2. 2018新年快乐 !(附幸运读者名单)
  3. Jupyter on Kubernetes机器学习-MLflow
  4. 基于 Asio 的 C++ 网络编程
  5. 【JavaScript高级程序设计】读书笔记之一 —— 理解函数
  6. 在SunOS5.8/solaris7上使用Xerces-C解析器
  7. hdu 1115(多边形重心)
  8. HTML第八章ppt,第八章 web基础教程之HTML篇v1.0.ppt
  9. 获取Authorize.Net Transaction Key ( Getting Your Authorize.Net Transaction Key )
  10. Spring Data Elasticsearch案例详解
  11. 停止计算机sql服务,“本地计算机上的SQLSERVER服务启动后又停止了”解决方法
  12. idea git 整合使用
  13. [COLING18]两种成分句法分析的局部特征模型
  14. 电脑e盘里的文件误删了 如何恢复教程分享
  15. 【无线通信】基于matlab无线传感网络WSN仿真【含Matlab源码 1237期】
  16. 关于连接PostgreSQL时提示 FATAL: password authentication failed for user 连接用户名 的解决方法...
  17. 斐讯K3刷官方root版
  18. 常见设计模式之(五):观察者模式
  19. 使用pygame制作贪吃蛇小游戏
  20. signature=de4fefc549f99f0b0c76a2cec8e340bf,Diagnostics based on faulty signature

热门文章

  1. [深度学习]什么叫梯度学习
  2. 分析JQ作者的类实现过程
  3. 使用SoapUI生成wsdl文件客户端(二)
  4. spring mvc 中自定义404页面在IE中无法显示favicon.ico问题的解决方法。
  5. 七、【应用的主要框架】
  6. 嵌入式软件架构设计之分层设计
  7. Sourcegraph 代码搜索
  8. gitee添加成员_成员权限管理,到底能有多精细?
  9. php twig输出html,php – HTML不呈现[Twig] / [Slim]
  10. 基于matlab的光伏电池通用数学模型,基于MATLAB的光伏电池通用数学模型.doc