search
HomeWeChat AppletWeChat DevelopmentWeChat development tutorial series (1)

WeChat development tutorial series (1)

Mar 31, 2017 pm 02:18 PM
WeChat development

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

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;
        }
    }
}
?>

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.