1. Go to this page and download the library: Download innis/nostr-client 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/ */
innis / nostr-client example snippets
use Innis\Nostr\Client\Infrastructure\Factory\NostrClientFactory;
use Innis\Nostr\Core\Application\Port\EventHandlerInterface;
use Innis\Nostr\Core\Domain\Collection\EventKindCollection;
use Innis\Nostr\Core\Domain\Entity\Event;
use Innis\Nostr\Core\Domain\ValueObject\Content\EventKind;
use Innis\Nostr\Core\Domain\ValueObject\Protocol\Filter;
use Innis\Nostr\Core\Domain\ValueObject\Protocol\RelayUrl;
use Innis\Nostr\Core\Domain\ValueObject\Protocol\SubscriptionId;
$client = NostrClientFactory::create();
$damus = RelayUrl::tryFromString('wss://relay.damus.io')
?? throw new InvalidArgumentException('Invalid relay URL');
$nosLol = RelayUrl::tryFromString('wss://nos.lol')
?? throw new InvalidArgumentException('Invalid relay URL');
$client->connect($damus);
$client->connect($nosLol);
$handler = new class implements EventHandlerInterface {
public function handleEvent(Event $event, SubscriptionId $subscriptionId): void
{
echo substr((string) $event->getContent(), 0, 100)."\n";
}
public function handleEose(SubscriptionId $subscriptionId): void {}
public function handleClosed(SubscriptionId $subscriptionId, string $message): void {}
public function handleNotice(RelayUrl $relayUrl, string $message): void {}
};
$filter = new Filter(kinds: EventKindCollection::fromInts([EventKind::TEXT_NOTE]), limit: 10);
$subscriptionId = $client->subscribe($damus, $filter, $handler);
\Amp\delay(5);
$client->unsubscribe($damus, $subscriptionId);
$client->close();
use Innis\Nostr\Core\Domain\Factory\RumourFactory;
use Innis\Nostr\Core\Domain\ValueObject\Identity\KeyPair;
use Innis\Nostr\Core\Infrastructure\Crypto\Secp256k1Signer;
$signer = Secp256k1Signer::create();
$keyPair = KeyPair::generate($signer);
$signedEvent = RumourFactory::createTextNote($keyPair->getPublicKey(), 'Hello Nostr!')
->sign($keyPair, $signer);
// publishEvent() returns a Future<PublishResult>. Await it for the relay's verdict,
// or drop it for fire-and-forget.
$result = $client->publishEvent($relay, $signedEvent)->await();
if ($result->isAccepted()) {
echo "Stored by the relay\n";
} else {
echo "Rejected: {$result->getMessage()}\n";
}
use Innis\Nostr\Core\Domain\Collection\EventKindCollection;
use Innis\Nostr\Core\Domain\Collection\FilterCollection;
$subscriptionId = $client->subscribeMultiple(
$relay,
new FilterCollection([
new Filter(kinds: EventKindCollection::fromInts([EventKind::TEXT_NOTE]), limit: 10),
new Filter(kinds: EventKindCollection::fromInts([EventKind::REACTION]), limit: 10),
]),
$handler,
);
use Innis\Nostr\Client\Application\Port\ReconnectionListenerInterface;
use Innis\Nostr\Core\Domain\ValueObject\Protocol\RelayUrl;
$listener = new class implements ReconnectionListenerInterface {
public function onReconnected(RelayUrl $relayUrl): void
{
// resubscribe, reauthenticate, etc.
}
};
$client->setReconnectionListener($listener);