Configuration of WeChat public account

 1. First log in to the WeChat official account -> Developer Tools -> Public Platform Test Account. The TOKEN inside must be consistent with the TOKEN defined in the file to configure successfully.


2. Verify whether the request comes from the WeChat server

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

3. Send a message to the official account to verify whether there is a reply

code show as below

	if(!empty($keyword))
                {
              		$msgType = "text";
                    $contentStr = $this->word($keyword);
                    // $contentStr ="we come";
                	$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                	echo $resultStr;
                }else{
                	echo "Input something...";
                }

        }else {
        	echo "";
        	exit;
        }
    }
    // 关键字
    public function word($keyword){
    	if($keyword === '天气'){
	    	return $contentStr ="今天天气有雨!";
        }else if($keyword === '你好'){	
	    	return $contentStr ="你好!有什么需要帮助?";   	
        }else{
	        return $contentStr ="sorry,I can not help you。";
	    
    }

 

4. The code that plays an important role is as follows

 

define("TOKEN", "weixin");
 $echoStr = $_GET["echostr"];
echo $echoStr;

 

 

Guess you like

Origin blog.csdn.net/TIANJING_er/article/details/79828027