Request
Request
HTTP Request class.
class Request implements \ArrayAccessImplements: ArrayAccess
This class is used to create and manage HTTP requests. It is used by the Client class to create and send requests to remote servers. The Request class is used to create the request and the Client class is used to send the request and receive the response.
The Request class is used to create the request and the Client class is used to send the request and receive the response.
Properties
method
The HTTP method for the request (e.g., GET, POST, PUT, DELETE).
public string $method = 'GET'Type: string
context
public mixed $contextType: mixed
url
The URL object representing the resource being requested.
public URL $urlType: Hazaar\HTTP\URL
headers
private $headers = array (
'User-Agent' => 'hazaar-php (PHP)',
'Content-Type' => 'text/html',
'Connection' => 'close',
)fsockHost
The formatted host string for socket connections (includes protocol and port).
private string $fsockHostType: string
body
The body of the HTTP request, which can be a string, array, or other data type.
private null|array|string|\stdClass $bodyType: null|array|string|stdClass
multipart
Indicates whether the request uses multipart encoding.
private bool $multipartType: bool
encodeURL
Indicates whether URLs should be encoded using rawurlencode().
private bool $encodeURL = trueType: bool
jsonEncodeFlags
Flags used for JSON encoding when rendering the request body.
private int $jsonEncodeFlagsType: int
jsonEncodeDepth
Maximum depth for JSON encoding when rendering the request body.
private int $jsonEncodeDepth = 512Type: int
data
private array $dataType: array
Methods
__construct
HTTP request constructor.
public __construct(string|URL|null $url, string $method = 'GET', ?string $contentType, mixed $customContext): voidParameters
| Parameter | Type | Description |
|---|---|---|
$url | string | Hazaar\HTTP\URL |
$method | string | The request method to use. Typically GET, POST, etc |
$contentType | string | optionally set the content type header |
$customContext | mixed | Optionally use a custom context. Allows to define private SSL certificates. |
url
Set the url of the resource that is being requested.
public url(string|URL|null $url): URLReturns: Hazaar\HTTP\URL
Parameters
| Parameter | Type | Description |
|---|---|---|
$url | string | Hazaar\HTTP\URL |
appendURL
Appends a path segment to the current URL.
public appendURL(string $path): voidReturns: void
Updates the URL object by adding the specified path segment and re-parsing the URL.
Parameters
| Parameter | Type | Description |
|---|---|---|
$path | string | the path segment to append to the URL |
setContentType
Sets the Content-Type header for the request.
public setContentType(string $contentType): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$contentType | string |
getContentType
Returns the current Content-Type header for the request.
public getContentType(): stringReturns: string
enableMultipart
Enable multipart mime request body optionally using the specified boundary and content type.
public enableMultipart(?string $contentType, ?string $boundary): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$contentType | string | Optional request content type to use. Defaults to multipart/form-data. |
$boundary | string | Optional boundary identifier. Defaults to HazaarMultipartBoundary_{uniqid} |
isMultipart
Returns a boolean indicating if the request is a multipart request.
public isMultipart(): boolReturns: bool
getMultipartBoundary
Return the current multipart boundary name.
public getMultipartBoundary(): false|stringReturns: false|string
addMultipart
Add a multipart chunk to the request.
public addMultipart(array|string $data, ?string $contentType, ?array $headers): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$data | array | string |
$contentType | string | the content type of the added data |
$headers | array |
getHost
Returns the name of the host that will be sent this request.
public getHost(): stringReturns: string
setBody
Set the request body.
public setBody(array|Element|string|\stdClass $body, ?string $contentType): voidReturns: void
If multipart is enabled, then the body will be added as a new chunk.
Parameters
| Parameter | Type | Description |
|---|---|---|
$body | array | Hazaar\XML\Element |
$contentType | string |
getBody
Return the body of the request.
public getBody(): null|array|string|\stdClassReturns: null|array|string|stdClass
If multipart is enabled, this will return an array containing request body and content type.
getHeaders
Returns all the headers currently set on the request.
public getHeaders(): arrayReturns: array
getHeader
Returns the value of the header specified by $key.
public getHeader(string $key): stringReturns: string
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | The name of the header to return |
setHeader
Sets the value of a header.
public setHeader(string $key, string $value, bool $allowMultiple): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the name of the header to set |
$value | string | the value to set on the header |
$allowMultiple | bool | Whether multiple instances of the header are allowed. Defaults to false, meaning if the header exists, it will be updated. Multiple headers are rare but the main one is 'Cookie'. |
toString
Output the request as a string.
public toString(): stringReturns: string
This is the method that renders the request as a HTTP/1.1 compliant request.
authorise
Add Basic HTTP authentication.
public authorise(string $username, string $password): voidReturns: void
Basic authentication should ONLY be used over HTTPS.
Other methods are not yet supported, however Bearer is implemented using Request::authorization() method.
Parameters
| Parameter | Type | Description |
|---|---|---|
$username | string | The username to send |
$password | string | The password to send |
authorization
Alias for 'authorisation' for the bad 'merican spells.
public authorization(Adapter $user, string $type = 'Bearer'): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$user | Hazaar\Auth\Adapter | |
$type | string |
authorisation
Use an auth adapter to set an Oauth token on the request.
public authorisation(Adapter $user, ?string $type): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$user | Hazaar\Auth\Adapter | |
$type | string |
setLocalCertificate
Set a local PEM encoded certificate to use for SSL communication.
public setLocalCertificate(string $localCert, ?string $passphrase, ?string $localPk): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$localCert | string | |
$passphrase | string | |
$localPk | string |
setContextOption
Wrapper function to the internal PHP function stream_context_set_option() function.
public setContextOption(array $options): boolReturns: bool
See http://php.net/manual/en/context.ssl.php documentation for all the available wrappers and options.
Parameters
| Parameter | Type | Description |
|---|---|---|
$options | array | Must be an associative array in the format $arr['wrapper']['option'] = $value; |
allowSelfSigned
When using SSL communications, this allows the use of self-signed certificates.
public allowSelfSigned(bool $value = true): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | bool |
verifyPeer
When using SSL communications, this sets whether peer certificate verification is used.
public verifyPeer(bool $value = true): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | bool |
verifyPeerName
When using SSL communications, this sets whether peer name verification is used.
public verifyPeerName(bool $value = true): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | bool |
setURLEncode
Enable/Disable URL encoding.
public setURLEncode(bool $value = true): voidReturns: void
url encoding is enabled by default. Internally this calls PHPs rawurlencode() function to encode URLs into HTTP safe urls. However there may occasionally be special circumstances where the encoding may need to be disabled. Usually this is becuase the encoding is already being done by the calling function/class.
A prime example of this is Hazaar's SharePoint filesystem backend driver. SharePoint is very finicky about the format of the urls and wants some characters left alone (ie: brackets and quotes) as they make up the function/path reference being accessed. These functions/references will then have their contents only encoded and this is handled by the driver itself so encoding again in the Request class will screw things up.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | bool | TRUE enables encoding (the default). FALSE will disable encoding. |
setJSONEncodeFlags
Set JSON encoding flags/depth used when request is encoded to JSON.
public setJSONEncodeFlags(int $flags, int $depth = 512): voidReturns: void
Requests will automatically encode any data parameters to JSON encoded strings when generating the reqeust as a string. If there are any JSON encoding flags required, this function will apply those flags to all JSON encoding methods used when rendering the request. This includes requests sent with a mime content type of application/json as well as multipart encoded requests.
Parameters
| Parameter | Type | Description |
|---|---|---|
$flags | int | |
$depth | int |
toJSON
public toJSON(int $flags, int $depth = 512): stringReturns: string
Parameters
| Parameter | Type | Description |
|---|---|---|
$flags | int | |
$depth | int |
offsetExists
public offsetExists(mixed $offset): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$offset | mixed |
offsetGet
public offsetGet(mixed $offset): mixedReturns: mixed
Parameters
| Parameter | Type | Description |
|---|---|---|
$offset | mixed |
offsetSet
public offsetSet(mixed $offset, mixed $value): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$offset | mixed | |
$value | mixed |
offsetUnset
public offsetUnset(mixed $offset): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$offset | mixed |
count
public count(): intReturns: int
toArray
public toArray(): arrayReturns: array
populate
public populate(array $data): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$data | array |
Generated by Hazaar API Doc Generator on Fri, 04 Sep 2026 23:32:18 +0000