Supercharge your PHP application with XRPL

zgrguric

Z. Grgurić

Posted on May 5, 2023

Supercharge your PHP application with XRPL

The XRP Ledger (XRPL) is a decentralized, public blockchain led by a global developer community. This article will showcase how to implement XRPL communication natively in your PHP8 application.

Prerequisites

To get started you need to have understanding how to install composer package. We won't cover basic PHP installation and environment setup at this time.

We will be using terminal in this article with php command to run scripts below.

For this tutorial, we assume you have:

  • basic knowledge of PHP and terminal
  • composer installed on your machine
  • PHP^8.1 installed on your machine

Project setup

Include XRPL API package

composer require xrplwin/xrpl
Enter fullscreen mode Exit fullscreen mode

index.php
To run sample application create index.php and run it in terminal, just to check if everything is working.

<?php
require __DIR__.'/vendor/autoload.php';

echo 'Hello world';
Enter fullscreen mode Exit fullscreen mode

run in terminal:

php ./index.php
Enter fullscreen mode Exit fullscreen mode

should output:

Hello world
Enter fullscreen mode Exit fullscreen mode

Now the fun begins!

XRPL Client setup

index.php

To init XRPL Client add line below. If you like to change endpoints refer to documentation.

<?php
require __DIR__.'/vendor/autoload.php';

$client = new \XRPLWin\XRPL\Client([]);

Enter fullscreen mode Exit fullscreen mode

Prepare XRPL Method

index.php (part)

Prepare XRPL API method (see docs) with parameters. In this example we will try to run account_info query described here.

...

$account_info = $client->api('account_info')->params([
  'account' => 'rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn',
  'strict' => true,
  'ledger_index' => 'current',
  'queue' => true
]);
Enter fullscreen mode Exit fullscreen mode

Send request to XRPLedger

index.php (part)

Now when we prepared XRPL method it is time to actually send request to XRPL and get results. Within catch handle errors like: machine does not have internet connection, XRPL endpoint is offline, XRPL endpoint returned non 200 or 503 HTTP status code.

...

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

Check if response is success and get result

index.php (part)

...

if(!$account_info->isSuccess()) {
  //XRPL response is returned but field result.status
  //did not return 'success', we will print returned error message
  echo $account_info->result()->result->error_message;
  return;
}

//Everything OK
$result = $account_info->finalResult();
print_r($result);
Enter fullscreen mode Exit fullscreen mode

should output:

stdClass Object
(
    [Account] => rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn
    [Balance] => 59759978
    [Flags] => 1048576
    [LedgerEntryType] => AccountRoot
    [MessageKey] => 0200000000000000000000000038901D3A772963CF12FF7C0E010FE350B6CCC45D 
    [OwnerCount] => 0
    [PreviousTxnID] => 8A5E55F36D8131A322F2206EAE7DD29C36F7CFFECB59B9CB6C7B478B88AC13F3
    [PreviousTxnLgrSeq] => 78523455
    [RegularKey] => rhLkGGNZdjSpnHJw4XAFw1Jy7PD8TqxoET
    [Sequence] => 192221
    [index] => 92FA6A9FC8EA6018D5D16532D7795C91BFB0831355BDFDA177E86C8BF997985F        
)
Enter fullscreen mode Exit fullscreen mode

Full index.php

<?php
require __DIR__.'/vendor/autoload.php';

$client = new \XRPLWin\XRPL\Client([]);

$account_info = $client->api('account_info')->params([
  'account' => 'rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn',
  'strict' => true,
  'ledger_index' => 'current',
  'queue' => true
]);

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


if(!$account_info->isSuccess()) {
  //XRPL response is returned but field result.status
  //did not return 'success', we will print returned error message
  echo $account_info->result()->result->error_message;
  return;
}

//Everything OK
$result = $account_info->finalResult();
print_r($result);
Enter fullscreen mode Exit fullscreen mode

Conclusion

With few lines of code you have successfully queried XRPL JSON-RPC endpoint and fetched result. With XRPLWin/XRPL API you can query any XRPL method available, fetch results and use them in your PHP app.

To find out advanced features this API provides like Promises, asynchronous requests, pagination, cooldown rate limiting, endpoint changing, read more in the Github project.

Do you like this tutorial? Give us 🌟 on Github!


Star on Github | Follow on Twitter

💖 💪 🙅 🚩
zgrguric
Z. Grgurić

Posted on May 5, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related