Home  >  Article  >  Backend Development  >  Example tutorial for WeChat development to obtain jsapi_ticket

Example tutorial for WeChat development to obtain jsapi_ticket

零下一度
零下一度Original
2017-07-20 17:28:231888browse

Instance tutorial for WeChat development to obtain jsapi_ticket

1. Obtaining process

1. Obtain access_token

2. Exchange access_token for jsapi_ticket

3. Signature algorithm

The signature generation rules are as follows: fields participating 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 = "{$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;
}

The validity time of access_token is 7200s, so it can be used Save the file using the file storage method 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 = "{$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;
}

Exchange through access_token to get jsapi_ticket, validity period 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}×tamp={$timestamp}&url={$url}";
    $signature = sha1($string1);    
    $signPackage = array(        'appId'     => $this->appID,        'nonceStr'  => $noncestr,        'timestamp' => $timestamp,        'signature' => $signature,
    );    return $signPackage;
}

Signature algorithm,

uses the format of URL key-value pair (i.e. key1=value1&key2=value2...) spliced ​​into string string1:

Sign string1 with sha1 and get signature:

Notes:

1. Noncestr and signature used timestamp must be the same as 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 Example tutorial for WeChat development to obtain jsapi_ticket. For more information, please follow other related articles on the PHP Chinese website!

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