Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Commit

Permalink
feat(kuaishou): ✨ func
Browse files Browse the repository at this point in the history
* refactor(kernel): 🦄 response

* feat(kuaishou): ✨ app

* feat(kuaishou): ✨ oauth

* feat(kuaishou): ✨ user_info
  • Loading branch information
waset authored Apr 19, 2022
1 parent 9623b9b commit 391e01d
Show file tree
Hide file tree
Showing 12 changed files with 310 additions and 23 deletions.
30 changes: 24 additions & 6 deletions src/Douyin/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ public function item($access_token, $openid, $date_type = 30)
'open_id' => $openid,
"date_type" => 30
];
return $this->https_get($api_url, $params, $headers);

$res = $this->https_get($api_url, $params, $headers)->toArray();

return $res['data'];
}

/**
Expand All @@ -45,7 +48,10 @@ public function fans($access_token, $openid, $date_type = 30)
'open_id' => $openid,
"date_type" => 30
];
return $this->https_get($api_url, $params, $headers);

$res = $this->https_get($api_url, $params, $headers)->toArray();

return $res['data'];
}

/**
Expand All @@ -67,7 +73,10 @@ public function like($access_token, $openid, $date_type = 30)
'open_id' => $openid,
"date_type" => 30
];
return $this->https_get($api_url, $params, $headers);

$res = $this->https_get($api_url, $params, $headers)->toArray();

return $res['data'];
}

/**
Expand All @@ -89,7 +98,10 @@ public function comment($access_token, $openid, $date_type = 30)
'open_id' => $openid,
"date_type" => 30
];
return $this->https_get($api_url, $params, $headers);

$res = $this->https_get($api_url, $params, $headers)->toArray();

return $res['data'];
}

/**
Expand All @@ -111,7 +123,10 @@ public function share($access_token, $openid, $date_type = 30)
'open_id' => $openid,
"date_type" => 30
];
return $this->https_get($api_url, $params, $headers);

$res = $this->https_get($api_url, $params, $headers)->toArray();

return $res['data'];
}

/**
Expand All @@ -133,6 +148,9 @@ public function profile($access_token, $openid, $date_type = 30)
'open_id' => $openid,
"date_type" => 30
];
return $this->https_get($api_url, $params, $headers);

$res = $this->https_get($api_url, $params, $headers)->toArray();

return $res['data'];
}
}
4 changes: 3 additions & 1 deletion src/Douyin/Fans.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public function list(string $access_token, string $openid, int $cursor = 0, int
"count" => $count
];

return $this->https_get($api_url, $params, $headers);
$res = $this->https_get($api_url, $params, $headers)->toArray();

return $res['data'];
}
}
19 changes: 15 additions & 4 deletions src/Douyin/Oauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public function token(string $code)
'grant_type' => 'authorization_code'
];

return $this->https_post($api_url, $params);
$res = $this->https_post($api_url, $params)->toArray();

return $res['data'];
}

/**
Expand All @@ -53,7 +55,10 @@ public function renew_refresh_token(string $refresh_token)
'client_secret' => $this->client_secret,
'refresh_token' => $refresh_token
];
return $this->https_post($api_url, $params);

$res = $this->https_post($api_url, $params)->toArray();

return $res['data'];
}

/**
Expand All @@ -76,7 +81,10 @@ public function refresh_token(string $refresh_token)
'client_secret' => $this->client_secret,
'refresh_token' => $refresh_token
];
return $this->https_post($api_url, $params);

$res = $this->https_post($api_url, $params)->toArray();

return $res['data'];
}

/**
Expand All @@ -99,7 +107,10 @@ public function client_token()
'client_secret' => $this->client_secret,
'grant_type' => 'client_credential'
];
return $this->https_post($api_url, $params);

$res = $this->https_post($api_url, $params)->toArray();

return $res['data'];
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Douyin/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public function info($access_token, $openid)
'access_token' => $access_token,
'open_id' => $openid
];
return $this->https_get($api_url, $params);

$res = $this->https_get($api_url, $params)->toArray();

return $res['data'];
}
}
4 changes: 3 additions & 1 deletion src/Douyin/Video.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public function list(string $access_token, string $openid, int $cursor = 0, int
"count" => $count
];

return $this->https_get($api_url, $params, $headers);
$res = $this->https_get($api_url, $params, $headers)->toArray();

return $res['data'];
}
}
77 changes: 77 additions & 0 deletions src/Kuaishou/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Waset\Kuaishou;

use Waset\Kernel\BaseApi;

/**
* 快手应用入口
*
* @method function Oauth Oauth()
*/
class Application extends BaseApi
{
/**
* 快手接口地址
*/
const BaseUrl = "https://open.kuaishou.com";

/**
* 应用唯一标识
*
* @var array
*/
private array $config;

/**
* 应用唯一标识
*
* @var string
*/
public string $app_id = '';

/**
* 应用唯一标识对应的密钥
*
* @var string
*/
public string $app_secret = '';

/**
* 用户唯一标志
*
* @var string
*/
public string $open_id = '';

/**
* 构造函数
*
* @param array $config
*/
public function __construct(array $config)
{
$this->config = $config;
$this->app_id = $config['app_id'];
$this->app_secret = $config['app_secret'];
$this->open_id = $config['open_id'] ?? '';
}

/**
* 静态魔术加载方法
* @param $name
* @param $arguments
* @return class
*/
public function __call($name, $arguments)
{
$name = ucfirst(strtolower($name));
$class = "\\Waset\\Kuaishou\\{$name}";

if (!empty($class) && class_exists($class)) {
return new $class(array_merge($this->config, $arguments));
} else {
throw new \Exception("{$name} is not exists");
}
}
}
121 changes: 121 additions & 0 deletions src/Kuaishou/Oauth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

namespace Waset\Kuaishou;

class Oauth extends Application
{
/**
* 快手授权码(code)
*
* 该接口只适用于快手获取动态二维码
*
* 该URL不是用来请求的, 需要展示给用户用于扫码,在快手APP支持端内唤醒的版本内打开的话会弹出客户端原生授权页面。
* @see https://open.kuaishou.com/platform/openApi?menu=12
*
* @param array $scope 应用授权作用域,多个授权作用域以英文逗号(,)分隔
* @param string $redirect_uri 授权成功后的回调地址,必须以http/https开头。
* @param string $state 用于保持请求和回调的状态
* @param string $type 返回值类型,可选img和path,选择img会返回二维码图片,选择path会返回二维码内容的地址
* @param boolean $debug 是否开启调试模式,开启后,在用户授权失败时,会在快手APP内显示调试信息,可以根据调试信息来调试.默认false
* @return BaseApi
*/
public function code(array $scope, string $redirect_uri, string $state = "", string $type = "path", bool $debug = false)
{
$api_url = self::BaseUrl . '/oauth2/qr_code';
$params = [
// 应用唯一标识
'app_id' => $this->app_id,
// 写死为'code'即可
'response_type' => 'code',
'scope' => implode(',', $scope),
'redirect_uri' => $redirect_uri,
'type' => $type,
'debug' => $debug
];

if ($state) {
$params['state'] = $state;
}

return $this->https_url($api_url, $params);
}

/**
* 获取access_token
*
* 该接口用于获取用户授权凭证access_token
*
* 该接口适用于快手授权
* @see https://open.kuaishou.com/platform/openApi?menu=13
*
* @param string $code
* @return BaseApi
*/
public function token(string $code)
{
$api_url = self::BaseUrl . '/oauth2/access_token';
$params = [
'app_id' => $this->app_id,
'app_secret' => $this->app_secret,
'code' => $code,
'grant_type' => 'authorization_code'
];

$res = $this->https_get($api_url, $params)->toArray();

return $res;
}

/**
* 刷新refresh_token
*
* 该接口用于刷新refresh_token的有效期
*
* 该接口适用于快手授权
*
* @see https://open.snssdk.com/platform/doc/6848806519174154248
* @param string $refresh_token
*
* @return BaseApi
*/
public function renew_refresh_token(string $refresh_token)
{
$api_url = self::BaseUrl . '/platform/oauth/renew_refresh_token/';
$params = [
'client_key' => $this->client_key,
'client_secret' => $this->client_secret,
'refresh_token' => $refresh_token
];

$res = $this->https_post($api_url, $params)->toArray();

return $res;
}

/**
* 刷新access_token
*
* 该接口用于刷新access_token的有效期
*
* 该接口适用于快手授权
*
* @see https://open.snssdk.com/platform/doc/6848806519174154248
* @param string $refresh_token
*
* @return BaseApi
*/
public function refresh_token(string $refresh_token)
{
$api_url = self::BaseUrl . '/oauth2/refresh_token';
$params = [
'app_id' => $this->app_id,
'app_secret' => $this->app_secret,
'refresh_token' => $refresh_token,
'grant_type' => "refresh_token"
];

$res = $this->https_post($api_url, $params)->toArray();

return $res;
}
}
25 changes: 25 additions & 0 deletions src/Kuaishou/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Waset\Kuaishou;

class User extends Application
{
/**
* 获取用户信息
* @param string $access_token
* @param string $openid
* @return User
*/
public function info($access_token, $openid)
{
$api_url = self::BaseUrl . '/oauth/userinfo/';
$params = [
'access_token' => $access_token,
'open_id' => $openid
];

$res = $this->https_post($api_url, $params)->toArray();

return $res['user_info'] ?? throw new \Exception("快手获取用户失败", 1);;
}
}
Loading

0 comments on commit 391e01d

Please sign in to comment.