SoFunction
Updated on 2025-03-08

Method for obtaining WeChat access_token by PHP timed tasks

This article describes the method of obtaining WeChat access_token by PHP timed tasks. Share it for your reference, as follows:

WeChat access_token will change slightly during development. Here we will introduce the method of obtaining WeChat access_token for PHP timed tasks.

Recently, the WeChat public platform was developed. When the official account calls each interface, access_token is the globally unique interface calling credential of the official account and needs to be properly saved during development. The validity period of access_token is 7200 seconds. Repeated acquisition will cause the access_token you obtained last time to be invalid.

Since WeChat has restricted the number of API calls to obtain access_token, it is recommended that developers store and update access_token globally. Frequent refresh of access_token will lead to API calls being restricted and affect their own business.

So what are the good solutions to access_token storage and refresh? My method is: schedule the task to refresh regularly to get access_token, and then save the access_token to the server local, which can be saved in a file, database or cache.

Below I use PHP to get access_token and save it to a local file. Create one, the code is as follows:

$url = "/cgi-bin/token?grant_type=client_credential&app&secret=".AppSecret;
$result = http_request($url);
// Generate file and save token$dir = __DIR__; //Real path, the php execution of the crontab command is in cli mode, and the relative path cannot be correctly identified, so use __DIR__$filename = $dir."/access_token.php";
create_file($filename, $result);
function http_request($url,$data = null){
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  if (!empty($data)){
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  }
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  $output = curl_exec($curl);
  curl_close($curl);
  return $output;
}
// Generate filefunction create_file($filename, $content){
  $fp = fopen($filename, "w");
  fwrite($fp, "" . $content);
  fclose($fp);
}

In the above code, the two constants AppID and AppSecret are provided by the WeChat public platform and can be obtained by logging in to the basic configuration of the WeChat public platform. The access_token obtained is saved to the file access_token.php. Note that the content of this file should not be accessed by the user.

Next, we set up timed tasks. We use CentOS on Linux as an example to set up scheduled tasks using crontab.

5 * * * * /usr/local/bin/php -f /home/web/ >> /dev/null 2>&1

The above command is set to be executed every 1 hour, that is, the 05th minute of each hour.

In this way, we can ensure the normal acquisition and use of access_token, and we do not need to refresh the WeChat server frequently.

For more information about PHP related content, please check out the topic of this site:Summary of PHP WeChat development skills》、《Summary of PHP encoding and transcoding operation techniques》、《Summary of PHP network programming skills》、《Introduction to PHP basic syntax》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope this article will be helpful to everyone's PHP programming.