chinatown34/php-electrum-api
Electrum JSONRPC-API的PHP封装
v1.7.0
2020-10-15 15:33 UTC
Requires
This package is not auto-updated.
Last update: 2024-09-20 03:33:21 UTC
README
php-electrum-api - Electrum库
Licence: GPL-3.0
Author: Pascal Krason <p.krason@padr.io>
Language: PHP 5.6-7.1
请注意,这个库目前尚未完成,但仍可用于生产环境。到目前为止,我只实现了最常用的API调用。如果您认为遗漏了什么,请创建一个问题或分支项目。
设置Electrum
首先您需要设置一个新的Electrum钱包。根据您的操作系统,在Electrum下载页面上遵循说明。安装成功后,您需要通过输入以下命令设置rpc端口:
electrum setconfig rpcport 7777
electrum setconfig rpcuser "username"
electrum setconfig rpcpassword "password"
然后我们可以创建一个默认钱包,别忘了记录生成的种子,如果有一天需要恢复,这是必要的。
electrum create
现在我们可以继续以守护进程模式启动Electrum
electrum daemon start
由于一些新版本的Electrum要求您在启动时手动加载钱包
electrum daemon load_wallet
要求
在PHP方面,没有太多要求,您只需要至少PHP 5.6和安装了curl扩展。然后您可以通过Composer继续操作,Composer将为您完成其他所有工作。
安装
首先您需要安装Composer,完成此步骤后,您可以继续操作。
composer require padrio/php-electrum-api
然后您可以直接包含自动加载器并开始使用库
// Include composer autoloader require_once 'vendor/autoloader.php';
示例
基本示例
一个非常基本的用法示例。每个API调用都有自己的请求对象。您只需创建一个并执行它。
$method = new \Electrum\Request\Method\Version(); try { $response = $method->execute(); } catch(\Exception $exception) { die($exception->getMessage()); } $response->getVersion();
创建新钱包
创建默认钱包
$client = new \Electrum\Client('http://127.0.0.1', 7777, 0, 'user', 'password'); $wallet = new \Electrum\Request\Method\Wallet\CreateWallet($client); $response = $wallet->execute();
此代码类似于以下命令
$ electrum create
您也可以通过指定新钱包的标志来创建具有自定义名称的更多钱包。
$client = new \Electrum\Client('http://127.0.0.1', 7777, 0, 'user', 'password'); $wallet = new \Electrum\Request\Method\Wallet\CreateWallet($client); $response = $wallet->execute(['wallet_path' => '~/.electrum/wallets/your_wallet']);
此代码类似于以下命令
$ electrum create -w ~/.electrum/wallets/your_wallet
响应将是
[ 'seed' => 'wallet seed', 'path' => 'path where wallet file is stored', 'msg' => 'Please keep your seed in a safe place; if you lose it, you will not be able to restore your wallet.', ];
加载钱包
$client = new \Electrum\Client('http://127.0.0.1', 7777, 0, 'user', 'password'); $load_wallet = new \Electrum\Request\Method\Wallet\LoadWallet($client); $load_wallet->execute(['wallet_path' => '~/.electrum/wallets/your_wallet']);
列出钱包
获取所有已加载钱包的列表
$client = new \Electrum\Client('http://127.0.0.1', 7777, 0, 'user', 'password'); $list_wallets = new \Electrum\Request\Method\Wallet\ListWallets($client); $list_wallets->execute();
获取新地址
$client = new \Electrum\Client('http://127.0.0.1', 7777, 0, 'user', 'password'); $wallet = new \Electrum\Request\Method\Payment\AddRequest($client); $tx = $wallet->execute(); echo $tx->getAddress();
为钱包创建新地址
$client = new \Electrum\Client('http://127.0.0.1', 7777, 0, 'user', 'password'); $newAddress = new \Electrum\Request\Method\Address\CreateNewAddress($client); $newAddress->execute(['wallet' => '~/.electrum/wallets/your_wallet']);
发起新的支付
$client = new \Electrum\Client('http://127.0.0.1', 7777, 0, 'user', 'password'); $method = new \Electrum\Request\Method\Payment\PayTo($client); $method->setDestination('BTC4ddress1234'); //Destination parameter is the address where we'll send the btc $method->setAmount(1); //send 1 BTC = 10k usd $tx = $method->execute(); //$tx returns the transaction ID of the payment, this is still not sent to the blockchain /** * @param array ['password' => '<password>'] * If the Electrum wallet is encrypted with a password use the following execute method instead * The previous one will return an error of "Password required" */ //$tx = $method->execute(['password' => 'myPass123']); // $broadcast = new Electrum\Request\Method\Payment\Broadcast($client); $broadcast->setTransaction($tx); $result = $broadcast->execute(); //broadcasts payment to the blockchain echo $result; A payment has been made
自定义客户端配置
每个请求/方法都接受一个Electrum\Client
实例作为参数,该实例替换默认实例。自定义实例在需要设置自定义配置参数(如不同的主机名或端口)时非常有用。
$client = new \Electrum\Client('http://127.0.0.1', 7777, 0, 'username', 'password'); $method = new \Electrum\Request\Method\Version($client); try { $response = $method->execute(); } catch (\Exception $exception) { die($exception->getMessage()); } $response->getVersion();
高级异常处理
处理异常很容易。您可以捕获两种类型的异常,指示是请求还是响应错误。
$method = new \Electrum\Request\Method\Version(); try { $response = $method->execute(); } catch (\Electrum\Request\Exception\BadRequestException $exception) { die(sprintf( 'Failed to send request: %s', $exception->getMessage() )); } catch(\Electrum\Response\Exception\BadResponseException $exception) { die(sprintf( 'Electrum-Client failed to respond correctly: %s', $exception->getMessage() )); }