第一步:基本配置
设置url(为资源服务器,可以进行token验证和返回token的一个地址下)。
Token为返回的一个token
EncodingAESKey:随机生成。
第二步:Appid和Appsecret设置和生成。
获取AppId和AppSecret:成为开发者后,在“开发者中心中”页面,可获取AppId和AppSecret
两个参数。
第三步:编写文件
在url路径中的文件中返回一个echostr。即可
若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,成为开发者成功(不过在此过程中需要进行signature对请求进行校验);可不进行校验
此GET请求中有四个参数:
signature:微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
timestamp:时间戳
nonce:随机数
echostr:随机字符串.
)将token、timestamp、nonce三个参数进行字典序排序
2)将三个参数字符串拼接成一个字符串进行sha1加密
3)开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。
微信接入指南——wx_sample.php,可以返回文本信息。
define("TOKEN", "gewechatbtp");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
$wechatObj->responseMsg();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
if (!empty($postStr)){
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{