1. Go to this page and download the library: Download selaz/nem-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// Laravel/Lumen registering the service provider
$app = Laravel\Lumen\Application(realpath(__DIR__));
$app->register(NEM\ServiceProvider::class);
// 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: 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 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 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
// ------------------------------------------------------------
// 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
// ------------------------------------------------------------
$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
// ------------------------------------------------------------
$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
// --------------------------------------------------------------------------
$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)
// --------------------------------------------------------------------------
$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
// --------------------------------------------------------------------------
$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