smarterqueue/threads-api
Composer 安装命令:
composer require smarterqueue/threads-api
包简介
Provides a wrapper around Meta's Threads OAuth and API
README 文档
README
This provides a PHP wrapper around Meta's Threads API.
Usage
There are 2 main API service classes - the ThreadsApi and ThreadsOAuthHelper
ThreadsApi- The base class for sending requests and parsing the response & handling errorsThreadsOAuthHelper- A helper class with methods to:- Build a login URL
- Exchange the authorization code for a short-lived token
- Exchange a short-lived token for a long-lived token
- Refresh a long-lived token
On successful responses, the API services will return a ThreadsResponse object that will contain
the http code, headers, and decoded data.
On unsuccessful requests or PHP exceptions, the API services will throw a ThreadsException object that will contain
the message, http code, status code (if any), sub code (if any), error type (if any), fb trace id (if any),
and the previous exception (if any).
Create a link to the login page
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);
Exchange code for access tokens
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'];
Refresh the long-lived 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'];
General requests
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']]);
Handling errors
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, ]); }
By SmarterQueue
smarterqueue/threads-api 适用场景与选型建议
smarterqueue/threads-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 598 次下载、GitHub Stars 达 4, 最近一次更新时间为 2024 年 07 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 smarterqueue/threads-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 smarterqueue/threads-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 598
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-07-16