PHP code example of ndeet / ln-lnd-rest

1. Go to this page and download the library: Download ndeet/ln-lnd-rest 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/ */

    

ndeet / ln-lnd-rest example snippets



et tls and macaroon paths.
$tlsPath = '';
$macaroonPath = '';
$local_user = posix_getpwuid(posix_getuid());

switch (PHP_OS) {
  case "Darwin":
    $tlsPath = $local_user['dir'] . '/Library/Application Support/Lnd/tls.cert';
    $macaroonPath = $local_user['dir'] . '/Library/Application Support/Lnd/macaroon.admin';
    break;

  case "Linux":
    $tlsPath = $local_user['dir'] . '/.lnd/tls.cert';
    $macaroonPath = $local_user['dir'] . '/.lnd/macaroon.admin';
}

if (! $sslCert = file_get_contents($tlsPath)) {
  $certError = <<<EOT
    tls.cert not found in "example" directory. Make sure to copy it from your
    LND config directory.
    MacOS: ~/Library/Application Support/Lnd/tls.cert
    Linux: ~/.lnd/tls.cert
EOT;

  throw new Exception($certError);
}

// We need to use Configuration class for the url and can't pass it directly in GuzzleClient.
$apiConfig = new \Lnd\Rest\Configuration();
$apiConfig->setHost('https://localhost:8001');

// First we need to unlock the encrypted wallet. This needs only run once.
$walletInstance = new Lnd\Rest\Api\WalletUnlockerApi(
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
// This is optional, `GuzzleHttp\Client` will be used as default.
  new GuzzleHttp\Client([
    'debug' => TRUE,
    'verify' => $tlsPath,
    'headers' => [
      'Grpc-Metadata-macaroon' => $macaroon
    ]
  ]),
  $apiConfig
);

$unlockRequest = new \Lnd\Rest\Model\LnrpcUnlockWalletRequest([
  'walletPassword' => base64_encode('YOUR_WALLET_PASS')
]);

try {
  $unlocked = $walletInstance->unlockWallet($unlockRequest);
  // gives 408 timeout but unlock successful, afterwards 404 not found
} catch (Exception $e) {
  echo 'Exception when calling WalletUnlockerApi->unlockWallet(): ', $e->getMessage(), PHP_EOL;
}

// We can now use the getInfo endpoint:
$apiInstance = new Lnd\Rest\Api\LightningApi(
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
// This is optional, `GuzzleHttp\Client` will be used as default.
  new GuzzleHttp\Client([
    'debug' => TRUE,
    'verify' => $tlsPath,
    'headers' => [
      'Grpc-Metadata-macaroon' => bin2hex(file_get_contents($macaroonPath))
    ]
  ]),
  $apiConfig
);

try {
  $result = $apiInstance->getInfo();
  var_dump($result);
} catch (Exception $e) {
  echo 'Exception when calling LightningApi->getInfo: ', $e->getMessage(), PHP_EOL;
}

// Let's generate an lightning invoice.
$invoice = new \Lnd\Rest\Model\LnrpcInvoice([
  'memo' => 'testinvoice memo',
  'value' => 1001,
  'expiry' => 3600
]);

try {
  $invoiceResult = $apiInstance->addInvoice($invoice);
  var_dump($invoiceResult);
} catch (Exception $e) {
  echo 'Exception when calling LightningApi->addInvoice: ', $e->getMessage(), PHP_EOL;
}