I. wechat interface documents
- Navigation: wechat official documentation –> Server –> interface call credentials –> getAccessToken
Specific call please go to the official document to view
Access_token storage and update
It’s in the interface documentation. Please read the interface documentation carefully
access_token
Storage should be retained at least512
Character space;access_token
Is currently valid for2 hours
, need to periodically refresh, repeated acquisition will result in the last acquisitionaccess_token
Failure;- It is recommended that developers use the central control server for unified access and refresh
access_token
, used by other business logic serversaccess_token
All are from the central control server, should not be refreshed separately, otherwise it is easy to cause conflicts, resulting inaccess_token
Coverage affects business; access_token
The validity period is returned byexpires_in
To convey, for now7200 seconds
The central control server needs to refresh in advance according to this effective time. In the refresh process, the central control server can continue to output the oldaccess_token
At this time, the public platform background will be guaranteed in5 minutes
The new and oldaccess_token
Are available, which ensures a smooth transition of third-party services;access_token
The effective time may be adjusted in the future, so the central control server not only needs internal timing active refresh, but also needs to provide passive refreshaccess_token
To make it easy for the business server to learn from the API callaccess_token
Can be triggered if the timeout has expiredaccess_token
Refresh process.
For details, please refer to the document “Obtaining Access_Token” of wechat Public Platform.
PHP generates access_token for wechat applets
1, code,
public function test() {
$accessToken = self::getAccessToken();
echo "access_token is {$accessToken}";
}
public function getAccessToken() {
$url = 'https://api.weixin.qq.com/cgi-bin/token';
$data = [
'grant_type'= >'client_credential'./ / fill in client_credential
'appid'= >'wx523***********'.// Applet unique credentials: AppID 16-bit hexadecimal (lowercase)
'secret'= >'c4ca4238a0************9a6f75849b' // Applets unique credentials: AppSecret 32-bit hexadecimal (lowercase) => similar to MD5 ()
];
$apiUrl = self::getUrl($url.$data);
$result = json_decode(self::curlGet($apiUrl),true);
$accessToken = isset($result['access_token'])?$result['access_token'] : ' ';
if (!$accessToken) {
die(json_encode($result, JSON_UNESCAPED_UNICODE)); // Output error
}
return $accessToken;
}
/ / url generator
public function getUrl($apiUrl.$param = []){
$param = http_build_query($param);
return $apiUrl . '? ' . $param;
}
public function curlGet($url.$header = []) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPGET, true);
if ($header) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // Set the timeout period to 30s
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // Ignore SSL detection
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //1 or TRUE returns information retrieved by curl_exec() as a string rather than output it directly. -
curl_setopt($ch, CURLINFO_HEADER_OUT, true); // trace the request string for the handle when TRUE, available as of PHP 5.1.3. And this is key, is to allow you to see the request header
$output = curl_exec($ch);
if (!$output) {
// echo "request $url fail:", (array)curl_error($ch); // Log
}
curl_close($ch);
// echo "request $url success:" . json_encode(array($url, $header, $output), true); // Log
return $output;
}
Copy the code
2, print,
- Due to the
appid
.secret
Not true and valid, so print it below
{"errcode":40013,"errmsg":"invalid appid rid: 609deb67-3288bef4-69fe5dee"}
Copy the code
- Return to normal
{"access_token":"ACCESS_TOKEN","expires_in":7200}
Copy the code