Home > Article > Backend Development > The first entry into the WeChat trap. The first tweet on the WeChat official account. The first WeChat official account of China Business News. The first message on the WeChat official account.
signature: WeChat encrypted signature, signature combines the token parameter filled in by the developer with the timestamp parameter and nonce parameter in the request.
Among them:
timestamp: timestamp
nonce: random number
echostr: random string2. Concatenate the three parameters into a string and use sha1 encryption.
3. The developer compares the encrypted string with the signature; if they are the same, the request comes from WeChat and is credible.
<span style="font-family:KaiTi_GB2312;font-size:14px;"><?php define("TOKEN","weixin"); $weixinObj = new Wechat(); $weixinObj->valid(); class Wechat{ public function valid(){ $echoStr = $_GET['echostr']; if($this->checkSignature()){ echo $echoStr; 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; } } }</span>
The above introduces the first step in WeChat, including the first aspect of WeChat. I hope it will be helpful to friends who are interested in PHP tutorials.