Request
Request
HTTP Request class.
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
public string $method = 'GET'
context
public mixed $context
rawURL
public string $rawURL
headers
private $headers = array (
'User-Agent' => 'hazaarmvc-php/',
0 => '.',
1 => 'HAZAAR_VERSION',
2 => '.',
3 => ' (PHP)',
'Content-Type' => 'text/html',
'Connection' => 'close',
)
url
private URL $url
fsock_host
private string $fsock_host
body
private mixed $body
multipart
private bool $multipart
dontEncodeURL
private bool $dontEncodeURL
jsonEncodeFlags
private int $jsonEncodeFlags
jsonEncodeDepth
private int $jsonEncodeDepth = 512
data
private array $data
Methods
__construct
HTTP request constructor.
public __construct(URL $url, string $method = 'GET', ?string $content_type, mixed $custom_context): void
Parameters
Parameter | Type | Description |
---|---|---|
$url | URL | The url of the resource that will be requested |
$method | string | The request method to use. Typically GET, POST, etc |
$content_type | string | optionally set the content type header |
$custom_context | 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(URL $url): URL
Parameters
Parameter | Type | Description |
---|---|---|
$url | URL |
setContentType
Sets the Content-Type header for the request.
public setContentType(string $content_type): void
Parameters
Parameter | Type | Description |
---|---|---|
$content_type | string |
getContentType
Returns the current Content-Type header for the request.
public getContentType(): string
enableMultipart
Enable multipart mime request body optionally using the specified boundary and content type.
public enableMultipart(?string $content_type, ?string $boundary): bool
Parameters
Parameter | Type | Description |
---|---|---|
$content_type | 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(): bool
getMultipartBoundary
Return the current multipart boundary name.
public getMultipartBoundary(): string
addMultipart
Add a multipart chunk to the request.
public addMultipart(mixed $data, ?string $content_type, ?array $headers): void
Parameters
Parameter | Type | Description |
---|---|---|
$data | mixed | the data to add to the request |
$content_type | 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(): string
setBody
Set the request body.
public setBody(mixed $body, ?string $content_type): void
If multipart is enabled, then the body will be added as a new chunk.
Parameters
Parameter | Type | Description |
---|---|---|
$body | mixed | |
$content_type | string |
getBody
Return the body of the request.
public getBody(): void
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(): void
getHeader
Returns the value of the header specified by $key.
public getHeader(string $key): 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 $allow_multiple = false): void
Parameters
Parameter | Type | Description |
---|---|---|
$key | string | the name of the header to set |
$value | string | the value to set on the header |
$allow_multiple | 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(?string $encryption_key, ?string $encryption_cipher): string
This is the method that renders the request as a HTTP/1.1 compliant request.
Parameters
Parameter | Type | Description |
---|---|---|
$encryption_key | string | optionally encrypt the request using this encryption key |
$encryption_cipher | string | optionally specifiy the cipher used to encrypt the request |
authorise
Add Basic HTTP authentication.
public authorise(string $username, string $password): 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'): bool
Parameters
Parameter | Type | Description |
---|---|---|
$user | Adapter | |
$type | string |
authorisation
Use an auth adapter to set an Oauth token on the request.
public authorisation(Adapter $user, ?string $type): bool
Parameters
Parameter | Type | Description |
---|---|---|
$user | Adapter | |
$type | string |
setLocalCertificate
Set a local PEM encoded certificate to use for SSL communication.
public setLocalCertificate(string $local_cert, ?string $passphrase, ?string $local_pk): bool
Parameters
Parameter | Type | Description |
---|---|---|
$local_cert | string | |
$passphrase | string | |
$local_pk | string |
setContextOption
Wrapper function to the internal PHP function stream_context_set_option() function.
public setContextOption(array $options): 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): 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): 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): bool
Parameters
Parameter | Type | Description |
---|---|---|
$value | bool |
setURLEncode
Enable/Disable URL encoding.
public setURLEncode(bool $value = true): 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): 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 = 0, int $depth = 512): string
Parameters
Parameter | Type | Description |
---|---|---|
$flags | int | |
$depth | int |
offsetExists
public offsetExists(mixed $offset): bool
Parameters
Parameter | Type | Description |
---|---|---|
$offset | mixed |
offsetGet
public offsetGet(mixed $offset): mixed
Parameters
Parameter | Type | Description |
---|---|---|
$offset | mixed |
offsetSet
public offsetSet(mixed $offset, mixed $value): void
Parameters
Parameter | Type | Description |
---|---|---|
$offset | mixed | |
$value | mixed |
offsetUnset
public offsetUnset(mixed $offset): void
Parameters
Parameter | Type | Description |
---|---|---|
$offset | mixed |
count
public count(): int
toArray
public toArray(): void
populate
public populate(array $data): void
Parameters
Parameter | Type | Description |
---|---|---|
$data | array |
Generated by Hazaar API Doc Generator