ecourty/xrpl-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ecourty/xrpl-php

最新稳定版本:1.3.0

Composer 安装命令:

composer require ecourty/xrpl-php

包简介

A PHP library to interact with an XRP Ledger Node.

README 文档

README

PHP CI

A PHP library to interact with an XRP Ledger Node.

Table of Contents

Features

This library provides a simple way to interact with an XRP Ledger Node.
It covers 100% of the public XRP Ledger API JSON-RPC methods at date of writing.

The library is designed to be simple to use and easy to understand.
xrpl-php allows you to:

  • Communicate with the XRP Ledger
  • Manage and generate XRP Ledger wallets
  • Fund your testnet / devnet wallets using the Faucet
  • Sign and submit transactions to the XRP Ledger
    • Support for Single-Signature and Multi-Signature transactions
  • Translate XRP to Drops amounts

Here are some usage examples.

Installation

Install the package using Composer:

composer require ecourty/xrpl-php

Usage

XRP Ledger Communication

If you wish to communicate with an XRP Ledger Node, you can use the XRPLClient class as follows:

<?php

use XRPL\Client\XRPLClient;

$client = new XRPLClient('https://s1.ripple.com:51234'); // Public XRP Ledger Node

// Testnet Public Node: https://s.altnet.rippletest.net:51234
// Devnet Public Node: https://s.devnet.rippletest.net:51234

XRP Ledger Wallet Management

xrpl-php allows you to create and import XRP Ledger Wallets.
You can generate or import a wallet as follows:

<?php

use XRPL\Enum\Algorithm;
use XRPL\Service\Wallet\WalletGenerator;
use XRPL\ValueObject\Wallet;

$newWallet = WalletGenerator::generate(Algorithm::SECP256K1);
// Also works as Wallet::generate(Algorithm::ALGORITHM_SECP256K1);

$seed = 'sEd7Fv8k1vF9R5kFtPbQG7wYyVr'; // Example seed, do not reuse
$importedWallet = WalletGenerator::generateFromSeed($seed);
// Also works as Wallet::generateFromSeed($seed);

Submitting a transaction to the XRP Ledger

You can submit a transaction to the XRP Ledger with multiple approaches:

<?php

use XRPL\Client\XRPLClient;
use XRPL\ValueObject\Wallet;

$client = new XRPLClient('https://s1.ripple.com:51234');

$wallet = Wallet::generateFromSeed('...')

/**
 * @see https://xrpl.org/docs/references/protocol/transactions/types
 */
$transactionData = [
    'Account' => $wallet->getAddress(),
    'TransactionType' => 'Payment',
    'Destination' => 'r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59',
    'Amount' => '1000000',
    // ...
];

// 1. Use the XRPLCLient to submit the transaction directly
$client->submitSingleSignTransaction($transactionData, $wallet);
$client->submitMultiSignTransaction($transactionData, $wallet, $signers);

// 2. Hash the transaction yourself and submit it
$client->autofillTransaction(transactionData); // Add the missing fields if not already set (Fee, Sequence, LastLedgerSequence)
$transactionBlob = $wallet->sign($transactionData);

$client->transaction->submitOnly($transactionBlob);

Autofilling Transaction Fields

The XRP Ledger requires some fields to be set in the transaction data.
These fields are:

  • Fee
  • Sequence
  • LastLedgerSequence

If you want to automatically fill these fields in the transaction data, you can use the autofillTransaction method.
This will query the correct values from your account and fill them in the transaction data.

Code examples

Code examples can be found in the examples directory.

Adding funds on a TestNet / DevNet wallet

You can add funds to a TestNet / DevNet wallet using either the Wallet class or the Faucet class.

  1. Adding funds using the Wallet class

    <?php
    
    use XRPL\ValueObject\Wallet;
    
    $wallet = Wallet::generate(); // Or import a wallet using ::generateFromSeed
    $wallet->addFunds(); // Adds 100 XRP to the wallet
  2. Adding funds using the Faucet class

    <?php
    
    use XRPL\Service\Faucet;
    
    $wallet = Wallet::generate(); // Or import a wallet using ::generateFromSeed
    Faucet::addFunds($wallet); // Adds 100 XRP to the wallet

Contracts

xrpl-php implements two contracts to allow for seamless integration with external systems:

  • XRPL\Contract\Wallet
  • XRPL\Contract\KeyPair

The provided Wallet and KeyPair classes implement these contracts.

You can implement these interfaces in your own classes to allow for easy integration with xrpl-php.

Examples

  1. Getting the balance of an account

    <?php
    
    use XRPL\Client\XRPLClient;
    
    $client = new XRPLClient('https://s1.ripple.com:51234');
    
    $accountLines = $client->account->getAccountLines('r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59');
    
    foreach ($accountLines->lines as $line) {
        $currency = $line->currency;
        $amount = $line->balance;
    
        // Implement your own logic
    }
  2. Getting the last transactions of a Ledger (by hash / index)

    If no ledger hash or index is passed, the latest Ledger data will be returned.

    <?php
    
    $lastLedger = $client->ledger->getLedger(
        ledgerIndex: 93392983,
        transactions: true,
        expand: true
    );
    
    foreach ($lastLedger->ledger->transactions as $transaction) {
        $txHash = $transaction->hash;
        $txAmount = $transaction->takerGets->getValue();
        $txType = $transaction->TransactionType;
    
        // Implement your own logic
    }
  3. Getting the NFTs of an account

    <?php
    
    $accountNFTs = $client->account->getAccountNFTs('r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59');
    
    foreach ($accountNFTs->accountNfts as $nft) {
        $nftId = $nft->id;
        $nftOwner = $nft->owner;
        $nftIssuer = $nft->issuer;
    
        // Implement your own logic
    }
  4. Trading (Paths & Order Book)

    <?php
    
    $bookChanges = $client->pathOrderBook->getBookChanges(
    
    );
    
    foreach ($bookChanges->changes as $change) {
        $open = $change->open;
        $close = $change->close;
    
        $lowest = $change->low;
        $highest = $change->high; // More data is available in the model
    
        // Implement your own logic
    }

© Edouard Courty, 2025

统计信息

  • 总下载量: 7.3k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-11