PHP code example of xrplwin / xrpl

1. Go to this page and download the library: Download xrplwin/xrpl 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/ */

    

xrplwin / xrpl example snippets


$client = new \XRPLWin\XRPL\Client([
    # Following values are defined by default, uncomment to override
    //'endpoint_reporting_uri' => 'http://s1.ripple.com:51234',
    //'endpoint_fullhistory_uri' => 'https://xrplcluster.com'
]);

# Create new 'account_tx' method instance
$account_tx = $client->api('account_tx')->params([
    'account' => 'rAccount...',
    'limit' => 10
]);

//(optional) Override default cooldown seconds (default is 5 seconds)
$account_tx->setCooldownSeconds(10);
//(optional) Override default number of tries when request is rate-limited (default is 3)
$account_tx->setTries(5);
//(optional) Set http timeout to 3 seconds (default is 0 - eg no timeout)
//           After 3 seconds method will throw \XRPLWin\XRPL\Exceptions\BadRequestException on timeout
$account_tx->setTimeout(3);

//(optional) Define custom cooldown callback
$account_tx->setCooldownHandler(
    /**
     * @param int $current_try Current try 1 to max
     * @param int $default_cooldown_seconds Predefined cooldown seconds
     * @return void|boolean return false to stop process
     */
    function(int $current_try, int $default_cooldown_seconds) {
        //Sample usage: calculate how much sleep() is needed depending on 
        $sec = $default_cooldown_seconds * $current_try;
        if($sec > 15) return false; //force stop
        sleep($sec);
    }
);

# Send request to Ledger
try {
    $account_tx->send();
} catch (\XRPLWin\XRPL\Exceptions\XWException $e) {
    // Handle errors
    throw $e;
}

if(!$account_tx->isSuccess()) {
    //XRPL response is returned but field result.status did not return 'success'
}

# Get fetched response as array
$response       = $account_tx->resultArray(); //array response from ledger
# Get fetched response as object
$response       = $account_tx->result();      //object response from ledger
# Get fetched final result from helper (varies method to method)
$transactions   = $account_tx->finalResult(); //array of transactions

$promise = $account_tx->requestAsync();

$promises = [
    'rAcct1' => $promise,
    //...more promises
];

// Wait for the requests to complete
// Throws a ConnectException if any of the requests fail
$responses = \GuzzleHttp\Promise\Utils::unwrap($promises);

//Fill response data back into $account_tx instance
$account_tx->fill($responses['rAcct1']);

//...
$transactions = $account_tx->finalResult();



# Check if there is next page
//$has_next_page = $account_tx->hasNextPage(); //bool

# Fetch next page of transactions if there is next page (next() does not return null)
if($next_account_tx = $account_tx->next()) {
    $next_result = $next_account_tx->send()->finalResult();
    // ...
}

$account_tx = $client->api('account_tx')->params([
    'account' => 'rAccount...',
    'limit' => 10
]);

$account_tx = $client->api('tx')->params([
    'transaction' => 'DE80B0064677CEFFDE...',
    'binary' => false
]);

use XRPLWin\XRPL\Utilities\Flags;

//Methods:
Flags::extract(int $flags, string $transactionType): array
Flags::description(string $transactiontype, string $flagname, bool $htmlFormat = false): string
Flags::hasFlag(int $flags, int $check): bool

use XRPLWin\XRPL\Utilities\UNLReportFlagLedger;

UNLReportFlagLedger::isFlag(256);   //for ledger sequence 256 - true
UNLReportFlagLedger::isFlag(257);   //for ledger sequence 257 - false
UNLReportFlagLedger::prev(6873600);          //6873344
UNLReportFlagLedger::prevOrCurrent(6873600); //6873600
UNLReportFlagLedger::next(6873600);          //6873856
UNLReportFlagLedger::nextOrCurrent(6873600); //6873600

use XRPLWin\XRPL\Utilities\Util;
#Syntax: Util::currencyToSymbol(string $ISO_or_HEX, $malformedUtf8ReturnString = '?')

//ISO Currency Code
Util::currencyToSymbol('EUR') //EUR 
//Deprecated Demurrage Currency Code
Util::currencyToSymbol('0158415500000000C1F76FF6ECB0BAC600000000') //XAU (-0.5% pa)
//Nonstandard Currency Code
Util::currencyToSymbol('534F4C4F00000000000000000000000000000000') //SOLO