ElectrumPHP is a PHP library designed to interact with the Electrum wallet through RPC calls. With this library, you can manage wallets, generate addresses, check balances and transaction history, and make Bitcoin payments using the Electrum daemon.
- PHP 7.4 or higher
- Curl enabled
- Electrum installed and configured on your server
composer require jeankassio/electrumphp
require_once(dirname(__FILE__) . "/path/to/autoload.php");
use JeanKassio\ElectrumPHP;
$walletPath = "/root/wallet";
$walletPass = "0123456789";
$rpcUser = "user";
$rpcPass = "9876543210";
$rpcPort = 7777;
$rpcHost = "127.0.0.1";
$electrum = new ElectrumPHP($walletPath, $walletPass, $rpcUser, $rpcPass, $rpcPort, $rpcHost);
$electrum->start();
$electrum->stop();
if($electrum->isRunning()){
echo "Electrum daemon is running.";
}
$walletPath = "/root/wallet";
$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 = "/root/wallet";
$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;
$balance = $electrum->getAddressBalance($address);
echo "Confirmed: " . $balance['confirmed'];
echo "Unconfirmed: " . $balance['unconfirmed'];
$balance = $electrum->getWalletBalance();
echo "Confirmed: " . $balance['confirmed'];
echo "Unconfirmed: " . $balance['unconfirmed'];
echo "Unmatured: " . $balance['unmatured'];
$txid = "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b";
$transaction = $electrum->getTransaction($txid);
echo "Transaction details: " . json_encode($transaction);
$address = "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa";
$amount = 0.001;
$fee = 0.00001;
$feerate = NULL;
$fromAddr = "bc1plgjpn3cr5khxfee0k40py8njx0qcejrqldldnedqrshvut64jlvs467hnr";
$fromCoins = NULL;
$change = "bc1pqys8mqkneumdyncyz42ljd5zl9gqw4pryd9xsj9gnx5cw6rlvn0sjhdlhy";
$nocheck = false;
$unsigned = false;
$replaceByFee = true;
$response = $electrum->pay($address, $amount, $fee, $feerate, $fromAddr, $fromCoins, $change, $nocheck, $unsigned, $replaceByFee);
echo "Payment response: " . json_encode($response);
$addressesTo = [
[
"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($addressesTo, $fee, $feerate, $fromAddr, $fromCoins, $change, $nocheck, $unsigned, $replaceByFee);
echo "Payment response: " . json_encode($response);
$pathWallet = "path/to/wallet";
$password = "0123456789";
$electrum->loadWallet($pathWallet, $password);
$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 = "bc1pce8yk5epjlqrpnnavelul54ed6663tjqv6taz3gq9cte979624uqjjrv55";
$privateKey = $electrum->getPrivateKeys($address);
echo "Private key: " . $privateKey;
$seed = $electrum->getSeed();
echo "Wallet seed: " . $seed;
$method = "getaddressunspent";
$params = ["bc1pce8yk5epjlqrpnnavelul54ed6663tjqv6taz3gq9cte979624uqjjrv55"];
$response = $electrum->custom($method, $params);
echo "Response: " . $response;
try{
$electrum->start();
}catch(Exception $e){
echo "Error: " . $e->getMessage();
}
Contributions are welcome! If you find a bug or have suggestions for improvements.
Feel free to open an issue or submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE.md file for details.