您的位置:首页 > 移动开发 > 微信开发

微信公众平台开发官方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;
}
}
}

?>

下面来个运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: