当前位置 : 主页 > 网络编程 > PHP >

tips-1051767-02.php

来源:互联网 收集:自由互联 发布时间:2021-06-28
tips-1051767-02.php wechat.inc.php _appid = $appid; $this-_appsecret = $appsecret; $this-_token = $token; } private function _request($curl, $https = true, $post = "get", $data = null, $header = null, $type = null) { $Curl = new Curl(); ret
tips-1051767-02.php
wechat.inc.php


 _appid = $appid;
        $this->_appsecret = $appsecret;
        $this->_token = $token;
    }

    private function _request($curl, $https = true, $post = "get", $data = null, $header = null, $type = null)
    {

        $Curl = new Curl();
        return $Curl->_request($curl, $https, $post, $data, $header, $type);
    }

    public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature())
        {
            echo $echoStr;
            exit;
        }
    }

    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $token = $this->_token;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        
        if( $tmpStr == $signature )
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    private function _getAccessToken()
    {
        $file = "./accessToken";
        
        if (file_exists($file))
        {
            $content = file_get_contents($file);
            $content = json_decode($content);
            if (time() - filemtime($file) < $content->expires_in)
            {
                return $content->access_token;
            }
        }
        $content = $this->_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->_appid."&secret=".$this->_appsecret);
        
        file_put_contents($file, $content);
        
        $content = json_decode($content);
        return $content->access_token;
    }
    
    public function send_template_message($data)
    {
        return $this->_request("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$this->_getAccessToken(), true, "post", $data);
    }
}
?>
上一篇:tips-1051767-01.php
下一篇:tips-1051767-03.php
网友评论