为了账号安全,请及时绑定邮箱和手机立即绑定

一个EOS区块链RPC API接口的PHP SDK包

标签:
PHP

作为我们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



作者:编程狂魔
链接:https://www.jianshu.com/p/b93710b88edd


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消