RestClient
in package
REST API client class.
Provides HTTP client functionality using cURL with support for GET/POST/PUT/DELETE methods, SSL authentication, and basic auth.
use \X\Util\RestClient;
$client = new RestClient([
'base_url' => 'https://example.com/',
'headers' => [
'X-API-KEY' => 'rrjhueveu3zcywqy4ry2m5qd',
],
]);
// Request GET https://example.com/users.
$res = $this->backend->get('users');
var_dump($res->response);
Table of Contents
Properties
- $error : string
- Response Error.
- $info : object
- Result of curl_getinfo().
- $options : array<string|int, mixed>
- REST client options.
- $requestUrl : string
- Request URL.
- $response : array<string|int, mixed>
- Response data.
- $responseHeaders : array<string|int, mixed>
- Response header.
- $responseRaw : string
- Raw response data.
- $status : int
- HTTP Status.
Methods
- __construct() : mixed
- Initialize RestClient.
- delete() : RestClient
- Send a DELETE request.
- get() : RestClient
- Send a GET request.
- post() : RestClient
- Send a POST request.
- put() : RestClient
- Send a PUT request.
- parse() : void
- Parse raw cURL response into headers, status, and body.
- send() : RestClient
- Execute an HTTP request via cURL.
Properties
$error
Response Error.
public
string
$error
$info
Result of curl_getinfo().
public
object
$info
$options
REST client options.
public
array<string|int, mixed>
$options
- headers: array of HTTP headers (e.g., ['X-API-KEY' => 'xxx'])
- parameters: array of default request parameters
- curl_option: array of additional cURL options
- user_agent: string custom user agent
- base_url: string base URL for requests
- username: string for basic authentication
- password: string for basic authentication
- ssl: array with 'cert_file', 'ca_file', 'secret_key_file', 'secret_key_passphrase'
$requestUrl
Request URL.
public
string
$requestUrl
$response
Response data.
public
array<string|int, mixed>
$response
$responseHeaders
Response header.
public
array<string|int, mixed>
$responseHeaders
$responseRaw
Raw response data.
public
string
$responseRaw
$status
HTTP Status.
public
int
$status
Methods
__construct()
Initialize RestClient.
public
__construct([array{headers?: array, parameters?: array, curl_option?: array, user_agent?: string, base_url?: string, username?: string, password?: string, ssl?: array{cert_file?: string, ca_file?: string, secret_key_file?: string, secret_key_passphrase?: string}} $options = [] ]) : mixed
Parameters
-
$options
: array{headers?: array
, parameters?: array, curl_option?: array, user_agent?: string, base_url?: string, username?: string, password?: string, ssl?: array{cert_file?: string, ca_file?: string, secret_key_file?: string, secret_key_passphrase?: string}} = [] -
Configuration options:
-
headers: Default HTTP headers for all requests. -
parameters: Default parameters (query for GET, body for POST/PUT). -
curl_option: Additional cURL options. -
user_agent: User agent string. Default is "PHP RestClient". -
base_url: Base URL prepended to all request URLs. -
username: Basic auth username. -
password: Basic auth password. -
ssl: SSL client certificate settings (cert_file, ca_file, secret_key_file, secret_key_passphrase).
-
delete()
Send a DELETE request.
public
delete(string $url[, array<string|int, mixed>|string $params = [] ][, array<string|int, mixed> $headers = [] ]) : RestClient
Parameters
- $url : string
-
Request URL (appended to base_url if set).
- $params : array<string|int, mixed>|string = []
-
Request body parameters.
- $headers : array<string|int, mixed> = []
-
Additional request headers.
Tags
Return values
RestClient —Cloned instance with response data populated.
get()
Send a GET request.
public
get(string $url[, array<string|int, mixed>|string $params = [] ][, array<string|int, mixed> $headers = [] ]) : RestClient
Parameters
- $url : string
-
Request URL (appended to base_url if set).
- $params : array<string|int, mixed>|string = []
-
Query parameters.
- $headers : array<string|int, mixed> = []
-
Additional request headers.
Tags
Return values
RestClient —Cloned instance with response data populated.
post()
Send a POST request.
public
post(string $url[, array<string|int, mixed>|string $params = [] ][, array<string|int, mixed> $headers = [] ]) : RestClient
Parameters
- $url : string
-
Request URL (appended to base_url if set).
- $params : array<string|int, mixed>|string = []
-
Request body parameters.
- $headers : array<string|int, mixed> = []
-
Additional request headers.
Tags
Return values
RestClient —Cloned instance with response data populated.
put()
Send a PUT request.
public
put(string $url[, array<string|int, mixed>|string $params = [] ][, array<string|int, mixed> $headers = [] ]) : RestClient
Parameters
- $url : string
-
Request URL (appended to base_url if set).
- $params : array<string|int, mixed>|string = []
-
Request body parameters.
- $headers : array<string|int, mixed> = []
-
Additional request headers.
Tags
Return values
RestClient —Cloned instance with response data populated.
parse()
Parse raw cURL response into headers, status, and body.
private
parse(string $res) : void
Parameters
- $res : string
-
Raw response from curl_exec().
send()
Execute an HTTP request via cURL.
private
send(string $url, string $method[, array<string|int, mixed>|string $params = [] ][, array<string|int, mixed> $headers = [] ]) : RestClient
Parameters
- $url : string
-
Request URL.
- $method : string
-
HTTP method (GET, POST, PUT, DELETE).
- $params : array<string|int, mixed>|string = []
-
Query parameters (GET) or request body (POST/PUT/DELETE).
- $headers : array<string|int, mixed> = []
-
Additional request headers.
Tags
Return values
RestClient —Cloned instance with response data populated.