### Installing Dependencies with Composer (Console) Source: https://github.com/nostrver-se/nostr-php/blob/main/README.md Run this command to install project dependencies, useful for testing or working with the provided code examples. ```console $ composer install ``` -------------------------------- ### Using nostr-php CLI Client (Console) Source: https://github.com/nostrver-se/nostr-php/blob/main/README.md This example demonstrates how to use the `bin/nostr-php` command-line client to post a text note to a Nostr relay, specifying the content, private key file path, and relay address. ```console Usage: $ bin/nostr-php --content "Hello world!" --key /home/path/to/nostr-private.key --relay wss://nostr.pleb.network ``` -------------------------------- ### Installing nostr-php with Composer (Console) Source: https://github.com/nostrver-se/nostr-php/blob/main/README.md Use this command to add the nostr-php package as a dependency to your PHP project using Composer. ```console $ composer require swentel/nostr-php ``` -------------------------------- ### Publishing a Nostr Event to a Single Relay (PHP) Source: https://github.com/nostrver-se/nostr-php/blob/main/README.md This example combines event creation, signing, and message generation, then shows how to use the Relay class to send the prepared event message to a single specified Nostr relay URL. ```php use swentel\nostr\Event\Event; use swentel\nostr\Message\EventMessage; use swentel\nostr\Relay\Relay; $note = new Event(); $note->setContent('Hello world'); $note->setKind(1); $signer = new Sign(); $signer->signEvent($note, $private_key); $eventMessage = new EventMessage($note); $relayUrl = 'wss://nostr-websocket.tld'; $relay = new Relay($relayUrl); $relay->setMessage($eventMessage); $result = $relay->send(); ``` -------------------------------- ### Example Response Structure for Reading Events (PHP) Source: https://github.com/nostrver-se/nostr-php/blob/main/README.md This snippet illustrates the expected structure of the $response variable after successfully reading events from a relay using the previous code example. It shows a multidimensional array keyed by relay URL, containing decoded event messages. ```php [ 'wss://nostr-websocket.tld' => [ 0 => [ "EVENT", "A8kWzjCVUHSD1rmuwGqyK2PxsolZMO9YXditbg05fch6p3Q4eT7vRFLEJINBna", [ 'id' => '1e8534623845629d40f7761c0577edf10f778c490e7b95a524845d9280c7c25a', 'kind' => 1, 'pubkey' => '06639a386c9c1014217622ccbcf40908c4f1a0c33e23f8d6d68f4abf655f8f71', 'created_at' => 1718723787, 'content' => 'Losing your social graph can feel the same for some I think 😮 ', 'tags' => [ ['e', 'f754a238947b7f32168f872650a8dd0b9376493e58005d7e0b8be52f6f229364', 'wss://nos.lol/', 'root'], ['e', 'fe7dd6ba22fa0aa39370aa160226b8bc2413460621c8d67ce862205ad5a02c24', 'wss://nos.lol/', 'reply'], ['p', 'fb1366abd5e4c92a8a950791bc72d51bde291a83555cb2c629a92fedd78068ac', '', 'mention'] ], 'sig' => '888c9b5d9e0b69eba3510dd2b5d03eddcf0a680ab0e7673820fb36a56448ad80701042a669c7ef9918593c5a41c8b3ccc1d82ade50f32b62dd843144f32df403' ], 1 => [ "EVENT", "A8kWzjCVUHSD1rmuwGqyK2PxsolZMO9YXditbg05fch6p3Q4eT7vRFLEJINBna", [ ...Nostr event ] ], 2 => [ ... ], 3 => [ ... ], 4 => [ ... ] ] ] ``` -------------------------------- ### Running PHPUnit Tests (Console) Source: https://github.com/nostrver-se/nostr-php/blob/main/README.md This command executes the PHPUnit test suite for the project, located in the `tests` directory, using the vendor binary. ```console $ php vendor/bin/phpunit ``` -------------------------------- ### Creating a Nostr Event (PHP) Source: https://github.com/nostrver-se/nostr-php/blob/main/README.md This snippet demonstrates how to instantiate an Event object, set its kind (e.g., 1 for a text note), set the content, and add tags using both setTags and addTag methods. ```php use swentel\nostr\Event\Event; $note = new Event(); $note->setKind(1); $note->setContent('Hello world!'); $note->setTags([ ['e', $relayUrl], ['p', $public_key, $relayUrl], ['r', $relayUrl] ]); // or use addTag() $note->addTag(['p', $public_key, $relayUrl]); ``` -------------------------------- ### Reading Events from Multiple Relays (PHP) Source: https://github.com/nostrver-se/nostr-php/blob/main/README.md This snippet shows how to fetch events from a predefined set of Nostr relays using the `RelaySet` class. It configures filters, creates a subscription, builds a request message, defines relays, sets up the `RelaySet`, and sends the request. ```php $filter1 = new Filter(); $filter1->setKinds([1]); $filter1->setLimit(5); $filters = [$filter1]; $subscription = new Subscription(); $requestMessage = new RequestMessage($subscription->getId(), $filters); $relays = [ new Relay('wss://nostr-websocket-1.tld'), new Relay('wss://nostr-websocket-2.tld'), new Relay('wss://nostr-websocket-3.tld'), ]; $relaySet = new RelaySet(); $relaySet->setRelays($relays); $request = new Request($relaySet, $requestMessage); $response = $request->send(); ``` -------------------------------- ### Generating Documentation with phpDocumentor (Console) Source: https://github.com/nostrver-se/nostr-php/blob/main/README.md This command invokes phpDocumentor to generate API documentation for the project. The output is saved to the `phpdoc.nostr-php.dev` directory. ```console $ phpdoc ``` -------------------------------- ### Reading Nostr Events from a Relay (PHP) Source: https://github.com/nostrver-se/nostr-php/blob/main/README.md This code demonstrates how to define filters (e.g., by kind, limit), create a subscription and request message, and use the Relay and Request classes to fetch events matching the filters from a specified relay. ```php $filter1 = new Filter(); $filter1->setKinds([1, 3]); // You can add multiple kind numbers $filter1->setLimit(25); // Limit to fetch only a maximum of 25 events $filters = [$filter1]; // You can add multiple filters. $subscription = new Subscription(); $requestMessage = new RequestMessage($subscription->getid(), $filters); $relayUrl = 'wss://nostr-websocket.tld'; $relay = new Relay($relayUrl); $relay->setMessage($requestMessage); $request = new Request($relay, $requestMessage); $response = $request->send(); ``` -------------------------------- ### Publishing a Nostr Event to Multiple Relays (PHP) Source: https://github.com/nostrver-se/nostr-php/blob/main/README.md Instead of sending to one relay, this snippet shows how to use the RelaySet class to manage and send the same prepared event message to a collection of multiple relay URLs simultaneously. ```php $relay1 = new Relay('wss://nostr-websocket1.tld'); $relay2 = new Relay('wss://nostr-websocket2.tld'); $relay3 = new Relay('wss://nostr-websocket3.tld'); $relay4 = new Relay('wss://nostr-websocket4.tld'); $relaySet = new RelaySet(); $relaySet->setRelays([$relay1, $relay2, $relay3, $relay4]); $relaySet->setMessage($eventMessage); $result = $relaySet->send(); ``` -------------------------------- ### Converting Hex Keys to Bech32 (PHP) Source: https://github.com/nostrver-se/nostr-php/blob/main/README.md This code demonstrates converting hexadecimal Nostr public and private keys into their respective bech32-encoded formats (npub and nsec) using the `swentel\nostr\Key\Key` class. ```php use swentel\nostr\Key\Key; $public_key = '7e7e9c42a91bfef19fa929e5fda1b72e0ebc1a4c1141673e2794234d86addf4e'; $private_key = '67dea2ed018072d675f5415ecfaed7d2597555e202d85b3d65ea4e58d2d92ffa'; $key = new Key(); $bech32_public = $key->convertPublicKeyToBech32($public_key); $bech32_private = $key->convertPrivateKeyToBech32($private_key); ``` -------------------------------- ### Generating Nostr Key Pair (PHP) Source: https://github.com/nostrver-se/nostr-php/blob/main/README.md This code snippet uses the `swentel\nostr\Key\Key` class to generate a new random private key and derive the corresponding public key. ```php use swentel\nostr\Key\Key; $key = new Key(); $private_key = $key->generatePrivateKey(); $public_key = $key->getPublicKey($private_key); ``` -------------------------------- ### Signing a Nostr Event (PHP) Source: https://github.com/nostrver-se/nostr-php/blob/main/README.md This code shows how to use the Sign class to generate the unique 'id' and 'sig' (signature) for a Nostr event using a private key. The 'pubkey', 'id', and 'sig' properties are added directly to the event object. ```php use swentel\nostr\Event\Event; use swentel\nostr\Sign\Sign; $note = new Event(); $note->setContent('Hello world!'); $note->setKind(1); $signer = new Sign(); $signer->signEvent($note, $private_key); ``` -------------------------------- ### Converting Bech32 Public Key to Hex (PHP) Source: https://github.com/nostrver-se/nostr-php/blob/main/README.md This snippet utilizes the `swentel\nostr\Key\Key` class to convert a bech32-encoded Nostr public key (npub) into its hexadecimal representation. ```php use swentel\nostr\Key\Key; $public_key = 'npub10elfcs4fr0l0r8af98jlmgdh9c8tcxjvz9qkw038js35mp4dma8qzvjptg'; $key = new Key(); $hex = $key->convertToHex($public_key); ``` -------------------------------- ### Generating a Nostr Event Message (PHP) Source: https://github.com/nostrver-se/nostr-php/blob/main/README.md After an event has been signed, this snippet demonstrates how to use the EventMessage class to format the event into the standard Nostr message string format: ["EVENT", ]. ```php use swentel\nostr\Sign\Sign; use swentel\nostr\Message\EventMessage; $signer = new Sign(); $signer->signEvent($note, $private_key); $eventMessage = new EventMessage($note); $message_string = $eventMessage->generate(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.