shahmal1yev/blueskysdk

BlueSky SDK 是一个用于与 BlueSky API 交互的 PHP 库。此库允许您使用 BlueSky API 执行各种操作。

v1.3.0-alpha1 2024-09-18 14:53 UTC

This package is auto-updated.

Last update: 2024-09-29 13:58:12 UTC


README

Logo

BlueSky SDK

GitHub tag (latest by date) Packagist Downloads License: MIT GitHub last commit GitHub issues GitHub stars GitHub forks GitHub contributors

项目描述

BlueSky SDK 是一个用于与 BlueSky API 交互的 PHP 库。此库允许您使用 BlueSky API 执行各种操作。

要求

安装

要通过 Composer 安装 BlueSky SDK,请使用以下命令

composer require shahmal1yev/blueskysdk

使用方法

安装完成后,您可以使用 SDK 与 BlueSky API 交互。以下是如何使用库进行身份验证和执行各种操作的示例。

身份验证和基本使用

首先,实例化 Client 类,并使用您的 BlueSky 凭据进行身份验证

use Atproto\Client;
use Atproto\Resources\Com\Atproto\Server\CreateSessionResource;

$client = new Client();

// Authenticate using your identifier (e.g., email) and password
$client->authenticate($identifier, $password);

// Once authenticated, you can retrieve the user's session resource
/** @var CreateSessionResource $session */
$session = $client->authenticated();

发送请求

BlueSky SDK 提供了一个流畅的界面来构建 API 请求。使用链式方法调用来遍历 API 词汇表并构建请求

use Atproto\Contracts\ResourceContract;

// Example: Fetching a profile
$profile = $client->app()
                  ->bsky()
                  ->actor()
                  ->getProfile()
                  ->forge()
                  ->actor('some-actor-handle') // Specify the actor handle
                  ->send();

处理响应

BlueSky SDK 支持资源和可铸接口,提供了处理 API 响应的灵活性,并使数据操作和铸造更为流畅,从而提高开发体验。

响应作为实现了 ResourceContract 的资源实例返回。这些资源提供了访问 API 返回的数据的方法。

// Retrieve properties from the profile
/** @var string $displayName */
$displayName = $profile->displayName();

/** @var Carbon\Carbon $createdAt */
$createdAt = $profile->createdAt();

处理资产和关系

BlueSky SDK 允许您通过资源实例直接访问复杂的资产,如关注者和标签。

use Atproto\Resources\Assets\FollowersAsset;
use Atproto\Resources\Assets\FollowerAsset;

// Fetch the user's followers
/** @var FollowersAsset<FollowerAsset> $followers */
$followers = $profile->viewer()
                     ->knownFollowers()
                     ->followers();

foreach ($followers as $follower) {
    /** @var FollowerAsset $follower */
    echo $follower->displayName() . " - Created at: " . $follower->createdAt()->format(DATE_ATOM) . "\n";
}

示例:获取配置文件信息

以下是一个更完整的示例,用于获取并显示配置文件信息,包括创建日期和标签

use Atproto\Client;
use Atproto\API\App\Bsky\Actor\GetProfile;
use Atproto\Resources\App\Bsky\Actor\GetProfileResource;

$client->authenticate('user@example.com', 'password');

$client->app()
       ->bsky()
       ->actor()
       ->getProfile()
       ->forge();
       // ->actor($client->authenticated()->did());

/** @var GetProfileResource $user */
$user = $client->send();

// Output profile details
echo "Display Name: " . $user->displayName() . "\n";
echo "Created At: " . $user->createdAt()->toDateTimeString() . "\n";

// Accessing and iterating over followers
$followers = $user->viewer()->knownFollowers()->followers();

foreach ($followers as $follower) {
    echo $follower->displayName() . " followed on " . $follower->createdAt()->format(DATE_ATOM) . "\n";
}

扩展 SDK

BlueSky SDK 考虑了可扩展性。您可以通过扩展现有类并创建自己的请求和资源类型来添加自定义功能。遵循 SDK 中使用的结构以保持一致性。

贡献

我们欢迎社区贡献!如果您发现任何错误或希望添加新功能,请随时

  • 打开一个问题:报告错误、请求功能或提出改进建议。
  • 提交一个拉取请求:欢迎对代码库的贡献。请遵循最佳实践,并确保您的代码符合现有的架构和编码标准。

许可证

BlueSky SDK 根据 MIT 许可证授权。有关详细信息,请参阅 LICENSE 文件。