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 Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version