介紹一個 EOS 區塊鏈 RPC API 介面的 PHP SDK 包

geeker發表於2018-11-19

作為我們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\n          get_local $3\n          i32.const 12\n          i32.add\n          tee_local $3\n          get_local $1\n          i32.lt_u\n          br_if $loop\n        end ;; $loop\n      end ;; $block1\n      return\n    end ;; $block\n    get_local $0\n    i32.const -4\n    i32.add\n    tee_local $3\n    get_local $3\n    i32.load\n    i32.const 2147483647\n    i32.and\n    i32.store\n    )\n  \n  (func $87\n    unreachable\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

更多內容檢視github 資料庫

安利幾個以太坊、EOS、比特幣等區塊鏈相關的互動式線上程式設計實戰教程:

EOS教程,本課程幫助你快速入門EOS區塊鏈去中心化應用的開發,內容涵蓋EOS工具鏈、賬戶與錢包、發行代幣、智慧合約開發與部署、使用程式碼與智慧合約互動等核心知識點,最後綜合運用各知識點完成一個便籤DApp的開發。
php以太坊,主要是介紹使用php進行智慧合約開發互動,進行賬號建立、交易、轉賬、代幣開發以及過濾器和交易等內容。
php比特幣開發教程,本課程面向初學者,內容即涵蓋比特幣的核心概念,例如區塊鏈儲存、去中心化共識機制、金鑰與指令碼、交易與UTXO等,同時也詳細講解如何在Php程式碼中整合比特幣支援功能,例如建立地址、管理錢包、構造裸交易等,是Php工程師不可多得的比特幣開發學習課程。

這裡是原文一個EOS區塊鏈RPC API介面的PHP SDK包

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章