PHP timed task Gets the example of micro-letter Access_token

Source: Internet
Author: User

The recent development of the micro-trust public platform, the public number of calls to each interface need to use Access_token,access_token is the public number of the global unique interface call credentials, development needs to be properly saved. Access_token is valid for 7,200 seconds, and repeated fetching will cause the last acquired access_token to fail.
Because of the limitation of the number of API calls to obtain Access_token, it is recommended that developers global storage and update Access_token, frequent refresh Access_token will lead to limited API calls, affecting their own business.
So what's the best way to solve Access_token storage and refresh? My approach is to schedule a task to refresh 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 a access.php 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 files, save token
$dir = __dir__; True path, crontab command PHP executes in CLI mode, the relative path is not recognized correctly, 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;
}

Generating files
function Create_file ($filename, $content) {
$fp = fopen ($filename, "w");
Fwrite ($FP, "". $content);
Fclose ($FP);
}
These two constants, AppID and Appsecret, are provided by the micro-trust public platform and can be logged into the basic configuration of the micro-credit public platform. The acquired Access_token is saved to the file access_token.php, noting that the contents of this file are not accessed by the user.
Next, we set the timer task, we use the Linux CentOS as an example, set the scheduled task with Crontab.
5 * * * */usr/local/bin/php-f/home/web/access.php >>/dev/null 2>&1
The above command sets the access.php to execute every 1 hours, that is, the first 05 minutes of every hour.
In this way, we can guarantee the normal acquisition and use of Access_token, not frequent to refresh the micro-trust server.

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.