PHP code example of amashukov / lnd-client-php

1. Go to this page and download the library: Download amashukov/lnd-client-php 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/ */

    

amashukov / lnd-client-php example snippets


use Amashukov\Lnd\LndClient;
use Amashukov\Lnd\Msat;
use Nyholm\Psr7\Factory\Psr17Factory;

$psr17 = new Psr17Factory();

// The macaroon header and TLS options belong in your PSR-18 pipeline, not here.
$client = new LndClient($http, $psr17, $psr17, 'https://lnd:8080');

$invoice = $client->addInvoice(Msat::fromBtc('0.0005'), 'order-7f3a91c2', 3600);
echo $invoice->bolt11;       // lnbc500u1p...
echo $invoice->paymentHash;  // lowercase hex, ready to persist

// later, from a poller
if ($client->lookupInvoice($invoice->paymentHash)->isSettled()) {
    // credit the order
}

$payment = $client->sendPayment($bolt11, feeLimit: Msat::fromString('150000'), timeoutSeconds: 60);

if ($payment->isFailed()) {
    // terminal: no funds left the node
}

$state = $client->trackPayment($paymentHash);

match (true) {
    $state->isSucceeded() => $order->finalize(),
    $state->isFailed()    => $order->markFailed(),
    default               => null, // still unresolved — poll again, do NOT re-send
};

$client->connectPeer($pubkey, 'node.example:9735');
$point = $client->openChannel($pubkey, localFundingSat: '10000000', satPerVbyte: 8);

foreach ($client->listChannels() as $channel) {
    // basis points, for liquidity alerting
    $channel->localShareBps();
    $channel->inboundShareBps();
}

$client->closeChannel($point, force: false, satPerVbyte: 8);
bash
composer