pbergman / ntfy-php
There is no license information available for the latest version (1.2.0) of this package.
Library to push messages to a ntfy server
1.2.0
2023-10-23 08:00 UTC
Requires
- php: ^7.4||^8.0
- psr/log: ^1|^2
- symfony/cache-contracts: ^1.1||^2.4||^3.0
- symfony/http-client-contracts: ^1.1||^2.4||^3.0
Requires (Dev)
- monolog/monolog: ^1|^2
- symfony/cache: ^4.4||^5.0||^6.0
- symfony/http-client: ^5.2||^6.0
README
A php library for publishing and subscribing messages to or from a ntfy server.
Client
use PBergman\Ntfy\Api\Client;
use Symfony\Component\HttpClient\HttpClient;
$client = new Client(
HttpClient::create([
'base_uri' => 'https://ntfy.sh',
]),
);
For authentication the auth_basic
or auth_bearer
can be used or use a AuthenticationInterface as second argument:
use PBergman\Ntfy\Api\Client;
use PBergman\Ntfy\Authentication\BasicAuthentication;
use Symfony\Component\HttpClient\HttpClient;
$client = new Client(
HttpClient::create([
'base_uri' => 'https://ntfy.sh',
]),
new BasicAuthentication('username', 'password')
);
Publishing
Simple message
use PBergman\Ntfy\Model\PublishParameters;
use PBergman\Ntfy\Model\HttpAction;
$message = new PublishParameters('Hello world test!!!', 'Test title');
$message->setTags(['foo', 'bar']);
$message->addAction(new HttpAction('Google', 'https://google.nl'));
$response = $client->publish('test', $message);
// will return a async response...
$response()->getId()
with Attachment
use PBergman\Ntfy\Model\PublishParameters;
use PBergman\Ntfy\Model\HttpAction;
if (false !== $body = file_get_contents('out.txt')) {
$message = new PublishParameters('Hello world test!!!', 'Test title');
$message->setFilename('out.txt')
$message->setTags(['foo', 'bar']);
$message->addAction(new HttpAction('Google', 'https://google.nl'));
$client->publish('test', $message, $body);
}
Subscribing
use PBergman\Ntfy\Model\SubscribeParameters;
$params = new SubscribeParameters();
$params->setSince('all');
foreach ($client->subscribe('test', $params) as $response) {
printf("[%s] %s | %s\n", (new \DateTime('@' . $response->getTime()))->format(\DateTime::ATOM), $response->getTitle(), $response->getMessage());
}
Or to retrieve all message after a given id
use PBergman\Ntfy\Model\SubscribeParameters;
$params = new SubscribeParameters();
$params->setSince('XXXXXXXXXX');
$params->setPoll(true);
$messages = \iterator_to_array($client->subscribe('test', $params));