TransactionsRequestBuilder
extends RequestBuilder
in package
Builds requests for the transactions endpoint in Horizon
This class provides methods to query transactions on the Stellar network, including fetching individual transactions, querying transactions for specific accounts, ledgers, or liquidity pools, and streaming real-time transaction updates.
Transactions represent operations that have been submitted to and processed by the network. Each transaction contains one or more operations and is signed by one or more accounts.
Query Methods:
- forAccount(): Get transactions for a specific account
- forLedger(): Get transactions in a specific ledger
- forClaimableBalance(): Get transactions involving a claimable balance
- forLiquidityPool(): Get transactions involving a liquidity pool
- includeFailed(): Include failed transactions in results
Usage Examples:
// Get a single transaction $tx = $sdk->transactions()->transaction("hash123...");
// Get transactions for an account with pagination $txs = $sdk->transactions() ->forAccount("GDAT5...") ->limit(20) ->order("desc") ->execute();
// Include failed transactions $txs = $sdk->transactions() ->forAccount("GDAT5...") ->includeFailed(true) ->execute();
// Stream real-time transactions $sdk->transactions() ->cursor("now") ->stream(function($tx) { echo "New transaction: " . $tx->getHash(); });
Tags
Table of Contents
Constants
- HEADERS = ["X-Client-Name" => "stellar_php_sdk", "X-Client-Version" => \Soneso\StellarSDK\StellarSDK::VERSION_NR]
Properties
- $httpClient : Client
- $queryParameters : array<string|int, mixed>
- $segments : array<string|int, mixed>
Methods
- __construct() : mixed
- Constructs a new TransactionsRequestBuilder instance
- buildUrl() : string
- Builds the complete request URL with all segments and query parameters
- cursor() : TransactionsRequestBuilder
- Sets the cursor position for pagination
- execute() : TransactionsPageResponse
- Builds the query URL and executes the request
- executeRequest() : Response
- Executes an HTTP request to Horizon and returns a parsed response object
- forAccount() : TransactionsRequestBuilder
- Filters transactions to those affecting a specific account
- forClaimableBalance() : TransactionsRequestBuilder
- Filters transactions to those affecting a specific claimable balance
- forLedger() : TransactionsRequestBuilder
- Filters transactions to those in a specific ledger
- forLiquidityPool() : TransactionsRequestBuilder
- Filters transactions to those affecting a specific liquidity pool
- getAndStream() : void
- Streams Server-Sent Events from Horizon to a callback function
- includeFailed() : TransactionsRequestBuilder
- Includes failed transactions in the results
- limit() : TransactionsRequestBuilder
- Sets the maximum number of transactions to return
- order() : TransactionsRequestBuilder
- Sets the sort order for results
- request() : TransactionsPageResponse
- Requests a specific URL and returns paginated transactions
- stream() : void
- Streams real-time transaction updates to a callback function
- transaction() : TransactionResponse
- Fetches a single transaction by its hash
- setSegments() : RequestBuilder
- Sets the URL path segments for this request
Constants
HEADERS
public
mixed
HEADERS
= ["X-Client-Name" => "stellar_php_sdk", "X-Client-Version" => \Soneso\StellarSDK\StellarSDK::VERSION_NR]
Properties
$httpClient
protected
Client
$httpClient
$queryParameters
protected
array<string|int, mixed>
$queryParameters
$segments
protected
array<string|int, mixed>
$segments
Methods
__construct()
Constructs a new TransactionsRequestBuilder instance
public
__construct(Client $httpClient) : mixed
Parameters
- $httpClient : Client
-
The Guzzle HTTP client for making requests
buildUrl()
Builds the complete request URL with all segments and query parameters
public
buildUrl() : string
Combines the URL path segments with query parameters to create the final request URL string that will be sent to Horizon.
Return values
string —The constructed URL with query parameters
cursor()
Sets the cursor position for pagination
public
cursor(string $cursor) : TransactionsRequestBuilder
A cursor is an opaque value that points to a specific location in a result set. Use "now" as the cursor to stream only new transactions.
Parameters
- $cursor : string
-
The paging token from a previous response or "now"
Tags
Return values
TransactionsRequestBuilder —This instance for method chaining
execute()
Builds the query URL and executes the request
public
execute() : TransactionsPageResponse
Combines all query parameters and filters to build the final URL, then executes the request and returns paginated transaction results.
Tags
Return values
TransactionsPageResponse —Paginated list of transactions matching the query
executeRequest()
Executes an HTTP request to Horizon and returns a parsed response object
public
executeRequest(string $url, string $requestType[, string|null $requestMethod = "GET" ]) : Response
This method sends the HTTP request to the Horizon server, handles errors, and parses the JSON response into the appropriate response type.
Parameters
- $url : string
-
The complete request URL to fetch
- $requestType : string
-
The expected response type for parsing
- $requestMethod : string|null = "GET"
-
The HTTP method to use (default: "GET")
Tags
Return values
Response —The parsed response object of the specified type
forAccount()
Filters transactions to those affecting a specific account
public
forAccount(string $accountId) : TransactionsRequestBuilder
Returns transactions where the specified account is either the source account or affected by one of the transaction's operations.
Builds request to GET /accounts/{account}/transactions
Parameters
- $accountId : string
-
Public key of the account to filter by (G-address)
Tags
Return values
TransactionsRequestBuilder —This instance for method chaining
forClaimableBalance()
Filters transactions to those affecting a specific claimable balance
public
forClaimableBalance(string $claimableBalanceId) : TransactionsRequestBuilder
Returns transactions that created, claimed, or clawed back the specified claimable balance.
Builds request to GET /claimable_balances/{claimable_balance_id}/transactions
Parameters
- $claimableBalanceId : string
-
ID of the claimable balance (B-address or hex format)
Tags
Return values
TransactionsRequestBuilder —This instance for method chaining
forLedger()
Filters transactions to those in a specific ledger
public
forLedger(string $ledgerSeq) : TransactionsRequestBuilder
Returns all transactions that were included in the specified ledger.
Builds request to GET /ledgers/{ledgerSeq}/transactions
Parameters
- $ledgerSeq : string
-
The ledger sequence number
Tags
Return values
TransactionsRequestBuilder —This instance for method chaining
forLiquidityPool()
Filters transactions to those affecting a specific liquidity pool
public
forLiquidityPool(string $liquidityPoolId) : TransactionsRequestBuilder
Returns transactions that deposited to, withdrew from, or traded with the specified liquidity pool.
Builds request to GET /liquidity_pools/{poolID}/transactions
Parameters
- $liquidityPoolId : string
-
The liquidity pool ID (L-address or hex format)
Tags
Return values
TransactionsRequestBuilder —This instance for method chaining
getAndStream()
Streams Server-Sent Events from Horizon to a callback function
public
getAndStream(string $relativeUrl, callable $callback[, bool $retryOnServerException = true ]) : void
This method establishes a persistent connection to Horizon's streaming endpoints using Server-Sent Events (SSE). It processes each event and passes the parsed data to the provided callback function. The stream automatically reconnects on server exceptions if retryOnServerException is true.
Horizon streaming uses SSE to push real-time updates. The stream sends:
- "hello" message on connection
- "byebye" message on disconnection
- JSON data objects for actual events
Parameters
- $relativeUrl : string
-
The relative URL to stream from
- $callback : callable
-
Function to receive parsed event data
- $retryOnServerException : bool = true
-
If true, automatically retry on server errors (default: true)
Tags
Return values
void —This method runs indefinitely until interrupted
includeFailed()
Includes failed transactions in the results
public
includeFailed(bool $value) : TransactionsRequestBuilder
By default, only successful transactions are returned. Set this to true to also include transactions that failed during execution.
Parameters
- $value : bool
-
Set to true to include failed transactions, false to exclude them
Return values
TransactionsRequestBuilder —This instance for method chaining
limit()
Sets the maximum number of transactions to return
public
limit(int $number) : TransactionsRequestBuilder
Defines the maximum number of records in the response. Maximum allowed is typically 200.
Parameters
- $number : int
-
Maximum number of transactions to return (1-200)
Return values
TransactionsRequestBuilder —This instance for method chaining
order()
Sets the sort order for results
public
order([string $direction = "asc" ]) : TransactionsRequestBuilder
Determines whether results are sorted by ledger sequence in ascending or descending order.
Parameters
- $direction : string = "asc"
-
Sort direction: "asc" for ascending, "desc" for descending
Return values
TransactionsRequestBuilder —This instance for method chaining
request()
Requests a specific URL and returns paginated transactions
public
request(string $url) : TransactionsPageResponse
This method is typically used internally for pagination. Use execute() instead for normal queries.
Parameters
- $url : string
-
The complete URL to request
Tags
Return values
TransactionsPageResponse —Paginated list of transactions
stream()
Streams real-time transaction updates to a callback function
public
stream([callable|null $callback = null ]) : void
This method establishes a persistent connection to Horizon and streams new transactions as they are added to the network. The callback is invoked for each new transaction.
Use cursor("now") before calling stream() to only receive new transactions and skip historical ones.
Example: $sdk->transactions()->cursor("now")->stream(function(TransactionResponse $tx) { printf("New transaction: %s\n", $tx->getHash()); });
Parameters
- $callback : callable|null = null
-
Function to receive TransactionResponse objects
Tags
Return values
void —This method runs indefinitely until interrupted
transaction()
Fetches a single transaction by its hash
public
transaction(string $transactionId) : TransactionResponse
Requests GET /transactions/{transactionId}
Parameters
- $transactionId : string
-
Hash of the transaction to fetch
Tags
Return values
TransactionResponse —The transaction details including operations, memo, and XDR
setSegments()
Sets the URL path segments for this request
protected
setSegments(string ...$segments) : RequestBuilder
This method constructs the URL path by combining multiple segments. Can only be called once per request builder instance.
Parameters
- $segments : string
-
Variable number of URL path segments
Tags
Return values
RequestBuilder —This instance for method chaining