PHP Examples

From Nxt Wiki
(Redirected from PHP)
Jump to: navigation, search

This example simply retrieves the balance of an account, but it should be easy to modify and use any desired method listed in the The Nxt API


<?php

// This is an example of how to use the Nxt API via PHP

// All other API calls are similar.  See: http://wiki.nxtcrypto.org/wiki/The_Nxt_API

function get_guaranteed_balance($server, $accno, $number_of_confirmations)
{
    $command = "nxt?requestType=getGuaranteedBalance&account=$accno&numberOfConfirmations=$number_of_confirmations";
    $data = file_get_contents($server . $command);
    // example results: {"guaranteedBalance":2700000}
    $obj = json_decode($data);
    $guaranteed_balance = $obj->{'guaranteedBalance'};  // nxt cents
    $guaranteed_balance = floatval($guaranteed_balance) / 100.0; // nxt
    return $guaranteed_balance;
}

$server = "http://node10.nxtbase.com:7874/"; // find some servers here: http://nxtra.org/nodes/
$accno = "2488827424806206243"; 
$guaranteed_balance = get_guaranteed_balance($server, $accno, 50);

echo("Account #: $accno <br />");
echo("Guranteed Balance (>= 50 confirmations): $guaranteed_balance <br />");

?>