Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsky committed Mar 12, 2022
2 parents d01591e + 4f3988c commit 2508d2c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Lib/SnsNetwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions src/Open/UserAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions tests/Test_UnionId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/**
* Created by IntelliJ IDEA.
* User: Max Sky
* Date: 3/12/2022
* Time: 4:45 PM
*/

use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Exception\ClientException;
use PHPUnit\Framework\TestCase;
use Tencent\QQ\Open\UserAPI;

class Test_UnionId extends TestCase {

public function testGetUnionId() {
$appId = '';
$appKey = '';

$accessToken = '';

try {
$result = (new UserAPI($appId, $appKey))->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);
}
}

0 comments on commit 2508d2c

Please sign in to comment.