Supercharge your PHP application with XRPL
Tags: php, xrpl, webdev, programming
Published: 2023-05-05 07:53:01

The [XRP Ledger](https://xrpl.org/) (XRPL) is a decentralized, public blockchain led by a global developer community. This article will showcase how to implement [XRPL communication](https://github.com/XRPLWin/XRPL) natively in your **PHP8** application.



### Prerequisites

To get started you need to have understanding how to install [composer](https://getcomposer.org/) 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](https://getcomposer.org/) installed on your machine
- PHP^8.1 installed on your machine

## Project setup

**Include [XRPL API package](https://github.com/XRPLWin/XRPL)**

```
composer require xrplwin/xrpl
```

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

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

echo 'Hello world';
```
**run in terminal:**
```
php ./index.php
```
should output:
```
Hello world
```
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](https://github.com/XRPLWin/XRPL#init-client).

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

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

```

### Prepare XRPL Method
**index.php (part)**

Prepare XRPL API method ([see docs](https://xrpl.org/public-api-methods.html)) with parameters. In this example we will try to run `account_info` query [described here](https://xrpl.org/account_info.html).
```PHP
...

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

### 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](https://xrpl.org/rate-limiting.html#json-rpc-rate-limited-error) HTTP status code.
```PHP
...

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

### Check if response is success and get result
**index.php (part)**

```PHP
...

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

### Full index.php

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

### Conclusion

With few lines of code you have successfully queried XRPL JSON-RPC endpoint and fetched result. With [XRPLWin/XRPL](https://github.com/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](https://github.com/XRPLWin/XRPL).

Do you like this tutorial? Give us 🌟 on [Github](https://github.com/XRPLWin/XRPL)!

---

[Star on Github](https://github.com/XRPLWin/XRPL) | [Follow on Twitter](https://twitter.com/xrplwin)