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

This package is auto-updated.

Last update: 2024-10-23 09:59:45 UTC


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));