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 DELETE request.
- get() : RestClient
- GET Request.
- post() : RestClient
- Send POST request.
- put() : RestClient
- Send PUT request.
- parse() : void
- Set response data (responseHeaders,responseRaw,response).
- send() : RestClient
- Common Requests.
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<string|int, mixed> $options = [] ]) : mixed
Parameters
- $options : array<string|int, mixed> = []
delete()
Send DELETE request.
public
delete(string $url[, array<string|int, mixed> $params = [] ][, array<string|int, mixed> $headers = [] ]) : RestClient
Parameters
- $url : string
-
Request URL.
- $params : array<string|int, mixed> = []
-
(optional) Request Body.
- $headers : array<string|int, mixed> = []
-
(optional) Request headers.
Return values
RestClientget()
GET Request.
public
get(string $url[, array<string|int, mixed> $params = [] ][, array<string|int, mixed> $headers = [] ]) : RestClient
Parameters
- $url : string
-
Request URL.
- $params : array<string|int, mixed> = []
-
(optional) Query Parameters.
- $headers : array<string|int, mixed> = []
-
(optional) Request headers.
Return values
RestClientpost()
Send POST request.
public
post(string $url[, array<string|int, mixed> $params = [] ][, array<string|int, mixed> $headers = [] ]) : RestClient
Parameters
- $url : string
-
Request URL.
- $params : array<string|int, mixed> = []
-
(optional) Request Body.
- $headers : array<string|int, mixed> = []
-
(optional) Request headers.
Return values
RestClientput()
Send PUT request.
public
put(string $url[, array<string|int, mixed> $params = [] ][, array<string|int, mixed> $headers = [] ]) : RestClient
Parameters
- $url : string
-
Request URL.
- $params : array<string|int, mixed> = []
-
(optional) Request Body.
- $headers : array<string|int, mixed> = []
-
(optional) Request headers.
Return values
RestClientparse()
Set response data (responseHeaders,responseRaw,response).
private
parse(string $res) : void
Parameters
- $res : string
-
Response data (curl_exec($ch)).
send()
Common Requests.
private
send(string $url, string $method[, array<string|int, mixed> $params = [] ][, array<string|int, mixed> $headers = [] ]) : RestClient
Parameters
- $url : string
-
Request URL.
- $method : string
-
HTTP Method.
- $params : array<string|int, mixed> = []
-
(optional) Query parameter in case of GET, request body otherwise.
- $headers : array<string|int, mixed> = []
-
(optional) Request headers.