微信公众号PHP接口校验代码

接口配置信息php版本:


define("TOKEN", "yourtoken"); 

//TOKEN值
$wechatObj = new wechat();
$wechatObj->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;  
        }  
    }
}?>

你可能感兴趣的:(PHP-基础,微信/小程序)