1. Go to this page and download the library: Download rikudou/lemmy-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/ */
rikudou / lemmy-api example snippets
use Rikudou\LemmyApi\DefaultLemmyApi;
use Rikudou\LemmyApi\Enum\LemmyApiVersion;
use Symfony\Component\HttpClient\Psr18Client;
use Nyholm\Psr7\Factory\Psr17Factory;
use Rikudou\LemmyApi\Enum\Language;
use Rikudou\LemmyApi\Exception\LemmyApiException;
$api = new DefaultLemmyApi(
instanceUrl: 'https://my-lemmy-instance.world',
version: LemmyApiVersion::Version3, // this is currently the only supported version
httpClient: new Psr18Client(), // assuming you use Symfony, otherwise provide any other implementation
requestFactory: new Psr17Factory(), // assuming you use Nyholm, otherwise provide any other implementation,
);
// before calling anything else, you must login
$response = $api->login('my_username_or_email', 'myPassword');
// now you can call any other methods, you don't even have to store the $response result, you are logged in automatically
try {
$community = $api->community()->get('some_cool_community');
$result = $api->post()->create($community, 'Post Name', body: 'Some content', language: Language::English);
} catch (LemmyApiException $e) {
// todo handle exception - all exceptions implement the LemmyApiException interface
}
use Rikudou\LemmyApi\DefaultLemmyApi;
use Rikudou\LemmyApi\Enum\LemmyApiVersion;
use Symfony\Component\HttpClient\Psr18Client;
use Nyholm\Psr7\Factory\Psr17Factory;
use Rikudou\LemmyApi\Enum\Language;
use Rikudou\LemmyApi\Exception\LemmyApiException;
$api = new DefaultLemmyApi(
instanceUrl: 'https://my-lemmy-instance.world',
version: LemmyApiVersion::Version3,
httpClient: new Psr18Client(),
requestFactory: new Psr17Factory(),
strictDeserialization: false, // here you disable the strict mode
);
use Rikudou\LemmyApi\Enum\Language;
use Rikudou\LemmyApi\Exception\LemmyApiException;
use Rikudou\LemmyApi\LemmyApi;
final readonly class RemindMeBot
{
public function __construct(
private LemmyApi $api,
// how long to sleep between each loop
private int $sleepFor,
private string $username,
#[SensitiveParameter] private string $password,
) {
}
public function loop(): void
{
$this->login();
while (true) {
try {
$unreadMentions = $this->api->currentUser()->getMentions(
unreadOnly: true,
);
foreach ($unreadMentions as $mention) {
$author = $mention->creator->id;
$message = $mention->comment->content;
$dateTime = $this->getDateFromMessage($message);
$this->storeReminderInDatabase($author, $dateTime);
$this->api->currentUser()->markMentionAsRead($mention->personMention);
$this->api->comment()->create(
post: $mention->post,
content: "Sure, I'll let you know!",
language: Language::English,
parent: $mention->comment,
);
}
} catch (LemmyApiException $e) {
$this->logException($e);
}
sleep($this->sleepFor);
}
}
private function login(): void
{
$this->api->login($this->username, $this->password);
}
private function getDateFromMessage(string $message): DateTimeInterface
{
// todo implement this yourself
}
private function storeReminderInDatabase(int $userId, DateTimeInterface $dateTime): void
{
// todo implement this yourself
}
private function logException(LemmyApiException $e): void
{
// todo implement this yourself
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.