search
HomeWeChat AppletWeChat DevelopmentDetailed introduction to WeChat interface development

The creation process requires signature verification, which is described as follows:

After the public platform user submits the information, we will request the filled-in Url in the form of a GET request, and bring Four parameters:

* signature — WeChat encrypted signature

* timestamp — timestamp

* nonce — random number

* echostr — random string

Developers verify the legality of URL access by checking signature. If this GET request returns the echostr parameter content as it is, the access will take effect, otherwise the access will fail. The verification signature will be combined with the token parameter, timestamp parameter and nonce parameter filled in by the developer. The encryption process is:

* Sort the three parameters of token, timestamp and nonce in lexicographic order

* Put the three parameters into lexicographic order. Parameter strings are spliced ​​into one string for SHA1 encryption

* The encrypted string obtained by the developer can be compared with the signature to identify that the request originated from WeChat.

Code:

<?  
        $signature = $_GET[&#39;signature&#39;];  
        $timestamp = $_GET[&#39;timestamp&#39;];  
        $nonce = $_GET[&#39;nonce&#39;];      
                  
        $token = TOKEN;  
        $tmpArr = array($token, $timestamp, $nonce);  
        sort($tmpArr);  
        $tmpStr = implode( $tmpArr );  
        $tmpStr = sha1( $tmpStr );  
          
        if( $tmpStr == $signature ){  
            return $_GET[&#39;echostr&#39;];  
        }else{  
            return false;  
        }  
?>

However, WeChat does not use json to transmit data:

So we need to use simplexml_load_string to load XML data as an object, and also find POST The method is not urlencode, so set HTTP_RAW_POST_DATA, and then you can read the data.

ToUserName The WeChat ID of the message receiver, usually the WeChat account of the public platform

FromUserName The WeChat ID of the message sender

CreateTime message creation time

MsgType text message is text

Content message content

<?php  
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //符合微信的POST规范  
if (!emptyempty($postStr))  
{  
 
    $postObj = simplexml_load_string($postStr, &#39;SimpleXMLElement&#39;, LIBXML_NOCDATA); //XML转对象函数,可能最近这一两年入行的不太清楚XML函数  
      
    //数据从对象取出  
    $fromUsername = $postObj->FromUserName;   
    $toUsername = $postObj->ToUserName;  
    $CreateTime = $postObj->CreateTime;  
    $MsgType = $postObj->MsgType;  
    $Content = $postObj->Content;  
 
    $keyword = trim($postObj->Content); //安全Trim  
    $time = time();  
    //XML数据体  
    $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(!emptyempty( $keyword )) //如果发信息来了,不是空白POST,微信规定立即回复,不用推送.  
    {  
        $msgType = "text"; //定义类型  
        $contentStr = "Hello World,I am Tater!"; //回复  
        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); //记住,Tpl是要载入的  
        echo $resultStr; //输出,以便微信抓!  
    }  
    else 
    {  
        echo "What are you say!"; //输入信息有问题,提示输入!  
    }  
 
}  
else 
{  
    echo "";  
    exit;  
}  
 
 
?>

The above is the detailed content of Detailed introduction to WeChat interface development. 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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function