Files
kiri-wchat/wchat/wx/Token.php
T

39 lines
868 B
PHP
Raw Normal View History

2019-10-25 15:20:09 +08:00
<?php
2020-03-05 12:41:49 +08:00
namespace wchat\wx;
2019-10-25 15:20:09 +08:00
2022-09-09 16:42:55 +08:00
use Kiri\Client;
use wchat\common\Result;
2020-09-10 17:25:46 +08:00
2022-09-09 16:42:55 +08:00
class Token extends SmallProgram
2019-10-25 15:20:09 +08:00
{
2022-09-09 16:42:55 +08:00
/**
* @return Result
*/
public function token(): Result
{
$query = [
'grant_type' => 'client_credential',
'appid' => $this->config->getAppid(),
'secret' => $this->config->getAppsecret()
];
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$client->get('cgi-bin/token', $query);
$client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
2023-08-18 19:59:46 +08:00
return new Result(code: 505, message: $client->getBody());
2022-09-09 16:42:55 +08:00
}
$body = json_decode($client->getBody(), true);
if (isset($body['errcode']) && $body['errcode'] != 0) {
return new Result(code: $body['errcode'], message: $body['errmsg']);
}
return new Result(code: 0, data: $body);
}
2019-11-11 18:14:47 +08:00
2019-10-25 15:20:09 +08:00
}