Example sharing of WeChat development to obtain JSAPI TICKET

小云云
Release: 2023-03-20 06:48:01
Original
1983 people have browsed it

This article mainly introduces the relevant methods of obtaining JSAPI_TICKET in WeChat development in detail. It has certain reference value. Interested friends can refer to it

1. Obtaining 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, file storage can be used 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

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}&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 format of URL key-value pair (i.e. key1=value1&key2=value2...) Spliced ​​into string string1:

Perform sha1 signature on string1 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.

For details, please refer to: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183

Related recommendations:

About WeChat Get jsapi_ticket during development

Instance tutorial of getting jsapi_ticket for WeChat development

WeChat development WeChat jsapi selects pictures, uploads pictures, previews and downloads pictures method

The above is the detailed content of Example sharing of WeChat development to obtain JSAPI TICKET. 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