From f070dd076a4137ff4e86600b63d1ccfe2f744a92 Mon Sep 17 00:00:00 2001 From: Anyon Date: Mon, 20 Jul 2020 14:55:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9json=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- We.php | 2 +- WeChat/Contracts/BasicWeChat.php | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/We.php b/We.php index 26865b6..846476c 100644 --- a/We.php +++ b/We.php @@ -86,7 +86,7 @@ class We * 定义当前版本 * @var string */ - const VERSION = '1.2.22'; + const VERSION = '1.2.23'; /** * 静态配置 diff --git a/WeChat/Contracts/BasicWeChat.php b/WeChat/Contracts/BasicWeChat.php index af4337b..403a62c 100644 --- a/WeChat/Contracts/BasicWeChat.php +++ b/WeChat/Contracts/BasicWeChat.php @@ -94,7 +94,7 @@ class BasicWeChat } /** - * 获取访问accessToken + * 获取访问 AccessToken * @return string * @throws \WeChat\Exceptions\InvalidResponseException * @throws \WeChat\Exceptions\LocalCacheException @@ -166,15 +166,15 @@ class BasicWeChat { try { return Tools::json2arr(Tools::get($url)); - } catch (InvalidResponseException $e) { + } catch (InvalidResponseException $exception) { if (isset($this->currentMethod['method']) && empty($this->isTry)) { - if (in_array($e->getCode(), ['40014', '40001', '41001', '42001'])) { + if (in_array($exception->getCode(), ['40014', '40001', '41001', '42001'])) { $this->delAccessToken(); $this->isTry = true; return call_user_func_array([$this, $this->currentMethod['method']], $this->currentMethod['arguments']); } } - throw new InvalidResponseException($e->getMessage(), $e->getCode()); + throw new InvalidResponseException($exception->getMessage(), $exception->getCode()); } } @@ -190,13 +190,15 @@ class BasicWeChat protected function httpPostForJson($url, array $data, $buildToJson = true) { try { - return Tools::json2arr(Tools::post($url, $buildToJson ? Tools::arr2json($data) : $data)); - } catch (InvalidResponseException $e) { - if (!$this->isTry && in_array($e->getCode(), ['40014', '40001', '41001', '42001'])) { + $options = []; + if ($buildToJson) $options['headers'] = ['Content-Type: application/json']; + return Tools::json2arr(Tools::post($url, $buildToJson ? Tools::arr2json($data) : $data, $options)); + } catch (InvalidResponseException $exception) { + if (!$this->isTry && in_array($exception->getCode(), ['40014', '40001', '41001', '42001'])) { [$this->delAccessToken(), $this->isTry = true]; return call_user_func_array([$this, $this->currentMethod['method']], $this->currentMethod['arguments']); } - throw new InvalidResponseException($e->getMessage(), $e->getCode()); + throw new InvalidResponseException($exception->getMessage(), $exception->getCode()); } }