PHP code example of jcsp / social-sdk

1. Go to this page and download the library: Download jcsp/social-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jcsp / social-sdk example snippets


// 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);

// 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);

$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(); 

// 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

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

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