How to obtain WeChat access_token for PHP scheduled tasks

Source: Internet
Author: User
This article describes how to obtain access_token from a PHP scheduled task, and describes how to use php to dynamically obtain access_token based on curl and set a scheduled task using crontab in CentOS, for more information about how to obtain the access_token of a PHP scheduled task, see the example in this article. We will share this with you for your reference. The details are as follows:

Access_token may change in development. here we will introduce how to obtain access_token from PHP scheduled tasks.

Recently, on the public platform, access_token is required when the public account calls all interfaces. access_token is the global unique interface of the public account to call creden。. it must be properly saved during development. The validity period of access_token is 7200 seconds. repeated access_token acquisition will invalidate the previous access_token.

Due to restrictions on the number of api calls to obtain access_token, we recommend that you store and update access_token globally. refreshing access_token frequently will restrict api calls and affect your own business.

So what are the solutions to store and refresh access_token? My solution is to regularly refresh the scheduled task to obtain the access_token and save the access_token to the local server. the storage method can be File, database, or cache.

Next I will use PHP to get access_token and save it to a local file. Create an access. php file with the following code:

$ Url = "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = ". appID. "& secret = ". appSecret; $ result = http_request ($ url); // Generate a file and save token $ dir = _ DIR __; // The actual path, the php command for crontab is executed in cli mode and cannot correctly identify the relative path. Therefore, 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, expires, 1 ); $ output = curl_exec ($ curl); curl_close ($ curl); return $ output;} // Generate the file function create_file ($ filename, $ content) {$ fp = fopen ($ filename, "w"); fwrite ($ fp ,"". $ content); fclose ($ fp );}

In the above code, the AppID and AppSecret constants are provided by the public platform and can be obtained by logging on to the basic configuration of the public platform. The obtained access_token is saved to the file access_token.php. Note that the content of this file cannot be accessed by users.

Next, we will set a scheduled task. taking CentOS in Linux as an example, we will use crontab to set a scheduled task.

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

The preceding command sets access. php to be executed every 1 hour, that is, 05th minutes every hour.

In this way, we can ensure that the access_token can be normally obtained and used without refreshing the server frequently.

I hope this article will help you with PHP programming.

For more articles about how to obtain the access_token for PHP scheduled tasks, refer to the PHP Chinese website!

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.