Request
in package
Class for sending HTTP requests.
HTTP client class for sending requests to REST API endpoints. Supports GET, POST, PUT, PATCH, and DELETE methods with automatic JSON encoding/decoding of request and response data.
Table of Contents
Properties
- $baseUrl : string
- Base URL for all API endpoints
- $fields : array<string|int, mixed>
- Maps HTTP methods to their corresponding cURL field options
- $methods : array<string|int, mixed>
- Maps HTTP methods to their corresponding cURL options
Methods
- __construct() : mixed
- Constructor
- send() : array<string|int, mixed>|string
- Sends an HTTP request to the specified endpoint
Properties
$baseUrl
Base URL for all API endpoints
private
string
$baseUrl
$fields
Maps HTTP methods to their corresponding cURL field options
private
array<string|int, mixed>
$fields
= ['POST' => CURLOPT_POSTFIELDS, 'PUT' => CURLOPT_POSTFIELDS, 'PATCH' => CURLOPT_POSTFIELDS, 'DELETE' => CURLOPT_POSTFIELDS, 'GET' => null]
$methods
Maps HTTP methods to their corresponding cURL options
private
array<string|int, mixed>
$methods
= ['POST' => CURLOPT_POST, 'PUT' => CURLOPT_CUSTOMREQUEST, 'PATCH' => CURLOPT_CUSTOMREQUEST, 'DELETE' => CURLOPT_CUSTOMREQUEST, 'GET' => null]
Methods
__construct()
Constructor
public
__construct(string $baseUrl) : mixed
Initializes the HTTP client with a base URL. Trailing slashes are automatically removed from the base URL.
Parameters
- $baseUrl : string
-
The base URL for all API requests
send()
Sends an HTTP request to the specified endpoint
public
send(string $endpoint[, array<string|int, mixed> $data = [] ][, string $method = 'GET' ]) : array<string|int, mixed>|string
Constructs and executes an HTTP request using cURL. For GET requests, data is appended as query parameters. For other methods, data is sent as JSON in the request body. Automatically decodes JSON responses or returns raw response if not JSON.
Parameters
- $endpoint : string
-
The API endpoint path (relative to base URL)
- $data : array<string|int, mixed> = []
-
Associative array of data to send with the request
- $method : string = 'GET'
-
HTTP method (GET, POST, PUT, PATCH, DELETE). Defaults to GET
Tags
Return values
array<string|int, mixed>|string —Decoded JSON response as array, or raw response string if not JSON