Home WeChat Applet WeChat Development Detailed introduction to WeChat interface development

Detailed introduction to WeChat interface development

Mar 24, 2017 pm 02:12 PM

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;  
        }  
?>
Copy after login

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;  
}  
 
 
?>
Copy after login

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 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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)