From 4f3988c793644e45dca7dd0bb44dc4aa9993c182 Mon Sep 17 00:00:00 2001 From: Max Sky Date: Sat, 12 Mar 2022 16:51:13 +0800 Subject: [PATCH] add get unionid method --- src/Lib/SnsNetwork.php | 2 +- src/Open/UserAPI.php | 17 +++++++++++++++ tests/Test_UnionId.php | 49 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tests/Test_UnionId.php diff --git a/src/Lib/SnsNetwork.php b/src/Lib/SnsNetwork.php index 5aab2d5..fe208cf 100755 --- a/src/Lib/SnsNetwork.php +++ b/src/Lib/SnsNetwork.php @@ -29,7 +29,7 @@ class SnsNetwork { * @return array * @throws GuzzleException */ - public static function makeRequest(string $url, string $method, array $params, array $headers): array { + public static function makeRequest(string $url, string $method, array $params, array $headers = []): array { if (strtoupper($method) === 'GET') { $options = [ 'headers' => $headers, diff --git a/src/Open/UserAPI.php b/src/Open/UserAPI.php index cfdef99..da1139a 100644 --- a/src/Open/UserAPI.php +++ b/src/Open/UserAPI.php @@ -13,9 +13,26 @@ use Tencent\QQ\Exceptions\QQAPIRouteException; use Tencent\QQ\Exceptions\QQOpenIDException; use Tencent\QQ\Exceptions\QQResponseException; +use Tencent\QQ\Lib\SnsNetwork; class UserAPI extends OpenAPIv3 { + /** + * @param string $access_token + * @param int $union_id + * @param string $fmt + * + * @return array + * @throws GuzzleException + */ + public function getUnionId(string $access_token, int $union_id = 1, string $fmt = 'json'): array { + return SnsNetwork::makeRequest('https://graph.qq.com/oauth2.0/me', 'GET', [ + 'access_token' => $access_token, + 'unionid' => $union_id, + 'fmt' => $fmt + ]); + } + /** * @param string $access_token * @param string $open_id diff --git a/tests/Test_UnionId.php b/tests/Test_UnionId.php new file mode 100644 index 0000000..0ffe60d --- /dev/null +++ b/tests/Test_UnionId.php @@ -0,0 +1,49 @@ +getUnionId($accessToken); + } catch (Throwable $e) { + print_r('Error Message: ' . $e->getMessage()); + print_r('Error Code: ' . $e->getCode()); + + if ($e instanceof ClientException) { + print_r($e->getResponse()); + } elseif ($e instanceof BadResponseException) { + print_r(json_decode($e->getResponse()->getBody(), true)); + } + + die; + } + + /** + * [ + * 'client_id' => '123456789', + * 'openid' => 'E10ADC3949BA59ABBE56E057F20F883E', + * 'unionid' => 'UID_E10ADC3949BA59ABBE56E057F20F883E' + * ] + */ + print_r($result); + + $this->assertTrue($result['unionid'] ?? null); + } +}