The method of obtaining WeChat Access_token by PHP timer task

Source: Internet
Author: User
Tags php programming
In this paper, we describe the method of PHP timing task acquiring Access_token. Share to everyone for your reference, as follows:

Access_token in the development of the change seems to be a few different, here we introduce the PHP timing task to get Access_token method.

Recently developed public platform, public number call 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. The Access_token is valid for 7,200 seconds, and repeated fetches will result in the last acquired Access_token failure.

Since the number of API calls to get Access_token is limited, it is recommended that developers global storage and update Access_token, frequent refresh Access_token will cause API calls to be restricted, affecting their own business.

So what's the best way to solve Access_token storage and refresh? My approach is to schedule a scheduled task refresh to get Access_token, and then save Access_token to the server locally, in a file, database, or cache.

Next 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 file, save Token$dir = __dir__; The real path, crontab command PHP execution in CLI mode, does not correctly identify the relative path, 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 File function Create_file ($filename, $content) {$fp = fopen ($filename, "w"); Fwrite ($FP, "".  $content); Fclose ($FP);} 

The two constants of AppID and Appsecret in the above code are provided by the public platform and can be obtained by logging into the basic configuration of the public platform. The obtained access_token is saved to the file access_token.php, note that the contents of this file should not be accessed by the user.

Next, we set up timed tasks, and we use Crontab to set up scheduled tasks using the CentOS for Linux example.

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

The above command is set to execute access.php every 1 hours, that is, 05 minutes per hour.

In this way, we can guarantee the normal acquisition and use of Access_token, not frequent to refresh the server.

I hope this article is helpful to you in PHP programming.

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.