Wechat GET request method/access_token/access_token storage

Keywords: curl SQL Database PHP

Let's take a look at some of the knowledge points of Wechat Public Number. First of all, today we'll learn how to get access_token.
As we all know, when we learn PHP, we need to go back and forth between the client and the server, which is enough to make people dizzy. Wechat Public Number here, there is another one, not only need the client, their own registered server, but also need to use the Wechat server. Haha, do you feel more overwhelming now? Don't be embarrassed. Pingzi will study with you. Okay, let's start now.
By the way, it is also important to note that the use of Wechat public platform requires cross-domain requests, where the main cross-domain processing is "curl"; then it is also important to note that there are many ways to get requests, in addition to the httpGet () method mentioned below, there are also links.
Pingzi uses the test number as an example.

GET Method Function of Getting Request Interface Encapsulated by Wechat Public Platform

httpGet The functions are as follows:
    function httpGet($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_URL, $url);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }
Get the access_token interface link:
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
 There are parameters in the link above. The uppercase part needs to be modified according to its own test numbers, appid and appsecret.
Here I will give the example code according to my own test number.
$appid = "wx4c9f603761xxxxxxxx";
$secret = "8d176ab5ccc60axxxxxxxxxxxxxxx";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
$res = httpGet($url);
$arr = json_decode($res,true);
echo $arr['access_token'];

With the above code operation and execution, you can get the access_token value. The results are as follows:

This is access_token.

access_token storage

Because access_token has a limited number of accesses per day and its validity period is two hours, according to this feature, we can save the acquired access_token value and request new values when it is about to expire for more than two hours. The advantage of this is that the Wechat server is more advocated and can reduce the pressure on its server.
The general idea is: query the corresponding database, first to see if there is value in the database, to determine whether it is invalid or not, if it is not invalid, it returns, and if it is invalid, it retrieves and stores it; if there is no value, it gets access_token value and stores it in the database.

storage access_token Functions of values:
//Get a correct token value, which is not expired
function getToken($url){
    $link = mysqli_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS,SAE_MYSQL_DB);
    mysqli_query($link,"set names utf8");
    //First check whether the database is stored or not.
    $sql = "SELECT * FROM token";
    $result = mysqli_query($link,$sql);
    if(mysqli_num_rows($result)>0){
        //Valuable
        $arr = mysqli_fetch_assoc($result);
        $token = $arr["token"];
        $time = $arr["time"];
        $newTime = time();
        if($newTime>$time+7000){
            //Out of date
            $res = httpGet($url);
            $arr = json_decode($res,true);
            $token = $arr["access_token"];
            $sql = "UPDATE token SET token='{$token}',time='{$newTime}'";
            mysqli_query($link,$sql);
            if(mysqli_affected_rows($link)>0){
                $sureToken = $token;
            }else{
                echo "Update failed";
            }
        }else{
            //Not expired
            $sureToken = $token;
        }
    }else{
        //No value
        $res = httpGet($url);
        $arr = json_decode($res,true);
        $token = $arr["access_token"];
        $time = time();
        $sql = "INSERT INTO token (id,token,time) VALUES (NULL,'{$token}','{$time}')";
        mysqli_query($link,$sql);
        if(mysqli_insert_id($link)>0){
            $sureToken = $token;
        }else{
            echo "Insert failure";
        }
    }
    return $sureToken;
}

//The address of token that needs to be retrieved initially
$getTokenApi = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
$token = getToken($getTokenApi);

Posted by adige on Wed, 02 Jan 2019 21:03:09 -0800