php 调用rpc eos,EOS Chain/Wallet RPC API的PHP开发包

本文介绍了如何配置和使用manamine/php-eos-rpc-sdk,展示了如何通过工厂方法实例化API并执行各种区块链和钱包操作,如获取区块信息、账户详情、交易推送等。此外,还提供了示例代码以帮助快速上手。
摘要由CSDN通过智能技术生成

法。

安装composer require manamine/php-eos-rpc-sdk

配置

使用你最喜欢的RPC API主机和KEOSD在项目根目录中创建一个dotenv.env文件。你可以使用env.example作为模板:cp .env.example .env

有一个漂亮的工厂方法来自动化实例所有依赖项:$api = (new ChainFactory)->api();

$walapi = (new WalletFactory)->api();

$eos = (new EosRpc($api, $walapi));

示例

为了帮助你入门,运行一个简单的示例,它涵盖了所有API命令。

只需通过cli运行此命令即可查看所有命令的示例输出:cd examplesphp chain.phpphp wallet.phpphp eosrpc.php

API方法

涵盖了几乎所有区块链和钱包API方法。

区块链API

Get Info

获取与节点相关的最新信息echo $api->getInfo();

Get Block

获取与区块相关的信息echo $api->getBlock("1337");

Get Block Header State

获取与块头状态相关的信息echo $api->getBlockHeaderState("0016e48707b181d93117b07451d9837526eba34a9a37125689fb5a73a5d28a38");

Get Account

获取与帐户相关的信息$api->getAccount("blockmatrix1");

Get Code

获取智能合约代码echo $api->getCode("eosio.token");

Get Table Rows

从帐户中获取智能合约数据echo $api->getTableRows("eosio", "eosio", "producers", ["limit" => 10]);

Get Currency Balance

获取帐户的货币余额echo $api->getCurrencyBalance("eosio.token", "eosdacserver");

Get Currency Stats

获取货币状态echo $api->getCurrencyStats("eosio.token", "EOS");

Get ABI

获取账户ABIecho $api->getAbi("eosio.token");

Get Raw Code and ABI

获取原生代码和ABIecho $api->getRawCodeAndAbi("eosio.token");

Get Producers

列出生产者echo $api->getProducers(10);

ABI JSON To Bin

将json序列化为二进制十六进制echo $api->abiJsonToBin("eosio.token", "transfer", ["blockmatrix1", "blockmatrix1", "7.0000 EOS", "Testy McTest"]);

ABI Bin To JSON

将二进制十六进制序列化为jsonecho $api->abiBinToJson("eosio.token", "transfer", "10babbd94888683c10babbd94888683c701101000000000004454f53000000000c5465737479204d6354657374");

Get Required Keys

获取签署交易所需的密钥echo $api->getRequiredKeys(

[         "expiration" => "2018-08-23T05.00.00",         "ref_block_num" => 15078,         "ref_block_prefix" => 1071971392,         "max_net_usage_words" => 0,         "delay_sec" => 0,         "context_free_actions" => [],         "actions" => [

[                 "account" => "eosio.token",                 "name" => "transfer",                 "authorization" => [

[                         "actor" => "user",                         "permission" => "active"

]

],                 "data" => "00000000007015d6000000005c95b1ca102700000000000004454f53000000000c757365722d3e746573746572"

]

],         "transaction_extensions" => []

],

[         "EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4"

]

);

Push Transaction

提交推送一个交易echo $api->pushTransaction("2018-08-23T05:29:39", "15780", "90170226",

[        "actions" => [

[                "account" => "eosio.token",                "name" => "transfer",                "authorization" => [

[                        "actor" => "user",                        "permission" => "active"

]

],                "data" => "00000000007015d6000000005c95b1ca102700000000000004454f53000000000c757365722d3e746573746572"

]

],        "signatures" => [            "SIG_K1_KaGHyi59BRqfaDUK6424TYEWcUhWxAG7BLCgYC8vwYNgaHgGLpduTUbNQEsfL8xLzboK8W9T2X69bNpqozTQVCbRSNJWFd"

]

]

);

Push transactions

提交推送一些交易echo $api->pushTransactions(

[

[            "compression" => "none",            "transaction" => [                "expiration" => "2018-08-23T06:27:26",                "ref_block_num" => 22017,                "ref_block_prefix" => 3920123292,                "context_free_actions" => [],                "actions" => [

[                        "account" => "eosio.token",                        "name" => "transfer",                        "authorization" => [

[                                "actor" => "user",                                "permission" => "active"

]

],                        "data" => "00000000007015d6000000005c95b1ca102700000000000004454f53000000000c757365722d3e746573746572"

]

],                "transaction_extensions" => []

],            "signatures" => [                "SIG_K1_JzN9DnpyhKfjoef3C2TZBTPA5b6ftwuEBnBpvzkueVXThJ34PFFpUFgqyayfXjeLRc15JmZmDiMYAFX99hUgX8vkGAYcnx"

]

],

[            "compression" => "none",            "transaction" => [                "expiration" => "2018-08-23T06:27:26",                "ref_block_num" => 22017,                "ref_block_prefix" => 3920123292,                "context_free_actions" => [],                "actions" => [

[                        "account" => "eosio.token",                        "name" => "transfer",                        "authorization" => [

[                                "actor" => "tester",                                "permission" => "active"

]

],                        "data" => "000000005c95b1ca00000000007015d6881300000000000004454f53000000000c7465737465722d3e75736572"

]

],                "transaction_extensions" => []

],            "signatures" => [                "SIG_K1_KZ2M4AG59tptdRCpqbwzMQvBv1dce5btJCJiCVVy96fTGepApGXqJAwsi17g8AQdJjUQB4R62PprfdUdRYHGdBqK1z9Sx9"

]

]

]

);

钱包 Wallet APIs

Create

创建具有给定名称的新钱包echo $walapi->create("testwallet");

Open

打开给定名称的现有钱包echo $walapi->open("testwallet");

Lock

锁定给定名称的现有钱包echo $walapi->lock("testwallet");

Lock All

锁定所有现有钱包echo $walapi->lockAll();

Unlock

使用给定的名称和密码解锁钱包echo $ walapi-> unlock([“testwallet”,“PW5Jb8RAZP6CBjjMLPser3T8i8k9hZXZkMBJ8kb1p6f6hAg2n68jY”]);

Import Key

将私钥导入给定名称的钱包echo $walapi->importKey(["testwallet", "5Jmsawgsp1tQ3GD6JyGCwy1dcvqKZgX6ugMVMdjirx85iv5VyPR"]);

Remove Key

从给定名称的钱包中删除密钥对echo $walapi->removeKey(["testwallet", "PW5Jb8RAZP6CBjjMLPser3T8i8k9hZXZkMBJ8kb1p6f6hAg2n68jY", "EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4"]);

Create Key

创建密钥对并导入echo $walapi->createKey(["testwallet", "K1"]);

List Wallets

列出所有钱包echo $walapi->listWallets();

List Keys

列出给定名称和密码的钱包中的所有密钥对echo $walapi->listKeys(["testwallet", "PW5Jb8RAZP6CBjjMLPser3T8i8k9hZXZkMBJ8kb1p6f6hAg2n68jY"]);

Get Public Keys

列出所有钱包中的所有公钥echo $walapi->getPublicKeys();

Set Timeout

设置钱包自动锁定超时(以秒为单位)echo $walapi->setTimeout(60);

Sign Transaction

签署交易echo $walapi->signTransaction(

[        "expiration" => "2018-08-23T06:35:30",        "ref_block_num" => 22985,        "ref_block_prefix" => 3016594541,        "max_net_usage_workds" => 0,        "delay_sec" => 0,        "context_free_actions" => [],        "actions" => [

[                "account" => "eosio.token",                "name" => "transfer",                "authorization" => [

[                        "actor" => "user",                        "permission" => "active"

]

],                "data" => "00000000007015d6000000005c95b1ca102700000000000004454f53000000000c757365722d3e746573746572"

]

],        "transaction_extensions" => []

],

[        "EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4"

],    "cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f");

EOS Wrapper APIs

Prerequisites

需要设置钱包名称和密码$eos->setWalletInfo("testwallet", "PW5Jb8RAZP6CBjjMLPser3T8i8k9hZXZkMBJ8kb1p6f6hAg2n68jY");

Push Transaction

推送交易echo $eos->pushTransaction(

[

[            "account" => "eosio.token",            "name" => "transfer",            "authorization" => [

[                    "actor" => "user",                    "permission" => "active"

]

],            "data" => [                "from" => "user",                "to" => "tester",                "quantity" => "1.0000 EOS",                "memo" => "memo"

]

]

]

);

Make Transaction

进行交易(对pushTransactions有用)$trx = $eos->makeTransaction(

[

[            "account" => "eosio.token",            "name" => "transfer",            "authorization" => [

[                    "actor" => "user",                    "permission" => "active"

]

],            "data" => [                "from" => "user",                "to" => "tester",                "quantity" => "1.0000 EOS",                "memo" => "memo"

]

]

]

);

Push Transactions

推送一些交易$trx_ids = $eos->pushTransactions(

[

$eos->makeTransaction(

[

[                    "account" => "eosio.token",                    "name" => "transfer",                    "authorization" => [

[                            "actor" => "user",                            "permission" => "active"

]

],                    "data" => [                        "from" => "user",                        "to" => "tester",                        "quantity" => "1.0000 EOS",                        "memo" => "memo"

]

]

]

),

$eos->makeTransaction(

[

[                    "account" => "eosio.token",                    "name" => "transfer",                    "authorization" => [

[                            "actor" => "tester",                            "permission" => "active"

]

],                    "data" => [                        "from" => "tester",                        "to" => "user",                        "quantity" => "0.5000 EOS",                        "memo" => "memo"

]

]

]

)

]

);foreach ($trx_ids as $key => $value) {    echo $trx_ids[$key]['transaction_id'] . PHP_EOL;

}

Push Action

推动一个操作echo $eos->pushAction("eosio", "buyram", ["payer"=>"tester","receiver"=>"tester","quant"=>"1.0000 EOS"], ["actor"=>"tester","permission"=>"active"]);

Transfer

交易支付echo $eos->transfer("user", "tester", "1.0000 EOS", "memo");

Create Key Pair

创建密钥对并返回$keyPair = $eos->createKeyPair("K1");echo "$keyPair[0], $keyPair[1]";

Tests

要运行测试套件,只需执行:vendor/bin/phpunit

如果您想获得想象并检查代码覆盖率:vendor/bin/phpunit --coverage-html tests/coverage

如果你真的很无聊,你可能想要运行一些静态分析:vendor/bin/phpmetrics --report-html="tests/static" .

=============================================================

作者:编程狂魔

链接:https://www.jianshu.com/p/f9fffd411747

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值