介紹一個(gè)EOS Chain/Wallet RPC API的PHP開發(fā)包招狸。
開始
你可以查看EOS的RPC API參考曹仗,但要注意缺少一些較新的方法。Wallet RPC API實(shí)現(xiàn)EOS v1.1.0 of RPC API reference扫腺。此外愁茁,這些文檔中的一些示例也使用了過時(shí)的語法蚕钦。
安裝
composer require manamine/php-eos-rpc-sdk
配置
使用你最喜歡的RPC API主機(jī)和KEOSD在項(xiàng)目根目錄中創(chuàng)建一個(gè)dotenv.env
文件。你可以使用env.example
作為模板:
cp .env.example .env
有一個(gè)漂亮的工廠方法來自動(dòng)化實(shí)例所有依賴項(xiàng):
$api = (new ChainFactory)->api();
$walapi = (new WalletFactory)->api();
$eos = (new EosRpc($api, $walapi));
示例
為了幫助你入門鹅很,運(yùn)行一個(gè)簡單的示例嘶居,它涵蓋了所有API命令。
只需通過cli運(yùn)行此命令即可查看所有命令的示例輸出:
cd examples
php chain.php
php wallet.php
php eosrpc.php
API方法
涵蓋了幾乎所有區(qū)塊鏈和錢包API方法促煮。
區(qū)塊鏈API
Get Info
獲取與節(jié)點(diǎn)相關(guān)的最新信息
echo $api->getInfo();
Get Block
獲取與區(qū)塊相關(guān)的信息
echo $api->getBlock("1337");
Get Block Header State
獲取與塊頭狀態(tài)相關(guān)的信息
echo $api->getBlockHeaderState("0016e48707b181d93117b07451d9837526eba34a9a37125689fb5a73a5d28a38");
Get Account
獲取與帳戶相關(guān)的信息
$api->getAccount("blockmatrix1");
Get Code
獲取智能合約代碼
echo $api->getCode("eosio.token");
Get Table Rows
從帳戶中獲取智能合約數(shù)據(jù)
echo $api->getTableRows("eosio", "eosio", "producers", ["limit" => 10]);
Get Currency Balance
獲取帳戶的貨幣余額
echo $api->getCurrencyBalance("eosio.token", "eosdacserver");
Get Currency Stats
獲取貨幣狀態(tài)
echo $api->getCurrencyStats("eosio.token", "EOS");
Get ABI
獲取賬戶ABI
echo $api->getAbi("eosio.token");
Get Raw Code and ABI
獲取原生代碼和ABI
echo $api->getRawCodeAndAbi("eosio.token");
Get Producers
列出生產(chǎn)者
echo $api->getProducers(10);
ABI JSON To Bin
將json序列化為二進(jìn)制十六進(jìn)制
echo $api->abiJsonToBin("eosio.token", "transfer", ["blockmatrix1", "blockmatrix1", "7.0000 EOS", "Testy McTest"]);
ABI Bin To JSON
將二進(jìn)制十六進(jìn)制序列化為json
echo $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
提交推送一個(gè)交易
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
創(chuàng)建具有給定名稱的新錢包
echo $walapi->create("testwallet");
Open
打開給定名稱的現(xiàn)有錢包
echo $walapi->open("testwallet");
Lock
鎖定給定名稱的現(xiàn)有錢包
echo $walapi->lock("testwallet");
Lock All
鎖定所有現(xiàn)有錢包
echo $walapi->lockAll();
Unlock
使用給定的名稱和密碼解鎖錢包
echo $ walapi-> unlock([“testwallet”邮屁,“PW5Jb8RAZP6CBjjMLPser3T8i8k9hZXZkMBJ8kb1p6f6hAg2n68jY”]);
Import Key
將私鑰導(dǎo)入給定名稱的錢包
echo $walapi->importKey(["testwallet", "5Jmsawgsp1tQ3GD6JyGCwy1dcvqKZgX6ugMVMdjirx85iv5VyPR"]);
Remove Key
從給定名稱的錢包中刪除密鑰對
echo $walapi->removeKey(["testwallet", "PW5Jb8RAZP6CBjjMLPser3T8i8k9hZXZkMBJ8kb1p6f6hAg2n68jY", "EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4"]);
Create Key
創(chuàng)建密鑰對并導(dǎo)入
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
設(shè)置錢包自動(dòng)鎖定超時(shí)(以秒為單位)
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
需要設(shè)置錢包名稱和密碼
$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
進(jìn)行交易(對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
推動(dòng)一個(gè)操作
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
創(chuàng)建密鑰對并返回
$keyPair = $eos->createKeyPair("K1");
echo "$keyPair[0], $keyPair[1]";
Tests
要運(yùn)行測試套件,只需執(zhí)行:
vendor/bin/phpunit
如果您想獲得想象并檢查代碼覆蓋率:
vendor/bin/phpunit --coverage-html tests/coverage
如果你真的很無聊菠齿,你可能想要運(yùn)行一些靜態(tài)分析:
vendor/bin/phpmetrics --report-html="tests/static" .
======================================================================
另外佑吝,如果大家覺得開源后續(xù)支持和服務(wù)有問題,也可以選擇這個(gè)商業(yè)的PHP EOS SDK 開發(fā)服務(wù)
分享一些以太坊绳匀、EOS芋忿、比特幣等區(qū)塊鏈相關(guān)的交互式在線編程實(shí)戰(zhàn)教程:
- java以太坊開發(fā)教程,主要是針對java和android程序員進(jìn)行區(qū)塊鏈以太坊開發(fā)的web3j詳解疾棵。
- python以太坊戈钢,主要是針對python工程師使用web3.py進(jìn)行區(qū)塊鏈以太坊開發(fā)的詳解。
- php以太坊是尔,主要是介紹使用php進(jìn)行智能合約開發(fā)交互殉了,進(jìn)行賬號創(chuàng)建、交易拟枚、轉(zhuǎn)賬薪铜、代幣開發(fā)以及過濾器和交易等內(nèi)容。
- 以太坊入門教程恩溅,主要介紹智能合約與dapp應(yīng)用開發(fā)隔箍,適合入門。
- 以太坊開發(fā)進(jìn)階教程脚乡,主要是介紹使用node.js鞍恢、mongodb、區(qū)塊鏈每窖、ipfs實(shí)現(xiàn)去中心化電商DApp實(shí)戰(zhàn)帮掉,適合進(jìn)階。
- C#以太坊窒典,主要講解如何使用C#開發(fā)基于.Net的以太坊應(yīng)用蟆炊,包括賬戶管理、狀態(tài)與交易瀑志、智能合約開發(fā)與交互涩搓、過濾器和交易等污秆。
- EOS教程,本課程幫助你快速入門EOS區(qū)塊鏈去中心化應(yīng)用的開發(fā)昧甘,內(nèi)容涵蓋EOS工具鏈良拼、賬戶與錢包、發(fā)行代幣充边、智能合約開發(fā)與部署庸推、使用代碼與智能合約交互等核心知識點(diǎn),最后綜合運(yùn)用各知識點(diǎn)完成一個(gè)便簽DApp的開發(fā)浇冰。
- java比特幣開發(fā)教程贬媒,本課程面向初學(xué)者,內(nèi)容即涵蓋比特幣的核心概念肘习,例如區(qū)塊鏈存儲际乘、去中心化共識機(jī)制、密鑰與腳本漂佩、交易與UTXO等脖含,同時(shí)也詳細(xì)講解如何在Java代碼中集成比特幣支持功能,例如創(chuàng)建地址投蝉、管理錢包养葵、構(gòu)造裸交易等,是Java工程師不可多得的比特幣開發(fā)學(xué)習(xí)課程墓拜。
- php比特幣開發(fā)教程港柜,本課程面向初學(xué)者请契,內(nèi)容即涵蓋比特幣的核心概念咳榜,例如區(qū)塊鏈存儲、去中心化共識機(jī)制爽锥、密鑰與腳本涌韩、交易與UTXO等,同時(shí)也詳細(xì)講解如何在Php代碼中集成比特幣支持功能氯夷,例如創(chuàng)建地址臣樱、管理錢包、構(gòu)造裸交易等腮考,是Php工程師不可多得的比特幣開發(fā)學(xué)習(xí)課程雇毫。
- tendermint區(qū)塊鏈開發(fā)詳解,本課程適合希望使用tendermint進(jìn)行區(qū)塊鏈開發(fā)的工程師踩蔚,課程內(nèi)容即包括tendermint應(yīng)用開發(fā)模型中的核心概念棚放,例如ABCI接口、默克爾樹馅闽、多版本狀態(tài)庫等飘蚯,也包括代幣發(fā)行等豐富的實(shí)操代碼馍迄,是go語言工程師快速入門區(qū)塊鏈開發(fā)的最佳選擇。
匯智網(wǎng)原創(chuàng)翻譯局骤,轉(zhuǎn)載請標(biāo)明出處攀圈。這里是原文EOS Chain/Wallet RPC API的PHP開發(fā)包