微信公众平台开发官方Demo的分析与搭建
2016-10-05 16:15
393 查看
在开发者工具?》开发者文档》开始开发》接入指南》第二步最后 有个PHP实例下载。
在每次第第一次填写服务器配置的时候 ,都需要验证token发送echostr.
下面是代码:(官方提供的例子,有个函数有问题,本人调试了几个小时,终于找到: libxml_disable_entity_loader(true);注释掉就ok)
wx_sample.php
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
/*******
如果是第一次使用接口 就需要 返回 echostr参数(过后不用)
*******/
if(isset($_GET['echostr'])){
$wechatObj->valid();
}else{
$wechatObj->responseMsg();
}
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
/********
总是产生 $HTTP_RAW_POST_DATA 变量包含有原始的 POST 数据。
此变量仅在碰到未识别 MIME 类型的数据时产生。
$HTTP_RAW_POST_DATA 对于 enctype="multipart/form-data" 表单数据不可用
如果post过来的数据不是PHP能够识别的,可以用 $GLOBALS['HTTP_RAW_POST_DATA']来接收,
比如 text/xml 或者 soap 等等
解释:
$GLOBALS['HTTP_RAW_POST_DATA']存放的是POST过来的原始数据。
$_POST或$_REQUEST存放的是 PHP以key=>value的形式格式化以后的数据。
但$GLOBALS['HTTP_RAW_POST_DATA']中是否保存POST过来的数据取决于centent-Type的设置,即POST数据时
必须显式示指明Content-Type: application/x-www-form-urlencoded,POST的数据才会存放到 $GLOBALS['HTTP_RAW_POST_DATA']中
************/
//extract post data
if (!empty($postStr)){
/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
the best way is to check the validity of xml by yourself */
/*************
下面这个是服务器的原因 本人注释掉 就运行正常了
*********/
// libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
if(!empty( $keyword ))
{
$msgType = "text";
$contentStr = "Welcome to wechat world!";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
}else {
echo "";
exit;
}
}
/*****************************
验证消息的确来自微信服务器:
signature微信加密签名 结合开发者填写的token参数和情趣中的timestamp时间戳,noce参数
nonce随机数
echostr随机字符串
开发者通过检验signature对请求进行校验(下面有校验方式)。若确认此次GET请求来自微信服务器
请原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失效。加密/校验流程:
1.将token ,timestamp,nonce三个参数进行字典序排序
2.将三个参数字符串拼成一个字符串进行sha1加密
3.开发者获得加密后的字符串可与signature对比,标识该请求来自微信。
********************************/
private function checkSignature()
{
// you must define TOKEN by yourself
if (!defined("TOKEN")) {
throw new Exception('TOKEN is not defined!');
}
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
// use SORT_STRING rule 字典序排序
sort($tmpArr, SORT_STRING);
//|拼接
$tmpStr = implode( $tmpArr );
//|sha1加密
$tmpStr = sha1( $tmpStr );
//|判断是否相等
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>
下面来个运行结果:
在每次第第一次填写服务器配置的时候 ,都需要验证token发送echostr.
下面是代码:(官方提供的例子,有个函数有问题,本人调试了几个小时,终于找到: libxml_disable_entity_loader(true);注释掉就ok)
wx_sample.php
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
/*******
如果是第一次使用接口 就需要 返回 echostr参数(过后不用)
*******/
if(isset($_GET['echostr'])){
$wechatObj->valid();
}else{
$wechatObj->responseMsg();
}
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
/********
总是产生 $HTTP_RAW_POST_DATA 变量包含有原始的 POST 数据。
此变量仅在碰到未识别 MIME 类型的数据时产生。
$HTTP_RAW_POST_DATA 对于 enctype="multipart/form-data" 表单数据不可用
如果post过来的数据不是PHP能够识别的,可以用 $GLOBALS['HTTP_RAW_POST_DATA']来接收,
比如 text/xml 或者 soap 等等
解释:
$GLOBALS['HTTP_RAW_POST_DATA']存放的是POST过来的原始数据。
$_POST或$_REQUEST存放的是 PHP以key=>value的形式格式化以后的数据。
但$GLOBALS['HTTP_RAW_POST_DATA']中是否保存POST过来的数据取决于centent-Type的设置,即POST数据时
必须显式示指明Content-Type: application/x-www-form-urlencoded,POST的数据才会存放到 $GLOBALS['HTTP_RAW_POST_DATA']中
************/
//extract post data
if (!empty($postStr)){
/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
the best way is to check the validity of xml by yourself */
/*************
下面这个是服务器的原因 本人注释掉 就运行正常了
*********/
// libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
if(!empty( $keyword ))
{
$msgType = "text";
$contentStr = "Welcome to wechat world!";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
}else {
echo "";
exit;
}
}
/*****************************
验证消息的确来自微信服务器:
signature微信加密签名 结合开发者填写的token参数和情趣中的timestamp时间戳,noce参数
nonce随机数
echostr随机字符串
开发者通过检验signature对请求进行校验(下面有校验方式)。若确认此次GET请求来自微信服务器
请原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失效。加密/校验流程:
1.将token ,timestamp,nonce三个参数进行字典序排序
2.将三个参数字符串拼成一个字符串进行sha1加密
3.开发者获得加密后的字符串可与signature对比,标识该请求来自微信。
********************************/
private function checkSignature()
{
// you must define TOKEN by yourself
if (!defined("TOKEN")) {
throw new Exception('TOKEN is not defined!');
}
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
// use SORT_STRING rule 字典序排序
sort($tmpArr, SORT_STRING);
//|拼接
$tmpStr = implode( $tmpArr );
//|sha1加密
$tmpStr = sha1( $tmpStr );
//|判断是否相等
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>
下面来个运行结果:
相关文章推荐
- 微信公众平台消息接口开发(1)-官方的Demo有问题
- struts开发流程,环境搭建,加入jar包的原因分析.
- Window XP驱动开发(二) 环境搭建(VS2008+WDK+DDKWzard)及示例源码分析
- Cocos2d-x开发环境搭建和报错分析
- 微信公众平台开发(62)股票行情及分析
- 微信公众平台开发(62)股票行情及分析
- 基于mini2440的qtopia应用程序开发之一:搭建平台+友善之臂的错误分析
- 一、新浪微博站内应用demo分析【新浪微博应用开发】
- 微信公众平台开发(62)股票行情及分析
- 微信公众平台开发(62)股票行情及分析
- 微信公众平台开发(PHP)(二) 微信公众平台示例代码分析
- demo for iphone苹果官方例子ios开发
- Android学习笔记---Android概念,开发环境的搭建,前景分析,模拟器的使用....
- Android架构分析之Android开发环境搭建
- 微信在android平台上开发的注意事项(以官方提供的demo为例):
- 关于Samsung S4开发蓝牙4.0应用的资料及官方demo的讨论。
- 【iOS-cocos2d-X 游戏开发之三】Mac下配置Android NDK环境并搭建Cocos2d-x环境并Eclipse正常编译运行Cocos2dX自带TestsDemo项目!
- 微信公众平台开发(二) 微信公众平台示例代码分析
- 微信公众平台开发教程(三) 基础框架搭建
- 微信公众平台开发之基于百度 BAE3.0 的开发环境搭建(采用 Baidu Eclipse)