WeChat development tutorial series (1)

高洛峰
Release: 2017-03-31 14:18:19
Original
2212 people have browsed it

First of all, we need to apply for a WeChat public account. Because this public account belongs to the company, it is not convenient to disclose it to everyone! I believe that simply applying for a job will be no problem! After the application is successful, "Advanced Functions" will appear in the menu bar, as shown below:

WeChat development tutorial series (1)

There are 2 modes to choose from, "Edit Mode" and "Development Mode", above Since they are all described in detail, I don’t want you to explain them anymore!

Here we are going to talk about the development mode. Okay, now you can enter the explanation of development mode!

Then we configure the URL and Token values, as shown below:

WeChat development tutorial series (1)

URL: Fill in the access URL where we put the demo, for example: http://www .123.com/wx_sample.php

Token: This value can be written casually.

Open the file wx_sample.php and modify the following content

define("TOKEN", "weixin"); //修改成自己填写的token
Copy after login

After filling it in, you can submit it!

After successful verification, we can write a small example to test it, the following code:

<?php
/**
  * wechat php test
  */
// define
// your
// token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$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"];
                                  
        // extract
        // post
        // data
        if(!empty($postStr)) {
                                      
            $postObj = simplexml_load_string($postStr, &#39;SimpleXMLElement&#39;, 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;
        } else {
            return false;
        }
    }
}
?>
Copy after login

The simplest reply message is completed!

The above is the detailed content of WeChat development tutorial series (1). For more information, please follow other related articles on the PHP Chinese website!

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