12345678910111213141516171819202122232425262728 |
- <?php
- namespace App\Service\Miniprogram\Wechat;
- use GuzzleHttp\Client;
- class AccessTokenService
- {
- public static function getAccessToken($appid, $secret) {
- $client = new Client(['timeout' => 3]);
- $httpResult = $client->get('https://api.weixin.qq.com/cgi-bin/token', [
- 'query' => ['grant_type' => 'client_credential', 'appid' => $appid, 'secret' => $secret]
- ]);
- if(200 != $httpResult->getStatusCode()) {
- throw new \RuntimeException('请求微信上游失败', '500001');
- }
- $parsedContent = \json_decode($httpResult->getBody()->getContents(), true);
- if(0 != ($parsedContent['errcode'] ?? 0)) {
- throw new \RuntimeException('请求微信上游失败:'. ($parsedContent['errmsg'] ?? ''), '500002');
- }
- return $parsedContent;
- }
- public static function getAccessTokenRedisKey($appid) {
- return 'dj.miniWechat.at.' . $appid;
- }
- }
|