Home > Backend Development > PHP Tutorial > About getting jsapi_ticket in WeChat development

About getting jsapi_ticket in WeChat development

炎欲天舞
Release: 2023-03-14 16:04:01
Original
4129 people have browsed it

1. Acquisition process

1. Obtain access_token

2. Exchange access_token for jsapi_ticket

3. Signature algorithm

The signature generation rules are as follows: The fields involved in the signature include noncestr (random string), valid jsapi_ticket, timestamp (timestamp), url (URL of the current web page, excluding # and its following parts). After sorting all the parameters to be signed according to the ASCII code of the field name from small to large (lexicographic order), use the URL key-value pair format (i.e. key1=value1&key2=value2...) to splice them into a string string1. It should be noted here that all parameter names are lowercase characters. Perform sha1 encryption on string1, use original values ​​for field names and field values, and do not perform URL escaping.

2. Specific implementation method

1. Obtain access_token


/**
 * [getAccessToken description] 获取access_token
 * @return [type] [description] */private  function getAccessToken() {
    $data = $this->getFile($this->accessTokenFile);    if(time() - $data['time'] > 0){
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appID}&secret={$this->appsecret}";
        $re = $this->httpGet($url);
        $access_token = $re['access_token'];        
        if(isset($access_token)){
            $data['access_token']  = $access_token;
            $data['time'] = time() + 7200;
            $this->setFile($this->accessTokenFile,json_encode($data));
        }
    }else{
        $access_token = $data['access_token'];
    }   return $access_token;
}
Copy after login

The validity time of access_token is 7200s. Therefore, you can use the file storage method to save it to avoid multiple requests;

2. Get jsapi_ticket


/**
 * [getJsapiTicket description] 获取jsapi_ticket
 * @return [type] [description] */private function getJsapiTicket() {
    $access_token = $this->getAccessToken();
    $jsapi_ticket = $this->getFile($this->jsapiTicketFile);    if(time() - $jsapi_ticket['time'] > 0) {
        $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$access_token}&type=jsapi";
        $re = $this->httpGet($url);
        $this->preArr($re);
        $jsapi_ticket = $re['ticket'];        
        if(isset($jsapi_ticket)){
            $data['jsapi_ticket'] = $jsapi_ticket;
            $data['time'] = time() + 7200;
            $this->setFile($this->jsapiTicketFile, json_encode($data));
        }
    }else{
        $jsapi_ticket = $jsapi_ticket['jsapi_ticket'];
    }   return $jsapi_ticket;
}
Copy after login

through access_token Exchange, get jsapi_ticket, the validity period is also 7200s;

3. Generate signature


/**
 * [getSignpackage description] 获取签名
 * @return [type] [description] */public function getSignpackage(){
    $jsapi_ticket = $this->getJsapiTicket();    // 注意 URL 一定要动态获取,不能 hardcode.
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $noncestr = $this->createNonceStr();
    $timestamp = time();

    $string1 = "jsapi_ticket={$jsapi_ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$url}";
    $signature = sha1($string1);    
    $signPackage = array(        
        'appId'     => $this->appID,        
        'nonceStr'  => $noncestr,        
        'timestamp' => $timestamp,        
        'signature' => $signature,
    );    
    return $signPackage;
}
Copy after login

Signature algorithm,

Use the URL key-value pair format (i.e. key1=value1&key2=value2...) to splice it into a string string1:

Sign string1 with sha1 to get signature:

Notes:

1. The noncestr and timestamp used for signature must be the same as the nonceStr and timestamp in wx.config.

2. The URL used for signature must be the complete URL of the page calling the JS interface.

3. For security reasons, developers must implement signature logic on the server side

The above is the detailed content of About getting jsapi_ticket in WeChat development. For more information, please follow other related articles on the PHP Chinese website!

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