ChallengeRequestBuilder
extends RequestBuilder
in package
Builder for constructing SEP-10 challenge request URLs and executing challenge requests.
This builder follows the builder pattern to construct properly formatted GET requests to the SEP-10 authentication endpoint for obtaining challenge transactions. It handles URL parameter encoding and provides a fluent interface for setting request parameters.
Purpose: Simplifies the construction of challenge requests by providing type-safe methods for each parameter. Ensures proper URL encoding and parameter validation before making requests to the authentication server.
Usage Pattern:
$builder = new ChallengeRequestBuilder($authEndpoint, $httpClient);
$response = $builder
->forAccountId("GCXXX...")
->forMemo(12345) // Optional: for shared accounts
->forHomeDomain("example.com") // Optional: when server serves multiple domains
->forClientDomain("wallet.com") // Optional: for client domain verification
->execute();
SEP-10 Parameters:
- account: The client account ID (G... or M... address) requesting authentication
- memo: Optional ID memo for identifying users within shared/pooled accounts
- home_domain: Optional domain when server serves multiple home domains
- client_domain: Optional domain for client domain verification (non-custodial wallets)
The builder constructs URLs like: https://auth.example.com?account=GCXXX...&memo=12345&client_domain=wallet.com
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 request builder instance
- buildUrl() : string
- Builds the complete request URL with all segments and query parameters
- cursor() : RequestBuilder
- Sets <code>cursor</code> parameter on the request.
- execute() : Response
- Build and execute request.
- executeRequest() : Response
- Executes an HTTP request to Horizon and returns a parsed response object
- forAccountId() : ChallengeRequestBuilder
- forClientDomain() : ChallengeRequestBuilder
- forHomeDomain() : ChallengeRequestBuilder
- forMemo() : ChallengeRequestBuilder
- forQueryParameters() : ChallengeRequestBuilder
- getAndStream() : void
- Streams Server-Sent Events from Horizon to a callback function
- limit() : RequestBuilder
- Sets <code>limit</code> parameter on the request.
- order() : RequestBuilder
- Sets <code>order</code> parameter on the request.
- request() : ChallengeResponse
- Requests specific <code>url</code> and returns {@link ChallengeResponse}.
- 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 request builder instance
public
__construct(string $authEndpoint, Client $httpClient) : mixed
Parameters
- $authEndpoint : string
- $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 <code>cursor</code> parameter on the request.
public
cursor(string $cursor) : RequestBuilder
A cursor is a value that points to a specific location in a collection of resources. The cursor attribute itself is an opaque value meaning that users should not try to parse it.
Parameters
- $cursor : string
Tags
Return values
RequestBuilderexecute()
Build and execute request.
public
execute() : Response
@throws HorizonRequestException
Return values
Response —The parsed response object
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
forAccountId()
public
forAccountId(string $accountId) : ChallengeRequestBuilder
Parameters
- $accountId : string
Return values
ChallengeRequestBuilderforClientDomain()
public
forClientDomain(string $clientDomain) : ChallengeRequestBuilder
Parameters
- $clientDomain : string
Return values
ChallengeRequestBuilderforHomeDomain()
public
forHomeDomain(string $homeDomain) : ChallengeRequestBuilder
Parameters
- $homeDomain : string
Return values
ChallengeRequestBuilderforMemo()
public
forMemo(int $memo) : ChallengeRequestBuilder
Parameters
- $memo : int
Return values
ChallengeRequestBuilderforQueryParameters()
public
forQueryParameters(array<string|int, mixed> $queryParameters) : ChallengeRequestBuilder
Parameters
- $queryParameters : array<string|int, mixed>
Return values
ChallengeRequestBuildergetAndStream()
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
limit()
Sets <code>limit</code> parameter on the request.
public
limit(int $number) : RequestBuilder
It defines maximum number of records to return. For range and default values check documentation of the endpoint requested.
Parameters
- $number : int
-
Maximum number of records to return
Return values
RequestBuilderorder()
Sets <code>order</code> parameter on the request.
public
order([string $direction = "asc" ]) : RequestBuilder
Parameters
- $direction : string = "asc"
-
"asc" or "desc"
Return values
RequestBuilderrequest()
Requests specific <code>url</code> and returns {@link ChallengeResponse}.
public
request(string $url) : ChallengeResponse
Parameters
- $url : string
Tags
Return values
ChallengeResponsesetSegments()
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