一個EOS區塊鏈RPC API介面的PHP SDK包
作為我們Block Producer對社群利益的承諾的一部分,我們希望專注於構建有助於提高EOS平臺採用率的工具/應用程式。與大多數大型應用程式一樣,當你只有少量或過時的文件並且缺乏工具來幫助你進行入門學習時,可能就會存在很高的進入門檻。
為了節省剛入門的EOS開發人員一些時間,我們整理了一個PHP SDK,它涵蓋了大部分EOS的API方法。在文件中,每個呼叫都有一些示例,其中包含API的實際輸出。
無論你是否使用PHP,我們都希望這將是一個有用的參考點,可以開始構建你的第一個應用程式,或者只是瞭解EOS區塊鏈API提供的內容也好。
你可以檢視EOS官方文件,但要注意缺少一些較新的方法。此外,這些文件中的一些示例使用了過時的語法。
安裝
composer require block-matrix-network/php-eos-rpc-sdk
設定
使用你喜歡的RPC API主機在專案根目錄中建立一個dotenv.env
檔案。你可以使用.env.example
作為模板:
cp .env.example .env
用法
有一個工廠方法來自動例項化所有依賴項:
$api = (new ChainFactory)->api();
示例
為了幫助你入門,有一個簡單的示例執行器,它涵蓋了所有API命令。
只需通過cli執行此命令即可檢視所有命令的示例輸出:
cd examples php chain.php
API方法
涵蓋了所有隻讀的EOS區塊鏈API方法。下面舉幾個例子:
getInfo 獲取資訊
獲取與節點相關的最新資訊。
echo $api->getInfo(); { "server_version": "db031363", "chain_id": "aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906", "head_block_num": 1380988, "last_irreversible_block_num": 1380657, "last_irreversible_block_id": "0015113163cbe7676c4e56d1758a1ce95e47fa645827b9202de5753031d36b8b", "head_block_id": "0015127c94676db3da55ec66210952db7f4db35b0e731abefff1562c201a0666", "head_block_time": "2018-06-18T15:38:45.000", "head_block_producer": "eoscannonchn", "virtual_block_cpu_limit": 200000000, "virtual_block_net_limit": 1048576000, "block_cpu_limit": 199900, "block_net_limit": 1048576 }
getBlock 獲取塊資訊
獲取與塊相關的資訊。
echo $api->getBlock("1337"); { "timestamp": "2018-06-09T12:09:21.500", "producer": "eosio", "confirmed": 0, "previous": "00000538b374c1cbfaeed7253ad3075ddc72a28f0a0515301fc1bbed675f2316", "transaction_mroot": "0000000000000000000000000000000000000000000000000000000000000000", "action_mroot": "bcb9763baa3bbf98ed36379b4be0ecb2d9cd21c75df01729c63b2b021001c10c", "schedule_version": 0, "new_producers": null, "header_extensions": [ ], "producer_signature": "SIG_K1_K5jWf36t6j454Hb2fGuV37YTwMTvuQ51ZPBtpru8Ud2axtMTEauWyvtpJuTpnvqzReUndDgEDXvoeEP4jdj2bpnYKBt6g2", "transactions": [ ], "block_extensions": [ ], "id": "00000539d17a03af7126e073be4c4d99a72b7f58793cf2c87b9bfd41b6c711fb", "block_num": 1337, "ref_block_prefix": 1944069745 }
getCode 獲取程式碼
獲取智慧合約程式碼。
echo $api->getCode("eosio.token"); { "account_name": "eosio.token", "code_hash": "3e0cf4172ab025f9fff5f1db11ee8a34d44779492e1d668ae1dc2d129e865348", "wast": "(module\n(type $0 (func (param i32 i64 i32)))\n(type $1 (func (param i32 i64 i64 i32 i32)))\n(type $2 (func (param i32 i64 i32 i32)))\n(type $3 (func ))\n(type $4 (func(result i64)))\n(type $5 (func (param i64 i64)))\n(type $6 (func (param i64)))\n(type $7 (func (param i32 i32)))\n(type $8 (func (param i64 i64 i64 i64) (result i32)))\n(type $9 (func (param i64 i64 i64 i64 i32 i32) (result i32)))\n(type $10 (func (param i32 i32 i32) (result i32)))\n(type $11 (func (param i64) (result i32)))\n(type $12 (func (param i32)))\n(type $13 (func(result i32)))\n(type $14 (func (param i32 i32) (result i32)))\n(type $15 (func (param i32) (result i32)))\n(type $16 (func (param i32 i32 i32 i32)))\n(type $17 (func (param i32 i64 i32 i64)))\n(type $18 (func (param i64 i64 i32 i32)))\n(type $19 (func (param i32 i64 i32) (result i32)))\n(type $20 (func (param i64 i64 i64)))\n(import \"env\" \"abort\" (func $23 ))\n(import \"env\" \"action_data_size\" (func $24(result i32)))\n(import \"env\" \"current_receiver\" (func $25(result i64)))\n(import \"env\" \"current_time\" (func $26(result i64)))\n(import \"env\" \"db_find_i64\" (func $27 (param i64 i64 i64 i64) (result i32)))\n...... (export \"_ZN5eosio5token5issueEyNS_5assetENSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE\" (func $51))\n(export \"_ZN5eosio5token11add_balanceEyNS_5assetEy\" (func $52))\n...........$block2\nget_local $3\ni32.const 12\ni32.add\ntee_local $3\nget_local $1\ni32.lt_u\nbr_if $loop\nend ;; $loop\nend ;; $block1\nreturn\nend ;; $block\nget_local $0\ni32.const -4\ni32.add\ntee_local $3\nget_local $3\ni32.load\ni32.const 2147483647\ni32.and\ni32.store\n)\n\n(func $87\nunreachable\n))", "wasm": "", "abi": { "version": "eosio::abi\/1.0", "types": [ { "new_type_name": "account_name", "type": "name" } ], "structs": [ { "name": "transfer", "base": "", "fields": [ { "name": "from", "type": "account_name" }, { "name": "to", "type": "account_name" }, { "name": "quantity", "type": "asset" }, { "name": "memo", "type": "string" } ] }, { "name": "create", "base": "", "fields": [ { "name": "issuer", "type": "account_name" }, { "name": "maximum_supply", "type": "asset" } ] }, { "name": "issue", "base": "", "fields": [ { "name": "to", "type": "account_name" }, { "name": "quantity", "type": "asset" }, { "name": "memo", "type": "string" } ] }, { "name": "account", "base": "", "fields": [ { "name": "balance", "type": "asset" } ] }, { "name": "currency_stats", "base": "", "fields": [ { "name": "supply", "type": "asset" }, { "name": "max_supply", "type": "asset" }, { "name": "issuer", "type": "account_name" } ] } ], "actions": [ { "name": "transfer", "type": "transfer", "ricardian_contract": "## Transfer Terms & Conditions\n\nI, {{from}}, certify the following to be true to the best of my knowledge:\n\n1. I certify that {{quantity}} is not the proceeds of fraudulent or violent activities.\n2. I certify that, to the best of my knowledge, {{to}} is not supporting initiation of violence against others.\n3. I have disclosed any contractual terms & conditions with respect to {{quantity}} to {{to}}.\n\nI understand that funds transfers are not reversible after the {{transaction.delay}} seconds or other delay as configured by {{from}}'s permissions.\n\nIf this action fails to be irreversibly confirmed after receiving goods or services from '{{to}}', I agree to either return the goods or services or resend {{quantity}} in a timely manner.\n" }, { "name": "issue", "type": "issue", "ricardian_contract": "" }, { "name": "create", "type": "create", "ricardian_contract": "" } ], "tables": [ { "name": "accounts", "index_type": "i64", "key_names": [ "currency" ], "key_types": [ "uint64" ], "type": "account" }, { "name": "stat", "index_type": "i64", "key_names": [ "currency" ], "key_types": [ "uint64" ], "type": "currency_stats" } ], "ricardian_clauses": [], "error_messages": [], "abi_extensions": [] } }
GetCurrencyBalance 獲得貨幣餘額
獲取帳戶的貨幣餘額。
echo $api->getCurrencyBalance("eosio.token", "eosdacserver"); [ "2453.2054 EOS" ]
測試
要執行測試套件,只需執行:
vendor/bin/phpunit
更多內容檢視ofollow,noindex">github 資料庫 。
======================================================================
分享一些以太坊、EOS、比特幣等區塊鏈相關的互動式線上程式設計實戰教程:
- EOS教程,本課程幫助你快速入門EOS區塊鏈去中心化應用的開發,內容涵蓋EOS工具鏈、賬戶與錢包、發行代幣、智慧合約開發與部署、使用程式碼與智慧合約互動等核心知識點,最後綜合運用各知識點完成一個便籤DApp的開發。
- java以太坊開發教程,主要是針對java和android程式設計師進行區塊鏈以太坊開發的web3j詳解。
- python以太坊,主要是針對python工程師使用web3.py進行區塊鏈以太坊開發的詳解。
- php以太坊,主要是介紹使用php進行智慧合約開發互動,進行賬號建立、交易、轉賬、代幣開發以及過濾器和交易等內容。
- 以太坊入門教程,主要介紹智慧合約與dapp應用開發,適合入門。
- 以太坊開發進階教程,主要是介紹使用node.js、mongodb、區塊鏈、ipfs實現去中心化電商DApp實戰,適合進階。
- C#以太坊,主要講解如何使用C#開發基於.Net的以太坊應用,包括賬戶管理、狀態與交易、智慧合約開發與互動、過濾器和交易等。
- java比特幣開發教程,本課程面向初學者,內容即涵蓋比特幣的核心概念,例如區塊鏈儲存、去中心化共識機制、金鑰與指令碼、交易與UTXO等,同時也詳細講解如何在Java程式碼中整合比特幣支援功能,例如建立地址、管理錢包、構造裸交易等,是Java工程師不可多得的比特幣開發學習課程。
- php比特幣開發教程,本課程面向初學者,內容即涵蓋比特幣的核心概念,例如區塊鏈儲存、去中心化共識機制、金鑰與指令碼、交易與UTXO等,同時也詳細講解如何在Php程式碼中整合比特幣支援功能,例如建立地址、管理錢包、構造裸交易等,是Php工程師不可多得的比特幣開發學習課程。
- tendermint區塊鏈開發詳解 ,本課程適合希望使用tendermint進行區塊鏈開發的工程師,課程內容即包括tendermint應用開發模型中的核心概念,例如ABCI介面、默克爾樹、多版本狀態庫等,也包括代幣發行等豐富的實操程式碼,是go語言工程師快速入門區塊鏈開發的最佳選擇。
匯智網原創翻譯,轉載請標明出處。這裡是原文一個EOS區塊鏈RPC API介面的PHP SDK包