PHP code example of jeankassio / electrumphp

1. Go to this page and download the library: Download jeankassio/electrumphp 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/ */

    

jeankassio / electrumphp example snippets




use JeanKassio\ElectrumPHP;

$walletPath = dirname(__FILE__) . "/wallet/walletfile";
$walletPass = "0123456789";
$rpcUser = "user";
$rpcPass = "9876543210";
$rpcPort = 7777;
$rpcHost = "127.0.0.1";
$binary = false; //if false, the code find automatically the binary of Electrum

$electrum = new ElectrumPHP($walletPath, $walletPass, $rpcUser, $rpcPass, $rpcPort, $rpcHost, $binary);


$electrum->start();

$electrum->stop();

if($electrum->isRunning()){
    echo "Electrum daemon is running.";
}

$address = "1PuJjnF476W3zXfVYmJfGnouzFDAXakkL4";
$valid = $electrum->validate($address);
echo "The Bitcoin address is " . ($valid ? "valid" : "invalid");

$walletPath = dirname(__FILE__) . "/wallet/walletfile";
$seed = false;
$password = "0123456789";
$encrypt = true;
$segwit = true;
$language = "english";
$entropy = 256;
$response = $electrum->createWallet($walletPath, $password, $seed, $encrypt, $segwit, $language, $entropy);
echo "Seed: " . $response['seed'];

$walletPath = dirname(__FILE__) . "/wallet/walletfile";
$seed = "excess title assist very badge rain pet purchase device narrow awesome recall";
$password = NULL;
$encrypt = false;
$segwit = false;
$response = $electrum->createWallet($walletPath, $password, $seed, $encrypt, $segwit);
echo "Seed: " . $response['seed'];

$address = $electrum->createAddress();
echo "New address: " . $address;

$address = "1PuJjnF476W3zXfVYmJfGnouzFDAXakkL4";
$balance = $electrum->getAddressBalance($address);
echo "Confirmed: " . $balance['confirmed'];
echo "Unconfirmed: " . $balance['unconfirmed'];

$balance = $electrum->getWalletBalance();
if(isset($balance['confirmed'])){
  echo "Confirmed: " . $balance['confirmed'];
}
				
if(isset($balance['unconfirmed'])){
  echo "Unconfirmed: " . $balance['unconfirmed'];
}
				
if(isset($balance['unmatured'])){
  echo "Unmatured: " . $balance['unmatured'];
}

$txid = "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b";
$transaction = $electrum->getTransaction($txid);
echo "Transaction details: " . json_encode($transaction);

$txid = "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b";
$confirmations = $electrum->getConfirmations($txid); //return int
echo "Number of confirmations: {$confirmations}";

$address = "bc1qlaee57ehqfe2388muudvrf7wvuw2p3lwz0kzh4";
$amount = 0.001;
$fee = 0.00001;
$feerate = NULL;
$fromAddr = "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa";
$fromCoins = NULL;
$change = "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa";
$nocheck = false;
$unsigned = false;
$replaceByFee = true;

$response = $electrum->pay($address, $amount, $fee, $feerate, $fromAddr, $fromCoins, $change, $nocheck, $unsigned, $replaceByFee);
echo "TXID: " . json_encode($response);

$outputs = [
  [
    "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa",
    0.002
  ],
  [
    "bc1pgmpdnkdyxs6qp2we865tz43umrssvsqweaxvz2cuq5gp9zz6e6tstuj3wa",
    0.0015
  ],
  [
    "bc1pce8yk5epjlqrpnnavelul54ed6663tjqv6taz3gq9cte979624uqjjrv55",
    5.9216
  ]
];
$fee = 0.0009;
$feerate = NULL;
$fromAddr = "bc1plgjpn3cr5khxfee0k40py8njx0qcejrqldldnedqrshvut64jlvs467hnr";
$fromCoins = NULL;
$change = "bc1pqys8mqkneumdyncyz42ljd5zl9gqw4pryd9xsj9gnx5cw6rlvn0sjhdlhy";
$nocheck = false;
$unsigned = false;
$replaceByFee = false;

$response = $electrum->payToMany($outputs, $fee, $feerate, $fromAddr, $fromCoins, $change, $nocheck, $unsigned, $replaceByFee);
echo "TXID: " . json_encode($response);

//Load the wallet instantiated
$electrum->loadWallet();

$wallets = $electrum->getWalletsOpen();
echo "Open wallets: " . json_encode($wallets);

$address = "bc1pce8yk5epjlqrpnnavelul54ed6663tjqv6taz3gq9cte979624uqjjrv55";
$yourUrl = "https://your-webhook-url";
$response = $electrum->notify($address, $yourUrl);
echo "Notify response: " . json_encode($response);

$address = "bc1pce8yk5epjlqrpnnavelul54ed6663tjqv6taz3gq9cte979624uqjjrv55";
$response = $electrum->deleteNotify($address);
echo "Notification deleted: " . json_encode($response);

$address = "bc1pce8yk5epjlqrpnnavelul54jjrv55";
$privateKey = $electrum->getPrivateKeys($address);
echo "Private key: " . $privateKey;

$seed = $electrum->getSeed();
echo "Wallet seed: " . $seed;

$method = "getaddressunspent";
$params = [
  'address' => "bc1pce8yk5epjlqrpnnavelul54ed6663tjqv6taz3gq9cte979624uqjjrv55"
];
$response = $electrum->custom($method, $params);
echo "Response: " . $response;


$outputs = [
	[
		"bc1qq4khfcyhnds27zqc8wldxu97lzr7u233t8n3e3",
		0.00010604
	],
	[
		"bc1qrdzwvaj84lwlkclp6ruz6y4c6ac7whqltjl6w9",
		0.00010000
	]
];

$checkBalance = true;

$infos = $electrum->getInfosBeforeTransaction($outputs, $checkBalance);
echo json_encode($infos);


try{
    $electrum->start();
}catch(Exception $e){
    echo "Error: " . $e->getMessage();
}