Skip to content

Nostr-PHP

A PHP helper library for Nostr

Empower your 🐘 PHP project with Nostr.

Nostr-PHP

Get started

Add the nostr-php package to your PHP project with Composer

bash
composer require nostrverse/nostr-php

Here is an example how to create and publish an event to a relay:

php
<?php

require __DIR__ . '/vendor/autoload.php';

use swentel\nostr\Event\Event;
use swentel\nostr\Key\Key;
use swentel\nostr\Message\EventMessage;
use swentel\nostr\Relay\Relay;
use swentel\nostr\Sign\Sign;

function send($message) {  
  try {        
    $key = new Key();
    $private_key = $key->generatePrivateKey(); // this will generate a private key
    $private_key_hex = $key->convertToHex($private_key);
    $public_key = $key->getPublicKey($private_key_hex);
    
    $relayUrl = 'wss://relay.damus.io';
        
    $note = new Event();
    $note->setKind(1);
    $note->addTag(['p', $public_key]);
    $note->addTag(['r', $relayUrl]);
    $note->setContent($message);
        
    $signer = new Sign();
    $signer->signEvent($note, $private_key);        
        
    $eventMessage = new EventMessage($note);
        
    $relay = new Relay($relayUrl);  
    $relay->setMessage($eventMessage);      
    $result = $relay->send();
        
    if ($result->isSuccess()) {
      print "The event has been sent to Nostr!\n";
    } else {
      print 'Something went wrong: ' . $result->message() . "\n";
    }
  } catch (Exception $e) {
    print 'Exception error: ' . $e->getMessage() . "\n";
  }
}

$message = 'Hello world ' . date('Y-m-d H:i:s');
send($message);

For more examples please check this README.

Released under the MIT License. Supported by OpenSats.
Source Code