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.

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
WBOYOriginal
2016-07-29 08:50:452078browse
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>

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.

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