schnoog/php_atproto

轻量级的PHP ATProto实现

0.0.0.7 2023-11-12 07:57 UTC

This package is auto-updated.

Last update: 2024-09-12 09:54:29 UTC


README

这是一个轻量级的atproto协议实现,以便与Bluesky一起娱乐

为什么?

在调查了与atproto通信的现有库后,我决定走自己的路。我尽量避免使用那些在composer.json中包含了十几个其他库的库,而实际的通信功能只需要更少的第三方组件即可覆盖。

目前(我称之为预alpha版本)我只包含了一个第三方库,即

  • "tcdent/php-restclient"

将来可能还会更多。

V0.0.0.5 备注

为了使一切变得不那么混乱,我将几乎所有函数都重命名了.....您可以在以下位置找到我实现的函数列表

按Endpoint.md排序的已实现函数

按函数名称排序的已实现函数.md

安装

Composer安装

您可以通过执行以下命令使用composer安装库:

composer require schnoog/php_atproto

这将按照常规的供应商目录安装库。现在将文件 /vendor/schnoog/php_atproto/src/config.dist.php 复制到您的安装目录,并将其重命名为 config.php

或者使用以下命令从您的安装目录复制文件自动(如果安装目录中已经存在名为 config.php 的文件,则不会覆盖它。

php -r 'if(!file_exists(__DIR__ . "/config.php")) copy (__DIR__ . "/vendor/schnoog/php_atproto/src/config.dist.php", __DIR__ . "/config.php");'

更改 config.php 的内容并输入您的凭证

包安装

如果您根本未安装composer,则可以使用此方法。

我创建了一个包含运行此所需所有文件的第二个仓库。

从这里下载zip版本: 打包的php_atproto版本

只需解压它,设置 config.php 中的凭证,您就准备好了。是的,真的就这么简单

手动安装

这意味着您要么通过以下方式拥有整个仓库的副本:

或者

  • 从Github下载 zip存档 并将其解压到 ->your install directory<-

在您的目标目录中有文件后,将文件 config.dist.php 复制到您的安装目录,并将其重命名为 config.php

别忘了通过执行 composer install 安装依赖

更改 config.php 的内容并输入您的凭证

我的要求

我(目前)只有少数几个

  • 登录(创建认证令牌)
  • 本地存储令牌
  • 如果我要调用一个函数来在Bluesky上发布帖子,我只需要调用该函数及其参数,其余的应该幕后完成

它是如何工作的

我不是OOP的最大粉丝。我的数字世界中不是所有东西都必须是类。所以我会保持接口纯粹是函数(即使创建帖子这样的封装在类中会更容易一些)

这里已经有的内容

好的,让我们检查一下有什么可用

身份验证

这个小型示例只是对服务器进行身份验证并本地存储令牌。如果可用本地存储的令牌,并且最后的有效性检查是在$config['atproto']['storedsession_validity']秒之前进行的,则将检查后端的有效性(在我的情况下是BlueSky)

这里是一个提醒给自己:在接口函数(post,get timeline...)的开头粘贴if (!atp_session_get())return false;

<?php 
require_once(__DIR__ . "/src/includer.php");


if (!atp_session_get()){
    echo "UNABLE TO ESTABLISH SESSION" . PHP_EOL;
    print_r($config['error']);
}else{
    echo "Session estalished and checked"  . PHP_EOL;
}

发布(到自己的时间线)

是的,这个功能实际上可以发布到自己的时间线。你只需要(在包含includer.php之后)调用该函数。

但这也有限制(就像地球上的任何事物一样)

  • 最多只能上传4张图片(jpg或png),每张图片最多1 000 000字节
  • 要么是Webcard,要么是额外的图片,两者一起不起作用

以下是对主函数atp_create_post参数的一些简要说明

  • 这是你发送的文本

$text = "这是要发布的文本,\n包含提及@develobot.bsky.social和链接https://github.com/schnoog/php_atproto";

  • 一个简单的语言键数组,但你也可以使用null代替

$lang = ['de','en'];

  • 这将使输入的URL可点击,否则它们将保持为简单文本

$add_link_facets = true;

  • 这将放置提及,如果你在文本中提及某人,如果为true,则被提及的人将被告知

$add_mentions_facets = true;

  • 这里我在此脚本的根路径中定义了2张图片,这些图片将被上传。你也可以使用图片的URL。但即使你只使用一张图片,你也必须提供数组中的文件名/URL

$images = [ __DIR__ . "/pic01.jpg" , __DIR__ . "/pic02.jpg"];

  • 这里我们为第二张图片添加了替代文本。如你所见,这需要第一张图片的alt文本为空

$images_alts = [ '', '第二张图片的替代文本'];

  • 这是Webcard的URL

如前所述,你可以有图片或Webcard。如果你定义了两者(并提供了函数),则只会显示Webcard

$website_uri = "https://github.com/schnoog/php_atproto";

  • Webcard上显示的标题。如果我留空,脚本将尝试读取网站定义的标题

$website_title = "我定义的用户标题";

  • 网站的描述。就像$website_title一样,如果留空,脚本将尝试抓取

$website_description = "我定义的描述";

  • 用于Webcard的图片的文件名(或URL)。如果没有提供,抓取脚本将尝试在网站的meta标签中找到它

$website_image = __DIR__ . "/website_image.png";

好的,让我们看看如何进行一些调用

<?php 
require_once(__DIR__ . "/src/includer.php");

/*
Let's define some variables
*/
$text = "This is the text to post, \n containing mentioning @develobot.bsky.social and a link https://github.com/schnoog/php_atproto";
$lang = ['de','en'];
$add_link_facets = true;
$add_mentions_facets = true;
$images = [ __DIR__ . "/pic01.jpg" , __DIR__ . "/pic02.jpg"];  
$images_alts = [ '', 'Alt text for second image']; 
$website_uri  = "https://github.com/schnoog/php_atproto"; 
$website_title = "My user defined title"; 
$website_description = "My user defined description";
$website_image = __DIR__ . "/website_image.png"; 

//The most simple text post - parsing of mentions and links is ENABLED by default
$answer = atp_create_post($text);

//Now a post, just like above, but this time with the 2 defined images attached, and the $lang keys
$answer = atp_create_post($text,$lang,true,true,$images,$images_alts);

//And now a post which includes a webcard, for which we only provide the URL
$answer = atp_create_post($text,$lang,true,true,[],[],$website_uri);

//Why not a post with a full user defined webcard? Own title, description and image
$answer = atp_create_post($text,null,true,true,[],[],$website_uri,$website_title,$website_description,$website_image);

读取自己的时间线

是的,读取自己的时间线(或feed或它被称为什么)也很简单。坦白说,这是最简单的方法

<?php 
require_once(__DIR__ . "/src/includer.php");

//Get 27 entries of the own timeline and print it by DebugOut
$timeline = atp_get_own_timeline(27);

DebugOut($timeline);

搜索帖子

这个函数有一些限制。我目前无法从app.bsky.feed.searchPosts接收任何有效的结果。所以我在从https://search.bsky.social/search/posts中读取

不好,但不管怎样它有效

<?php 
require_once(__DIR__ . "/src/includer.php");

//Search for posts containing "Arduino" and print the result
$answer = atp_search_posts_by_term("Arduino");
DebugOut($answer);


更多信息

我将创建一个包含已实现的函数和端点的列表,并尝试保持其更新。

Implemented_sorted_by_Endpoint.md

Implemented_sorted_by_Funtions.md