PHP code example of smarterqueue / threads-api

1. Go to this page and download the library: Download smarterqueue/threads-api 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/ */

    

smarterqueue / threads-api example snippets


use SmarterQueue\ThreadsApi;
use SmarterQueue\ThreadsOAuthHelper;

// Set up the API instances.
$threadsApi = new ThreadsApi($clientId, $clientSecret);
$threadsOAuthHelper = new ThreadsOAuthHelper($threadsApi);

// Setup CSRF protection.
$state = bin2hex(random_bytes(32));
$_SESSION['threads_state'] = $state;

// Setup scopes and callback.
$scopes = ['threads_basic', 'threads_content_publish'];
$redirectUri = 'https://my-site.com/oauth-callback';

// Generate login URL & redirect to it.
$loginUrl = $threadsOAuthHelper->getLoginUrl($scopes, $redirectUri, $state);
header('Location: ' . $loginUrl);

use SmarterQueue\ThreadsApi;
use SmarterQueue\ThreadsOAuthHelper;

// Set up the API instances.
$threadsApi = new ThreadsApi($clientId, $clientSecret);
$threadsOAuthHelper = new ThreadsOAuthHelper($threadsApi);

// GET params.
$code = $_GET['code'] ?? null;
$state = $_GET['state'] ?? null;

// CSRF Check.
if (!isset($_SESSION['threads_state']) || $state === null || $state !== $_SESSION['threads_state'])
{
	throw new \Exception('CSRF Error - State mismatch');
}

// Exchange authorization code for short-lived token.
$redirectUri = 'https://my-site.com/oauth-callback';
$response = $threadsOAuthHelper->getShortLivedAccessToken($code, $redirectUri);
$shortLivedToken = $response->decodedData['access_token'];

// Exchange short-lived token for long-lived token.
$response = $threadsOAuthHelper->getLongLivedAccessToken($shortLivedToken);
$longLivedToken = $response->decodedData['access_token'];

use SmarterQueue\ThreadsApi;
use SmarterQueue\ThreadsOAuthHelper;

// Set up the API instances.
$threadsApi = new ThreadsApi($clientId, $clientSecret);
$threadsOAuthHelper = new ThreadsOAuthHelper($threadsApi);
$longLivedToken = 'Replace with your token here';
$threadsApi->setAccessToken($longLivedToken);

// Refresh the token.
$response = $threadsOAuthHelper->refreshLongLivedAccessToken($longLivedToken);
$refreshedToken = $response->decodedData['access_token'];

use SmarterQueue\ThreadsApi;
use SmarterQueue\ThreadsOAuthHelper;

// Set up the API instance.
$threadsApi = new ThreadsApi($clientId, $clientSecret);
$longLivedToken = 'Replace with your token here';
$threadsApi->setAccessToken($longLivedToken);

// Get my details
$response = $threadsApi->get('me', ['fields' => 'id,username,threads_profile_picture_url']);

// Publish a post
$containerResponse = $threadsApi->post('me/threads', ['media_type' => 'TEXT', 'text' => "Test post"]);
$threadsApi->post('me/threads_publish', ['creation_id' => $containerResponse->decodedData['id']]);

try {
  $containerResponse = $threadsApi->post('me/threads', ['media_type' => 'TEXT', 'text' => "Test post"]);
} catch (ThreadsApiException $e) {
  $logger->log('Error getting my details', [
    'message' => $e->getMessage(),
    'code' => $e->getCode(),
    'subCode' => $e->subcode,
    'httpCode' => $e->httpCode,
  ]);
}