2017 © Pedro PelĂĄez
 

library nem-php

Composer Package for PHP (Laravel or Pure PHP), Wrapper to use the NEM blockchain REST API and SDK with NIS compatibility.

image

evias/nem-php

Composer Package for PHP (Laravel or Pure PHP), Wrapper to use the NEM blockchain REST API and SDK with NIS compatibility.

  • Monday, July 23, 2018
  • by gevs
  • Repository
  • 7 Watchers
  • 20 Stars
  • 205 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 7 Forks
  • 1 Open issues
  • 23 Versions
  • 101 % Grown

The README.md

, (*1)

Build Status Total Downloads Latest Stable Version Latest Unstable Version License , (*2)

This package aims to provide with an easy-to-use PHP Laravel Namespace helping developers to communicate with the NEM blockchain through its NIS API., (*3)

This package should be an aid to any developer working on Laravel/Lumen/Symfony applications with the NEM blockchain., (*4)

This package is currently still in development, please do not use in production., (*5)

The author of this package cannot be held responsible for any loss of money or any malintentioned usage forms of this package. Please use this package with caution., (*6)

Package licensed under MIT License., (*7)

Documentation

Reader-friendly Documentation will be added in development period and will be available on the Github Wiki at evias/nem-php Wiki., (*8)

Currently phpdocumentor is integrated to the project in order to automatically generate API documentation from the source code. You must run the phpdoc and phpdocmd command if you wish to generate the Documentation, the first stable release will include a generated Documentation version., (*9)

# 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/

Pot de vin

If you like the initiative, and for the sake of good mood, I recommend you take a few minutes to Donate a beer or Three [because belgians like that] by sending some XEM (or whatever Mosaic you think pays me a few beers someday!) to my Wallet:, (*10)

NB72EM6TTSX72O47T3GQFL345AB5WYKIDODKPPYW
Username Role
eVias Project Lead
RobertoSnap Developer

Installation

You can install this package with Composer. You only need to require evias/nem-php., (*11)

composer require evias/nem-php

The package can also be downloaded manually by cloning this repository or by downloading the packagist archive:, (*12)

Once you have required the package in your composer.json file (or using the command above), you can install the dependencies of this package:, (*13)

composer install

Unit Tests

The library provides with a Unit Test Suite for the implemented SDK features., (*14)

The unit test suite is also configured on Travis-CI with the current Build Status:, (*15)

  • Build Status

If you wish to run the unitary test suite, you can use the executable file provided by PHPUnit which is located under vendor/bin/phpunit., (*16)

Alernatively, you can create a symbolic link to this executable file in the nem-php clone root folder., (*17)

ln -s vendor/bin/phpunit 

Now you can simply run phpunit in the terminal and it will launch the Rocket.. meh, the Unit Tests Suite., (*18)

Laravel advanced features

Modify your config/app.php configuration file to include the NEM\ServiceProvider service provider. Look out for the providers configuration array and add our class as shown below:, (*19)

'providers' => [
    NEM\ServiceProvider::class,
],

If you wish to make use of the Laravel Facades provided by this library, you will also need to list the alias in your config/app.php configuration file under the aliases configuration array as described below:, (*20)

'aliases' => [
    'NemSDK' => NEM\Facades\NemSDK::class,
],

Usage / Examples

When you have installed the evias/nem-php package you will be able to use the API class to send API requests to the configured NIS. By default, the config/nem.php file defines the localhost NIS to be used, this can be changed., (*21)

If you are using Laravel or Lumen, you will need to register the Service Provider of this package into your app:, (*22)

    // Laravel/Lumen registering the service provider
    $app = Laravel\Lumen\Application(realpath(__DIR__));
    $app->register(NEM\ServiceProvider::class);

Example 1: Using the Service Provider (Laravel only)

    // 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"];

Example 2: Using the API wrapper

    // 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]);

Example 3: Sending GET/POST request to the NIS API and getting back 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));

Example 4: Custom Headers and response callback configurations

    // 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;
        }
    ]);

Example 5: Use the SDK to create NEM NIS compliant Objects

    // 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());

Example 6: Use the SDK NIS Web Service implementations

    // 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");

Example 7: Use the SDK to read an account's transactions

    // 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");

Example 8: Derive a Public Key with a hexadecimal or binary Private Key

    // 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);

Example 9: Create New KeyPair and Address (randomly)

    // 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());

Example 10: Sign Data with a NEM KeyPair

    // 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

Troubleshoot / Issues Resolution

Installing dependencies with MacOS MAMP or MAMP Pro

It may happen that your PHP server is not installed in the correct version or does not load the right extensions so here is a little snippet that will provide you with the exact PHP version needed (you also need to compile httpd)., (*23)

First you will need to rebuilt the httpd service :, (*24)

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

The above step is only because MAMP does not include the Apache build/ folder (which you can now find under ~/httpd-2.2.34/). Ok, next step is to recompile PHP as an Apache Module as required by MAMP. Following snippet will let you download and compile the PHP package with MacOS and MAMP, enabling GMP, GD, MySQL, XML, CURL, GETTEXT, MBSTRING and BCMATH modules., (*25)

# 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

After building the PHP source code you will also need to link to that file in your system. Following is an easy workaround, but please, make sure to backup any file before you overwrite executables., (*26)

The compilation of PHP will have installed a file: ~/httpd-2.2.34/modules/libphp7.so, you must link your system to that file in order to use the correct PHP modules. Use following snippet to link both, the PHP apache module and the PHP executable in your MAMP installation., (*27)

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

Changelog

Important versions listed below. Refer to the Changelog for a full history of the project., (*28)

License

This software is released under the MIT License., (*29)

© 2017-2018 Grégory Saive greg@evias.be, All rights reserved., (*30)

The Versions

23/07 2018

dev-master

9999999-dev

Composer Package for PHP (Laravel or Pure PHP), Wrapper to use the NEM blockchain REST API and SDK with NIS compatibility.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregory Saive
by Robin Pedersen

laravel api framework sdk restful cryptocurrency nem laravel-nem evias gregory saive nem-php php-nem

30/04 2018

v0.2.11

0.2.11.0

Composer Package for PHP (Laravel or Pure PHP), Wrapper to use the NEM blockchain REST API and SDK with NIS compatibility.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregory Saive
by Robin Pedersen

laravel api framework sdk restful cryptocurrency nem laravel-nem evias gregory saive nem-php php-nem

10/04 2018

dev-dev-aes_encrypt

dev-dev-aes_encrypt

Composer Package for PHP (Laravel or Pure PHP), Wrapper to use the NEM blockchain REST API and SDK with NIS compatibility.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregory Saive
by Robin Pedersen

laravel api framework sdk restful cryptocurrency nem laravel-nem evias gregory saive nem-php php-nem

20/03 2018

dev-dev-services

dev-dev-services

Composer Package for PHP (Laravel or Pure PHP), Wrapper to use the NEM blockchain REST API and SDK with NIS compatibility.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregory Saive
by Robin Pedersen

laravel api framework sdk restful cryptocurrency nem laravel-nem evias gregory saive nem-php php-nem

20/03 2018

v0.2.10

0.2.10.0

Composer Package for PHP (Laravel or Pure PHP), Wrapper to use the NEM blockchain REST API and SDK with NIS compatibility.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregory Saive
by Robin Pedersen

laravel api framework sdk restful cryptocurrency nem laravel-nem evias gregory saive nem-php php-nem

07/03 2018

v0.2.9

0.2.9.0

Composer Package for PHP (Laravel or Pure PHP), Wrapper to use the NEM blockchain REST API and SDK with NIS compatibility.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregory Saive
by Robin Pedersen

laravel api framework sdk restful cryptocurrency nem laravel-nem evias gregory saive nem-php php-nem

28/02 2018

v0.2.8

0.2.8.0

Composer Package for PHP (Laravel or Pure PHP), Wrapper to use the NEM blockchain REST API and SDK with NIS compatibility.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregory Saive
by Robin Pedersen

laravel api framework sdk restful cryptocurrency nem laravel-nem evias gregory saive nem-php php-nem

26/02 2018

v0.2.7

0.2.7.0

Composer Package for PHP (Laravel or Pure PHP), Wrapper to use the NEM blockchain REST API and SDK with NIS compatibility.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregory Saive
by Robin Pedersen

laravel api framework sdk restful cryptocurrency nem laravel-nem evias gregory saive nem-php php-nem

24/02 2018

v0.2.6

0.2.6.0

Composer Package for PHP (Laravel or Pure PHP), Wrapper to use the NEM blockchain REST API and SDK with NIS compatibility.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregory Saive
by Robin Pedersen

laravel api framework sdk restful cryptocurrency nem laravel-nem evias gregory saive nem-php php-nem

23/02 2018

v0.2.5

0.2.5.0

Composer Package for PHP (Laravel or Pure PHP), Wrapper to use the NEM blockchain REST API and SDK with NIS compatibility.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregory Saive
by Robin Pedersen

laravel api framework sdk restful cryptocurrency nem laravel-nem evias gregory saive nem-php php-nem

22/02 2018

v0.2.4

0.2.4.0

Composer Package for PHP (Laravel or Pure PHP), Wrapper to use the NEM blockchain REST API and SDK with NIS compatibility.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregory Saive
by Robin Pedersen

laravel api framework sdk restful cryptocurrency nem laravel-nem evias gregory saive nem-php php-nem

20/02 2018

v0.2.3

0.2.3.0

Composer Package for PHP (Laravel or Pure PHP), Wrapper to use the NEM blockchain REST API and SDK with NIS compatibility.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregory Saive
by Robin Pedersen

laravel api framework sdk restful cryptocurrency nem laravel-nem evias gregory saive nem-php php-nem

17/02 2018

v0.2.2

0.2.2.0

Composer Package for PHP (Laravel or Pure PHP), Wrapper to use the NEM blockchain REST API and SDK with NIS compatibility.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregory Saive
by Robin Pedersen

laravel api framework sdk restful cryptocurrency nem laravel-nem evias gregory saive nem-php php-nem

16/02 2018

v0.2.1

0.2.1.0

Composer Package for PHP (Laravel or Pure PHP), Wrapper to use the NEM blockchain REST API and SDK with NIS compatibility.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregory Saive
by Robin Pedersen

laravel api framework sdk restful cryptocurrency nem laravel-nem evias gregory saive nem-php php-nem

16/02 2018

v0.2.0

0.2.0.0

Composer Package for PHP (Laravel or Pure PHP), Wrapper to use the NEM blockchain REST API and SDK with NIS compatibility.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregory Saive
by Robin Pedersen

laravel api framework sdk restful cryptocurrency nem laravel-nem evias gregory saive nem-php php-nem

22/01 2018

dev-dev-1.0.0

dev-dev-1.0.0

Composer Package for PHP (Laravel or Pure PHP), Wrapper to use the NEM blockchain REST API and SDK with NIS compatibility.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregory Saive
by Robin Pedersen

laravel api framework sdk restful cryptocurrency nem laravel-nem evias gregory saive nem-php php-nem

09/01 2018

v0.1.5

0.1.5.0

Composer Package for PHP (Laravel or Pure PHP), Wrapper to use the NEM blockchain REST API and SDK with NIS compatibility.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregory Saive
by Robin Pedersen

laravel api framework sdk restful cryptocurrency nem laravel-nem evias gregory saive nem-php php-nem

28/12 2017

v0.1.4

0.1.4.0

Composer Package for PHP (Laravel), Wrapper to use the NEM blockchain REST API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregory Saive
by Robin Pedersen

laravel api framework restful cryptocurrency nem laravel-nem evias gregory saive

15/12 2017

v0.1.3

0.1.3.0

Composer Package for PHP (Laravel), Wrapper to use the NEM blockchain REST API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregory Saive
by Robin Pedersen

laravel api framework restful cryptocurrency nem laravel-nem evias gregory saive

15/12 2017

v0.1.2

0.1.2.0

Composer Package for PHP (Laravel), Wrapper to use the NEM blockchain REST API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregory Saive
by Robin Pedersen

laravel api framework restful cryptocurrency nem laravel-nem evias gregory saive

12/12 2017

v0.1.1

0.1.1.0

Composer Package for PHP (Laravel), Wrapper to use the NEM blockchain REST API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregory Saive
by Robin Pedersen

laravel api framework restful cryptocurrency nem laravel-nem evias gregory saive

18/02 2017

v0.0.2

0.0.2.0

Composer Package for PHP (Laravel), Wrapper to use the NEM blockchain REST API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregory Saive

laravel api framework restful cryptocurrency nem laravel-nem evias gregory saive

05/02 2017

v0.0.1

0.0.1.0

Composer Package for PHP (Laravel), Wrapper to use NEM blockchain REST API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregory Saive

laravel api framework restful cryptocurrency nem laravel-nem evias gregory saive