add clear
This commit is contained in:
+26
-2
@@ -4,11 +4,35 @@
|
||||
namespace wchat\wx;
|
||||
|
||||
|
||||
use wchat\base\Access_Token;
|
||||
use Kiri\Client;
|
||||
use wchat\common\Result;
|
||||
|
||||
class Token extends SmallProgram implements Access_Token
|
||||
class Token extends SmallProgram
|
||||
{
|
||||
|
||||
/**
|
||||
* @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])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user