In this WeChat public platform development tutorial, we assume that you already have the basics of PHP language program, MySQL database, computer network communication, and HTTP/XML/CSS/JS.
We will use the WeChat public account Fangbei Studio as an example to explain. See the QR code at the bottom.
This series of tutorials will guide you to complete the following tasks:
Apply for Baidu Cloud Platform resources
Enable WeChat public platform development Mode
##First article applying for server resources
Create Baidu Cloud Application
Apply for an account
Log in to http://developer .baidu.com/bae, use your email or mobile phone to register an account. Registration requires binding your mobile phone and verifying your email.Create application
After successful registration and login, clickon the upper right side, and the following window will pop up.
Fill in any application name, select "Mobile Web Application" as the access method, and then click OK.
说明:在以下的教程中,您可以将所有我填写为pondbay的地方改为你的一个相应的名称,如果您没有想好名称,最简单的方法就是qq这两个字符+qq号码,比如方倍工作室的QQ是1354386063,那么就将"pondbay"改为"qq1354386063"
注意:此处填写的域名将要在下面填写URL时用到。可以先保存下来。
Original text: http://www.php.cn/
Create version
In version management , click to create a new version #Fill in 0 for the version number, and then save.Original text: http://www.php.cn/
Upload code
Change the token in the following code Change it to your name and save as index.php.注意:此处填写的Token将要在下面填写URL时用到。可以先保存下来。
<?php/* CopyRight 2013 www.doucube.com All Rights Reserved*/define("TOKEN", "pondbay");$wechatObj = new wechatCallbackapiTest();if (isset($_GET['echostr'])) { $wechatObj->valid(); }else{ $wechatObj->responseMsg(); }class wechatCallbackapiTest { public function valid() { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ echo $echoStr; 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; }else{ return false; } } public function responseMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!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($keyword == "?" || $keyword == "?") { $msgType = "text"; $contentStr = date("Y-m-d H:i:s",time()); $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } }else{ echo ""; exit; } } }?>
, as shown below.
百度云应用的创建就成功了。
第二篇 启用开发模式
微信公众平台开发模式
高级功能
微信公众平台地址:https://mp.weixin.qq.com
登录微信公众平台后台,选择高级功能,进入后就看到两种模式
我们需要先关闭编辑模式。点击编辑模式的进入
滑动关闭
开发模式
进入开发模式里面
点击成为开发者
弹出URL和Token填写框
此处的URL为创建百度云应用的域名,包括后面的duapp.com,而Token为index.php中定义的值。在这篇教程中如下:
URL: http://pondbay.duapp.com Token: pondbay
填写如下图,
提交成功
再滑动右上角启用按钮。
恭喜,你成功启用开发模式。
自动回复
在上面的例子中,实现了一个发送“?”就能回复当前时间的功能。
效果如下:
至此,你的微信公众平台账号已经实现自动回复了。
更多微信公众平台消息接口开发 启用接口 相关文章请关注PHP中文网!