selaz / nem-php
PHP (Laravel或纯PHP)的Composer包,用于使用与NIS兼容的NEM区块链REST API和SDK的包装器。
Requires
- php: >=5.5.9
- christian-riesen/base32: ^1.3
- guzzlehttp/guzzle: >5
- illuminate/contracts: >5.0.33
- illuminate/support: >5.0.33
- kornrunner/keccak: ^1.1
- mdanter/ecc: *
- paragonie/sodium_compat: ^1.4
Requires (Dev)
- phpstan/phpstan: ^1.10
This package is not auto-updated.
Last update: 2024-09-18 14:46:03 UTC
README
本包旨在为开发者提供一个易于使用的PHP Laravel命名空间,帮助他们通过NIS API与NEM区块链进行通信。
本包应有助于任何正在使用Laravel/Lumen/Symfony应用和NEM区块链的的开发者。
本包目前仍在开发中,请勿在生产环境中使用。
本包的作者不承担任何因金钱损失或恶意使用本包而产生的责任。请谨慎使用本包。
本包遵循MIT许可证。
文档
在开发期间将添加易于阅读的文档,并将可在GitHub Wiki(evias/nem-php Wiki)上找到。
目前已将phpdocumentor集成到项目中,以自动从源代码生成API文档。如果您希望生成文档,必须运行phpdoc
和phpdocmd
命令,首个稳定版本将包括生成的文档版本。
# First build the API documentation ./vendor/bin/phpdoc -d src/ -t build/ --template="xml" # Then generate the Markdown ./vendor/bin/phpdocmd build/structure.xml docs/
回报
如果您喜欢这个项目,出于好心情,我建议您花几分钟时间捐赠啤酒或三瓶[因为比利时人喜欢这样],通过发送一些XEM(或您认为将来能支付我几瓶啤酒的任何Mosaic)到我的钱包。
NB72EM6TTSX72O47T3GQFL345AB5WYKIDODKPPYW
安装
您可以使用Composer安装此包。您只需要要求evias/nem-php。
composer require evias/nem-php
您也可以通过克隆此仓库或下载Packagist存档手动下载此包。
一旦您在composer.json
文件中(或使用上述命令)要求了此包,您就可以安装此包的依赖项。
composer install
单元测试
该库提供了一套单元测试,用于实现SDK功能。
单元测试套件已在Travis-CI上配置,并显示当前的构建状态。
如果您希望运行单元测试套件,可以使用PHPUnit提供的可执行文件,该文件位于vendor/bin/phpunit
下。
或者,您可以在nem-php
克隆根目录中创建此可执行文件的符号链接。
ln -s vendor/bin/phpunit
现在您可以在终端中简单地运行phpunit
,它将启动Rocket.. 嘛,单元测试套件。
Laravel高级功能
修改您的config/app.php
配置文件以包含NEM\ServiceProvider服务提供者。注意providers
配置数组,并按以下所示添加我们的类
'providers' => [ NEM\ServiceProvider::class, ],
如果您想使用此库提供的Laravel Facades,您还需要在config/app.php
配置文件中的aliases
配置数组中列出别名,如下所述
'aliases' => [ 'NemSDK' => NEM\Facades\NemSDK::class, ],
使用/示例
当您安装了evias/nem-php包后,您将能够使用API类向配置的NIS发送API请求。默认情况下,config/nem.php文件定义了要使用的localhost NIS,这可以更改。
如果您使用Laravel或Lumen,您需要将此包的服务提供者注册到您的应用中
// Laravel/Lumen registering the service provider $app = Laravel\Lumen\Application(realpath(__DIR__)); $app->register(NEM\ServiceProvider::class);
示例1:使用服务提供者(仅限Laravel)
// Example 1: Using the Service Provider // -------------------------------------- // The Service Provider for Laravel/Lumen will bind "nem.config", // "nem" and "nem.ncc" in the IoC. "nem" and "nem.ncc" are pre- // configured instances of the API class using APP_ENV for the environment. $nemConfig = $app["nem.config"] $nemAPI = $app["nem"]; $nccAPI = $app["nem.ncc"];
示例2:使用API包装器
// Example 2: Instantiating the API class // -------------------------------------- // You can also create a new instance of the API $nemAPI = new NEM\API(); $nemAPI->setOptions([ "protocol" => "http", "use_ssl" => false, "host" => "go.nem.ninja", "port" => 7890, "endpoint" => "/", ]); // If you wish you can define your own RequestHandler, have a look at the // NEM\Contracts\RequestHandler interface. $nemAPI->setOptions(["handler_class" => Path\To\My\Handler::class]);
示例 3:向 NIS API 发送 GET/POST 请求并获取 JSON
// Example 3: Sending GET/POST JSON requests // ----------------------------------------- // The API wrapper class can be used to send API requests to the // configured NIS host with following snippet: $response = $nemAPI->getJSON("heartbeat", ""); // sending JSON through POST and receiving JSON back. $postData = ["myField" => "hasThisValue", "yourField" => "isNotEmpty"]; $response = $nemAPI->postJSON("post/endpoint", json_encode($postData));
示例 4:自定义头部和响应回调配置
// Example 4: Custom headers and Callback configuration // ----------------------------------------------------- // The 3rd parameter of the get() and post() methods lets you pass // an options array to the RequestHandler. To add specific headers for // example you would do as follows: $response = $nemAPI->getJSON("hearbeat", "", ["headers" => ["Content-Type" => "text/xml"]]); // You may also define onSuccess, onError and onReject callbacks to be executed // when the Guzzle Promises respectively complete, encounter an error or are denied. // @see Psr\Http\Message\ResponseInterface // @see GuzzleHttp\Exception\RequestException $response = $nemAPI->getJSON("heartbeat", "", [ "onSuccess" => function(ResponseInterface $response) { echo $response->getBody(); }, "onError" => function(RequestException $exception) { echo "This is bad: " . $exception->getMessage(); }, "onReject" => function($reason) { echo "Request could not be completed: " . $reason; } ]);
示例 5:使用 SDK 创建符合 NIS 规范的 NEM 对象
// Example 5: Use the SDK to create NEM *NIS compliant* Objects // ------------------------------------------------------------ // You can create an instance and pass the *connection configuration* $sdk = new NEM\SDK([ "protocol" => "http", "use_ssl" => false, "host" => "go.nem.ninja", "port" => 7890, "endpoint" => "/", ]); // Or you can use an already initialized API client $sdk = new NEM\SDK([], new NEM\API()); $account = $sdk->models()->account(["address" => "TDWZ55R5VIHSH5WWK6CEGAIP7D35XVFZ3RU2S5UQ"]); // The \NEM\Contracts\DataTransferObject interface tells us that Models // always have a toDTO() method which will format the said object into // its *NIS compliant* object. // Dump [AccountMetaDataPair](https://bob.nem.ninja/docs/#accountMetaDataPair) object var_dump($account->toDTO());
示例 6:使用 SDK 的 NIS 网络服务实现
// Example 6: Use the SDK NIS Web Service implementations // ------------------------------------------------------------ $sdk = new NEM\SDK(); $service = $sdk->account(); // Generate a new account $account = $service->generateAccount(); // $account is an instance of \NEM\Models\Account // Read account data *from the NEM blockchain* using the Address $account = $service->getFromAddress("TDWZ55R5VIHSH5WWK6CEGAIP7D35XVFZ3RU2S5UQ"); // Read account data *from the NEM blockchain* using the Public Key $account = $service->getFromPublicKey("d90c08cfbbf918d9304ddd45f6432564c390a5facff3df17ed5c096c4ccf0d04");
示例 7:使用 SDK 读取账户的交易
// Example 7: Use the SDK to read an account's transactions // ------------------------------------------------------------ $sdk = new NEM\SDK(); $service = $sdk->account(); // Get incoming transaction for an account by its address // $incomings will be an instance of \NEM\Models\ModelCollection $incomings = $service->incomingTransactions("TDWZ55R5VIHSH5WWK6CEGAIP7D35XVFZ3RU2S5UQ"); // Get outgoing transaction for an account by its address $outgoings = $service->outgoingTransactions("TDWZ55R5VIHSH5WWK6CEGAIP7D35XVFZ3RU2S5UQ"); // Get unconfirmed transaction for an account by its address $unconfirmed = $service->unconfirmedTransactions("TDWZ55R5VIHSH5WWK6CEGAIP7D35XVFZ3RU2S5UQ");
示例 8:使用十六进制或二进制私钥推导公钥
// Example 8: Derive a Public Key with a hexadecimal or binary Private Key // -------------------------------------------------------------------------- $privateKey = "e77c84331edbfa3d209c4e68809c98a634ad6e8891e4174455c33be9dd25fce5"; $publicKey = "d90c08cfbbf918d9304ddd45f6432564c390a5facff3df17ed5c096c4ccf0d04"; $keypair = new NEM\Core\KeyPair($privateKey); var_dump($keypair->getPublicKey("hex")); // will output: // Create with *provided public key* (no derivation - faster) $keypair = new NEM\Core\KeyPair($privateKey, $publicKey);
示例 9:创建新的密钥对和地址(随机生成)
// Example 9: Create New KeyPair and Address (randomly) // -------------------------------------------------------------------------- $keypair = new NEM\Core\KeyPair(); $address = NEM\Models\Address::fromPublicKey($keypair->getPublicKey()); var_dump($keypair->getPrivateKey("hex"), $address->toClean());
示例 10:使用 NEM 密钥对签名数据
// Example 10: Sign Data with a NEM KeyPair // -------------------------------------------------------------------------- $keypair = new NEM\Core\KeyPair("abf4cf55a2b3f742d7543d9cc17f50447b969e6e06f5ea9195d428ab12b7318d"); var_dump($keypair->sign("nem-php by eVias!")->getHex()); // this will show you the following hexadecimal signature: // 177908f0cb5e56a0da11bfc3b38f6d749c4c870c9b356313db6460925e4584a9304e6aa1a5ba50ec2f773bbdfbfc03285a35d986d056df27b7d05a74f6c9b501 // you can now use this signature and verify it using the Public Key
故障排除/问题解决
使用 MacOS MAMP 或 MAMP Pro 安装依赖项
可能发生的情况是您的 PHP 服务器没有安装在正确的版本,或者没有加载正确的扩展。以下是一个小片段,将为您提供所需的准确 PHP 版本(您还需要编译 httpd)。
首先,您需要重新构建 httpd 服务
mkdir ~/httpd-2.2.34/ # Download httpd source code cd ~/Downloads wgets http://apache.belnet.be//httpd/httpd-2.2.34.tar.bz2 tar xvzf httpd-2.2.34.tar.bz2 # We now have the HTTPD source code unarchived cd httpd-2.2.34 ./configure make make install
上述步骤仅因为 MAMP 不包含 Apache build/文件夹(您现在可以在 ~/httpd-2.2.34/
下找到它)。好的,下一步是重新编译 PHP,作为 MAMP 所需的 Apache 模块。以下片段将使您能够使用 MacOS 和 MAMP 下载并编译 PHP 软件包,并启用 GMP、GD、MySQL、XML、CURL、GETTEXT、MBSTRING 和 BCMATH 模块。
# This is where the *built* PHP will be installed. mkdir ~/php-7.1.8/ # Download php source code cd ~/Downloads wgets http://de2.php.net/get/php-7.1.8.tar.bz2/from/this/mirror tar xvzf php-7.1.8.tar.bz2 # We now have the PHP source code unarchived cd php-7.1.8 # MacOS brew install intltool icu4c gettext brew link icu4c gettext ./configure --with-apxs2=/Applications/MAMP/Library/bin/apxs --prefix=/Users/greg/php-7.1.8 --enable-intl --with-gmp --with-xmlrpc --enable-bcmath --with-curl=/usr --with-gettext=/usr/local/Cellar/gettext/ --with-gd --with-pdo-mysql --with-openssl=/usr/local/Cellar/openssl/1.0.2n/ --enable-mbstring make make install # End-MacOS # Linux ./configure --prefix=/home/greg/php-7.1.8 --enable-intl --with-gmp --with-xmlrpc --enable-bcmath --with-curl=/usr --with-gettext --with-gd --with-pdo-mysql --with-openssl make make install # End-Linux
在构建 PHP 源代码后,您还需要将系统链接到该文件。以下是一个简单的解决方案,但 请 在覆盖可执行文件之前,确保备份任何文件。
PHP 的编译将安装一个文件:~/httpd-2.2.34/modules/libphp7.so
,您必须将系统链接到该文件,以便使用正确的 PHP 模块。使用以下片段链接 PHP Apache 模块和 MAMP 安装中的 PHP 可执行文件。
mkdir /Applications/MAMP/Library/build
cp -R /Users/greg/httpd-2.2.34/build/* /Applications/MAMP/Library/build/
sudo ln -s /Users/greg/httpd-2.2.34/modules/libphp7.so /Applications/MAMP/bin/php/php7.1.8/modules/
sudo ln -s /Users/greg/php-7.1.8/bin/php /usr/local/bin/php
变更日志
以下列出重要版本。请参阅 变更日志,以了解项目的完整历史。
许可协议
本软件根据 MIT 许可协议发布。
© 2017-2018 Grégory Saive greg@evias.be,版权所有。