Home > Backend Development > PHP Tutorial > 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.

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.

WBOY
Release: 2016-07-29 08:50:45
Original
2114 people have browsed it
Verification principle:

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 string
1. Sort the three parameters token, timestamp, and nonce in lexicographic order.

2. 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>
Copy after login

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.

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template