微信公众平台接口开发指南(5页).doc
-微信公众平台接口开发指南-第 5 页微信公众平台接口开发指南l 注册成为开发者注册公众账号,需要登记自己的真实身份信息注意:设置公众号信息时一定要谨慎,公众号名称与微信号填写后是不能修改的,需事先认真想好注册完成后,进入系统,选择导航上的高级功能其中有两个模式:编辑模式与开发模式默认开启的为编辑模式进入编辑模式,点击右上角的关闭链接进入开发模式,点击右上角的开启按钮开启后可以看到:点击成为开发者l URL、TOKEN认证成为开发者的第一步就是填写URL、TOKEN信息,来对你服务器进行验证1) 把下面代码复制并保存为一个php文件(如weixin.php)<?php * wechat php test/define your tokendefine("TOKEN", "weixin");$wechatObj = new wechatCallbackapiTest();$wechatObj->valid();class wechatCallbackapiTestpublic 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" /extract post dataif (!empty($postStr) $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 "Input something." else echo "" exit;private function checkSignature() $signature = $_GET"signature" $timestamp = $_GET"timestamp" $nonce = $_GET"nonce"$token = TOKEN;$tmpArr = array($token, $timestamp, $nonce);sort($tmpArr);$tmpStr = implode( $tmpArr );$tmpStr = sha1( $tmpStr );if( $tmpStr = $signature )return true;elsereturn false;2) 修改TOKENTOKEN是用来进行交互安全认证的,你自己随意定义,注意保证安全定义后修改代码,在代码顶部找到define("TOKEN", "weixin");把值改为你自己的TOKEN值(如:mytoken),保存文件,然后上传到服务器,要确保可以访问3) 填写URL TOKEN信息回到公众平台页面,把URL TOKEN信息填写后提交,需要保证URL与上传的php文件地址一致,并且TOKEN值与php中定义的一致信息填写正确后提交,正常会提示完成信息至此,已经正式成为了开发者,接下来就可以安装自己的思路开发程序了l 微信交互示例做一个简单的示例:用户发送什么文字,我们就回复什么文字注意:复制代码后要把TOKEN值修改为自己的<?php * wechat php test/define your tokendefine("TOKEN", "xxx");$str = ''$wechatObj = new wechatCallbackapiTest();$wechatObj->valid();class wechatCallbackapiTestpublic function valid() $echoStr = $_GET"echostr" /valid signature , option if($this->checkSignature() echo $echoStr; $this->responseMsg(); exit; public function responseMsg()/get post data, May be due to the different environments$postStr = $GLOBALS"HTTP_RAW_POST_DATA" /extract post dataif (!empty($postStr) $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>1</FuncFlag></xml>" if(!empty( $keyword ) $msgType = "text" $contentStr = $keyword; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; else echo "Input something." else echo "" exit;private function checkSignature() $signature = $_GET"signature" $timestamp = $_GET"timestamp" $nonce = $_GET"nonce"$token = TOKEN;$tmpArr = array($token, $timestamp, $nonce);sort($tmpArr);$tmpStr = implode( $tmpArr );$tmpStr = sha1( $tmpStr );if( $tmpStr = $signature )return true;elsereturn false;l 用户关注后发送欢迎信息在实际应用中,用户第一次关注是,我们需要发送欢迎和提示信息,就需要下面的判断代码if($keyword = "Hello2BizUser") $contentStr = "欢迎信息"开发文档中写明,用户关注后服务器自动发送过来的字符串为"Hello2BizUser",我们就依次来判断了注意:信息模板中此项<FuncFlag>1</FuncFlag>的值要设为1,这样用户微信中才会显示有新消息的标志此文档总结了微信公众平台开发的起步过程,接下来就需要大家发挥自己创造力了,祝大家开发出更多更有益的应用