OffersRequestBuilder
extends RequestBuilder
in package
Builds requests for the offers endpoint in Horizon
This class provides methods to query offers on the Stellar network. Offers are orders to buy or sell assets on the decentralized exchange. They specify the asset being sold, the asset being bought, the price, and the amount.
Query Methods:
- offer(): Fetch a single offer by ID
- forAccount(): Get all offers created by a specific account
- forSponsor(): Filter by sponsor account
- forSeller(): Filter by the account selling assets
- forSellingAsset(): Filter by the asset being sold
- forBuyingAsset(): Filter by the asset being bought
Offers can be combined with forSellingAsset() and forBuyingAsset() to analyze specific trading pairs in the order book.
Usage Examples:
// Get a specific offer $offer = $sdk->offers()->offer("123456");
// Get all offers for an account $offers = $sdk->offers() ->forAccount("GDAT5...") ->execute();
// Get offers for a trading pair $selling = Asset::native(); $buying = Asset::createNonNativeAsset("USD", "GBBD..."); $offers = $sdk->offers() ->forSellingAsset($selling) ->forBuyingAsset($buying) ->execute();
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
- Constructor
- buildUrl() : string
- Builds the complete request URL with all segments and query parameters
- cursor() : OffersRequestBuilder
- 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
- forAccount() : OffersRequestBuilder
- Builds request to <code>GET /accounts/{account}/offers</code>
- forBuyingAsset() : OffersRequestBuilder
- Returns all offers buying an asset.
- forSeller() : OffersRequestBuilder
- Returns all offers where the given account is the seller.
- forSellingAsset() : OffersRequestBuilder
- Returns all offers selling an asset.
- forSponsor() : OffersRequestBuilder
- Returns all offers sponsored by a given account.
- getAndStream() : void
- Streams Server-Sent Events from Horizon to a callback function
- limit() : OffersRequestBuilder
- Sets <code>limit</code> parameter on the request.
- offer() : OfferResponse
- The offer details endpoint provides information on a single offer.
- order() : OffersRequestBuilder
- Sets <code>order</code> parameter on the request.
- request() : OffersPageResponse
- Requests specific <code>url</code> and returns {@link OffersPageResponse}.
- stream() : mixed
- Streams Offer objects to $callback
- 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()
Constructor
public
__construct(Client $httpClient) : mixed
Parameters
- $httpClient : Client
-
The HTTP client used for making requests to Horizon
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) : OffersRequestBuilder
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
OffersRequestBuilderexecute()
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
forAccount()
Builds request to <code>GET /accounts/{account}/offers</code>
public
forAccount(string $accountId) : OffersRequestBuilder
Parameters
- $accountId : string
-
ID of the account for which to get payments.
Tags
Return values
OffersRequestBuilderforBuyingAsset()
Returns all offers buying an asset.
public
forBuyingAsset(Asset $asset) : OffersRequestBuilder
Parameters
- $asset : Asset
-
The asset being bought.
Return values
OffersRequestBuilder —current instance
forSeller()
Returns all offers where the given account is the seller.
public
forSeller(string $seller) : OffersRequestBuilder
Parameters
- $seller : string
-
Account ID of the offer creator.
Return values
OffersRequestBuilder —current instance
forSellingAsset()
Returns all offers selling an asset.
public
forSellingAsset(Asset $asset) : OffersRequestBuilder
Parameters
- $asset : Asset
-
The asset being sold.
Return values
OffersRequestBuilder —current instance
forSponsor()
Returns all offers sponsored by a given account.
public
forSponsor(string $sponsor) : OffersRequestBuilder
Parameters
- $sponsor : string
-
Account ID of the sponsor.
Return values
OffersRequestBuilder —current instance
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
limit()
Sets <code>limit</code> parameter on the request.
public
limit(int $number) : OffersRequestBuilder
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
OffersRequestBuilderoffer()
The offer details endpoint provides information on a single offer.
public
offer(string $offerId) : OfferResponse
Parameters
- $offerId : string
-
Specifies which offer to load.
Tags
Return values
OfferResponse —The offer details.
order()
Sets <code>order</code> parameter on the request.
public
order([string $direction = "asc" ]) : OffersRequestBuilder
Parameters
- $direction : string = "asc"
-
"asc" or "desc"
Return values
OffersRequestBuilderrequest()
Requests specific <code>url</code> and returns {@link OffersPageResponse}.
public
request(string $url) : OffersPageResponse
Parameters
- $url : string
Tags
Return values
OffersPageResponsestream()
Streams Offer objects to $callback
public
stream([callable|null $callback = null ]) : mixed
$callback should have arguments: OfferResponse
For example:
$sdk = StellarSDK::getTestNetInstance(); $sdk->offers()->forAccount("GCDNJUBQSX7AJWLJACMJ7I4BC3Z47BQUTMHEICZLE6MU4KQBRYG5JY6B")->cursor("now")->stream(function(OfferResponse $offer) { printf('Offer id %s' . PHP_EOL, $offer->getOfferId()); });
Parameters
- $callback : callable|null = null
Tags
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