-How to store and update Access_token regularly

Source: Internet
Author: User
Tags curl time limit

Source: 53002611

We know that request Access_token will return such a JSON, including Access_token (voucher) and expires_in (voucher time limit)

When we need to access_token the time does not need to request the server every time, because this credential in 2 hours are valid, then will be saved up, there are several ways to choose: 1.Redis; 2. Database; 3 file storage.

I'm using the way the database is stored:

---  the structure of the table ' Accesstoken '  --  CREATE table IF not EXISTS ' Accesstoken ' (    ' a_id ' int (one) not NULL,    ' a_to Ken ' varchar (COLLATE) utf8_bin not NULL,    ' a_date ' int (one) not null,    ' a_update ' int (one-by-one) DEFAULT NULL  ) ENGI Ne=innodb auto_increment=29 DEFAULT Charset=utf8 collate=utf8_bin;  

So here's the PHP code:

       Get Access_token of course before this please connect your own database function Curl ($appid, $appsecret) {$url = "Https://api.weixin.qq.com/cgi-bi N/token?grant_type=client_credential&appid= ". $appid."         &secret= ". $appsecret;         $ch = Curl_init ();         curl_setopt ($ch, Curlopt_timeout, 5);         curl_setopt ($ch, Curlopt_url, $url);         curl_setopt ($ch, Curlopt_returntransfer, 1);         curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE);      $dataBlock = curl_exec ($ch);//This is JSON data curl_close ($CH); $res = Json_decode ($dataBlock, true);     Accept a JSON-formatted string and convert it to a PHP variable return $res [' Access_token '];      }//To insert the token into the database function serilizable () {$appid = "your Own";      $appsecret = "Your Own";      $sql = "Select A_id,a_token,a_date from Accesstoken ORDER by a_id Desc";      $rs =mysql_query ($sql);      $times =time ();//Current Time $row =mysql_fetch_array ($RS);      $rownum =mysql_num_rows ($RS); Data time-The current time is less than 800s if ($rownum = = 0) {//If there is no data then get the token, deposit the database $timestamp =time ()+6000;//100 minutes after $token = Curl ($appid, $appsecret);          $sqlin = "INSERT into Accesstoken (a_token,a_date) VALUES (' $token ', ' $timestamp ')";          mysql_query ($sqlin);          return $token;      return $rownum; }else{//time to exceed the data, then regain the token if ($row [' a_date '] < $times) {$token = Curl ($appid, $ap              Psecret); $timestamp =time () +6000;//100 minutes later $sqlu = "UPDATE ' accesstoken ' SET ' a_token ' = ' $token ', ' a_date ' = ' $timestamp ' wher              E a_id = ' $row [a_id] ' ";              mysql_query ($SQLU);          return $token;                }else//not exceed, then take return from the database $row [A_token];   }         }

  

-How to store and update Access_token regularly

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.