WeChat public platform message interface development and enable interface

高洛峰
Release: 2017-02-21 16:30:23
Original
1488 people have browsed it

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:

  1. Apply for Baidu Cloud Platform resources

  2. 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, click

on 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"
Copy after login


微信公众平台消息接口开发 启用接口

Select "Cloud Environment (BAE)",

微信公众平台消息接口开发 启用接口

In the new window, fill in the name of the application domain name, select PHP as the environment type, select others according to your own needs or use the default values ​​​​as shown above, and then click OK

注意:此处填写的域名将要在下面填写URL时用到。可以先保存下来。
Copy after login

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时用到。可以先保存下来。
Copy after login


<?php/*
    CopyRight 2013 www.doucube.com  All Rights Reserved*/define("TOKEN", "pondbay");$wechatObj = new wechatCallbackapiTest();if (isset($_GET[&#39;echostr&#39;])) {    $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;
        }
    }
}?>
Copy after login


Then compress it into ZIP format, RAR format cannot be used

微信公众平台消息接口开发 启用接口

This will generate an index.zip file.

Back to version management

微信公众平台消息接口开发 启用接口

Select the package to upload and update

微信公众平台消息接口开发 启用接口

Select the zip you just compressed package, click Upload.

微信公众平台消息接口开发 启用接口

Confirm it takes effect, and click

, as shown below. 微信公众平台消息接口开发 启用接口

微信公众平台消息接口开发 启用接口

百度云应用的创建就成功了。

 

第二篇 启用开发模式

微信公众平台开发模式

高级功能

微信公众平台地址:https://mp.weixin.qq.com 

登录微信公众平台后台,选择高级功能,进入后就看到两种模式

微信公众平台消息接口开发 启用接口

我们需要先关闭编辑模式。点击编辑模式的进入

微信公众平台消息接口开发 启用接口

滑动关闭

微信公众平台消息接口开发 启用接口

 

开发模式

进入开发模式里面

微信公众平台消息接口开发 启用接口

 点击成为开发者

微信公众平台消息接口开发 启用接口

弹出URL和Token填写框

微信公众平台消息接口开发 启用接口

此处的URL为创建百度云应用的域名,包括后面的duapp.com,而Token为index.php中定义的值。在这篇教程中如下:


URL:     http://pondbay.duapp.com
Token:  pondbay
Copy after login


填写如下图,

微信公众平台消息接口开发 启用接口

 提交成功

微信公众平台消息接口开发 启用接口

再滑动右上角启用按钮。

微信公众平台消息接口开发 启用接口

恭喜,你成功启用开发模式。

 

自动回复

在上面的例子中,实现了一个发送“?”就能回复当前时间的功能。
效果如下:

微信公众平台消息接口开发 启用接口

至此,你的微信公众平台账号已经实现自动回复了。

更多微信公众平台消息接口开发 启用接口 相关文章请关注PHP中文网!


 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!