jcsp/social-sdk

社交API SDK

v1.2.7 2021-08-19 01:38 UTC

This package is auto-updated.

Last update: 2024-09-19 09:17:27 UTC


README

Jscp 社交 SDK 组件

安装

使用 composer 在项目根目录中安装此包。

composer require jscp/social-sdk

请注意,此库需要至少安装 PHP 7.2。

用法

初始化客户端实例

使用以下代码初始化客户端实例。

// Set the client config
$config = [
    // The directory to store temporary files while sharing the resources. 
    // The default value is /tmp .
    'temp_storage_path' => "/tmp/temp_file", 
    
    // A full class name or an instance, it must implement the contract \Jcsp\SocialSdk\Contract\CacheInterface . 
    // The default value is \Jcsp\SocialSdk\Cache\Session::class .
    'cache' => CustomCache::class,
    
    // A full class name or an instance, it must implement the contract \Jcsp\SocialSdk\Contract\LoggerInterface. 
    // The default value is \Jcsp\SocialSdk\Log\NoLog::class .
    'logger' => CustomLogger::class;
];

// Set the real social media name
$socialMediaName = "Youtube";

// Set the auth config
$authConfig = new \Jcsp\SocialSdk\Model\AuthConfig();
$authConfig->setClientId("Client id");
$authConfig->setClientSecret("Client secret");
$authConfig->setRedirectUrl("Oauth callback url");

// Create client instance
$factory = new \Jcsp\SocialSdk\ClientFactory($config);
$client = $factory->createClient($socialMediaName);

// Init auth data
$client->initAuth($authConfig);

一些函数需要提供访问令牌数据,因此必须传递 $accessToken 对象。

// Set the access token object
$accessToken = new \Jcsp\SocialSdk\Model\AccessToken();
$accessToken->setToken("Access token string");
$accessToken->setTokenSecret("Access token secret string");
$accessToken->setUserId("User id"); // optional, only used for some platforms
$accessToken->setRefreshToken("Refresh token string"); // optional, part of platforms support
$accessToken->setExpireTime($expireTimestramp); // optional, part of platforms support
$accessToken->setParams([]); // optional, for part of platforms

// Create client instance
$factory = new \Jcsp\SocialSdk\ClientFactory($config);
$client = $factory->createClient($socialMediaName);

// Init auth
$client->initAuth($authConfig, $accessToken);

授权

生成授权URL

$authUrl = $client->generateAuthUrl();

获取访问令牌

$accessToken = $client->getAccessToken(array_merge($_GET, $_POST));

刷新访问令牌

$allowRefreshToken = $client->allowRefreshToken();
$isAccessTokenExpired = $client->isAccessTokenExpired();
if $allowRefreshToken && $isAccessTokenExpired {
    $accessToken = $client->refreshAccessToken();
}

用户

获取用户资料

// Need access token
$userProfile = $client->getUserProfile(); 

渠道

不同的平台对于描述内容分享区域的名称不同,如YouTube中的频道、Facebook中的页面、Pinterest中的板等。为了方便,这里使用“频道”一词来统一名称。

获取渠道列表

// Need access token
$channelList = $client->getShareChannelList();

分享

可以分享给用户

$canShareToUser = $client->canShareToUser();

可以分享到渠道

$canShareToChannel = $client->canShareToChannel();

分享视频

// Need access token

// If the access token has expired, try to refresh it.
if ($client->allowRefreshToken() && $client->isAccessTokenExpired()) {
    $client->refreshAccessToken();
}

// Share video
$params = new \Jcsp\SocialSdk\Model\VideoShareParams();
$params->setTitle("Share Title");
$params->setDescription("Share Description");
$params->setVideoUrl("Video Url");
$params->setThumbnailUrl("Thumbnail Url");
$params->setSocialId("User Id or Channel Id");
$params->setDisplayName("Username or Channel Name"); // Some platforms need social id, other use social display name.
$params->setAccessToken("Access Token String");
$params->setIsPostToChannel(false); // true: post to channel, false: post to user
$result = $client->shareVideo($params);

模拟分享

使用其他方式分享内容。

创建模拟客户端

// Set the client config
$config = [
     // Specify these endpoint urls
    'simulate' => [
        'post_video_endpoint' => '',
        'query_post_task_endpoint' => '',
        'get_account_list_endpoint' => '',
    ],
    
    // A full class name or an instance, it must implement the contract \Jcsp\SocialSdk\Contract\LoggerInterface. 
    // The default value is \Jcsp\SocialSdk\Log\NoLog::class .
    'logger' => CustomLogger::class;
];

// Create client instance
$factory = new \Jcsp\SocialSdk\ClientFactory($this->config);
$client = $factory->createSimulateClient();

获取账户列表

获取活动账户列表。

$accountList = $client->getAccountList();

创建模拟视频帖子

要创建模拟视频帖子,实际上模拟服务器会生成一个任务并返回它。如果方法返回了任务,则保存该任务。

// Make a task
$params = new \Jcsp\SocialSdk\Model\SimulateVideoPostParams();
$params->setVideoUrl("Video Url");
$params->setTitle("Title");
$params->setDescription("Description");
$params->setAccount("Account");
$params->setCallbackUrl("Public Url to handle Post Result Notice");
$params->setSocialMediaName("Full Platform Name, like Youtube");
$task = $client->simPostVideo($params);

// Get task info
$taskId = $task->getTaskId(); // Important, save it.
$taskStatus = $task->getTaskStatus();
$msg = $task->getMsg(); // Message for develop
$info = $task->getInfo(); // Message for operation staff

处理模拟帖子回调

模拟帖子完成后,模拟服务器会调用帖子类型的回调URL,通知客户端服务器关于帖子任务的结果。

$requestParams = array_merge($_GET, $_POST);
$task = $client->handleSimPostCallback($requestParams);

查询任务信息

难以保证回调处理程序始终稳定运行,因此这里提供了一种手动查询任务信息的方法。

$result = $client->queryTaskInfo("Task Id");

任务状态

查看类 \Jcsp\SocialSdk\ModelSimulatePostTask

授权协议

该组件是开源软件,授权协议为Apache授权协议