Documentation

RestClient
in package

Class RestClient

A versatile PHP class for making HTTP requests using cURL with options for handling JSON responses and cookies.

Table of Contents

Properties

$baseUrl  : string|null
$cookieFilePath  : string|null
$downloadFilename  : string|null
$headers  : array<string|int, mixed>
$response  : string|null
$useCookies  : bool
$userAgent  : string|null

Methods

__construct()  : mixed
RestClient constructor.
decodeJson()  : array<string|int, mixed>
Decode the JSON response to an associative array.
delete()  : RestClient
Make an HTTP DELETE request.
download()  : mixed
get()  : RestClient
Make an HTTP GET request.
getResponse()  : string
Get the raw HTTP response.
post()  : RestClient
Make an HTTP POST request.
put()  : RestClient
Make an HTTP PUT request.
setBaseUrl()  : RestClient
Set the base URL for API requests.
setHeaders()  : RestClient
Set custom HTTP headers for requests.
setUseCookies()  : RestClient
Set whether to use cookies for maintaining session.
setUserAgent()  : RestClient
Set the user-agent string for HTTP requests.
jsonToArray()  : array<string|int, mixed>
Convert JSON string to an associative array.
request()  : string
Send an HTTP request.

Properties

$baseUrl

private string|null $baseUrl

The base URL for API requests.

$cookieFilePath

private string|null $cookieFilePath

The file path to use for storing cookies.

$downloadFilename

private string|null $downloadFilename

The filename to use when downloading a file.

$headers

private array<string|int, mixed> $headers

The HTTP headers to include in requests.

$response

private string|null $response

The HTTP response from the most recent request.

$useCookies

private bool $useCookies

Whether to use cookies for maintaining session.

$userAgent

private string|null $userAgent

The user-agent string to use in HTTP requests.

Methods

__construct()

RestClient constructor.

public __construct([mixed $cookieFilePath = '/tmp/rest_client_cookies.txt' ]) : mixed
Parameters
$cookieFilePath : mixed = '/tmp/rest_client_cookies.txt'

decodeJson()

Decode the JSON response to an associative array.

public decodeJson() : array<string|int, mixed>
Tags
throws
Exception

If there is no response to decode or if there is a decoding error.

Return values
array<string|int, mixed>

The decoded JSON response.

delete()

Make an HTTP DELETE request.

public delete(string $endpoint) : RestClient
Parameters
$endpoint : string

The API endpoint to request.

Return values
RestClient

$this Allows method chaining.

download()

public download(mixed $filename) : mixed
Parameters
$filename : mixed

get()

Make an HTTP GET request.

public get(string $endpoint) : RestClient
Parameters
$endpoint : string

The API endpoint to request.

Return values
RestClient

$this Allows method chaining.

getResponse()

Get the raw HTTP response.

public getResponse() : string
Return values
string

The raw HTTP response.

post()

Make an HTTP POST request.

public post(string $endpoint, mixed $data) : RestClient
Parameters
$endpoint : string

The API endpoint to request.

$data : mixed

The data to include in the POST request body.

Return values
RestClient

$this Allows method chaining.

put()

Make an HTTP PUT request.

public put(string $endpoint, mixed $data) : RestClient
Parameters
$endpoint : string

The API endpoint to request.

$data : mixed

The data to include in the PUT request body.

Return values
RestClient

$this Allows method chaining.

setBaseUrl()

Set the base URL for API requests.

public setBaseUrl(string $baseUrl) : RestClient
Parameters
$baseUrl : string

The base URL for API requests.

Return values
RestClient

$this Allows method chaining.

setHeaders()

Set custom HTTP headers for requests.

public setHeaders(array<string|int, mixed> $headers) : RestClient
Parameters
$headers : array<string|int, mixed>

An array of custom HTTP headers.

Return values
RestClient

$this Allows method chaining.

setUseCookies()

Set whether to use cookies for maintaining session.

public setUseCookies(bool $useCookies) : RestClient
Parameters
$useCookies : bool

Whether to use cookies (default: false).

Return values
RestClient

$this Allows method chaining.

setUserAgent()

Set the user-agent string for HTTP requests.

public setUserAgent(string $userAgent) : RestClient
Parameters
$userAgent : string

The user-agent string to use.

Return values
RestClient

$this Allows method chaining.

jsonToArray()

Convert JSON string to an associative array.

private jsonToArray(string $json) : array<string|int, mixed>
Parameters
$json : string

The JSON string to decode.

Tags
throws
Exception

If there is a JSON decoding error.

Return values
array<string|int, mixed>

The decoded JSON as an associative array.

request()

Send an HTTP request.

private request(string $method, string $endpoint[, mixed $data = null ]) : string
Parameters
$method : string

The HTTP request method (GET, POST, PUT, DELETE, etc.).

$endpoint : string

The API endpoint to request.

$data : mixed = null

The data to include in the request body.

Tags
throws
Exception

If there is a cURL error, an HTTP error, or a JSON decoding error.

Return values
string

The HTTP response.


        
On this page

Search results