Documentation

Client extends AbstractHttp
in package

HTTP client class

Tags
category

Pop

author

Nick Sagona, III dev@nolainteractive.com

copyright

Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)

license

http://www.popphp.org/license New BSD License

version
5.2.0

Table of Contents

Properties

$auth  : Auth|null
HTTP auth object
$handler  : HandlerInterface|null
Request handler
$multiHandler  : CurlMulti|null
Request multi-handler
$options  : array<string|int, mixed>
Client options
$request  : AbstractRequest|null
Request
$response  : AbstractResponse|null
Response

Methods

__call()  : Response|Promise|array<string|int, mixed>|string
Magic method to send requests by the method name, i.e. $client->get('http://localhost/');
__callStatic()  : Response|Promise|array<string|int, mixed>|string
Magic method to send requests by the static method name, i.e. Client::get('http://localhost/');
__construct()  : mixed
Instantiate the client object
__toString()  : string
To string magic method to render the client request to a raw string
addData()  : Client
Add data
addFile()  : Client
Add file
addHeader()  : Client
Add header
addHeaders()  : Client
Add headers
addOption()  : Client
Add an option
addOptions()  : Client
Add options
addQuery()  : Client
Add query
createMulti()  : CurlMulti
Factory to create a multi-handler object
fromCurlCommand()  : Client
Method to convert Curl CLI command to a client object
getAuth()  : Auth|null
Get auth
getBody()  : Body|null
Get request body
getBodyContent()  : string|null
Get request body content
getBodyContentLength()  : int
Get request body content length
getData()  : mixed
Get data
getFile()  : string|null
Get file
getFiles()  : array<string|int, mixed>|null
Get files
getHandler()  : HandlerInterface|null
Get handler
getHeader()  : mixed
Get header
getHeaders()  : mixed
Get headers
getMethod()  : string|null
Get method
getMultiHandler()  : CurlMulti
Get multi-handler
getOption()  : mixed
Get options
getOptions()  : array<string|int, mixed>
Get options
getQuery()  : mixed
Get query
getRequest()  : AbstractRequest
Get the request
getResponse()  : AbstractResponse
Get the response
getType()  : mixed
Get type
hasAuth()  : bool
Has auth
hasBody()  : bool
Has request body
hasData()  : bool
Has data
hasFile()  : bool
Has file
hasFiles()  : bool
Has files
hasHandler()  : bool
Has handler
hasHeader()  : bool
Has header
hasHeaders()  : bool
Has headers
hasMethod()  : bool
Has method
hasMultiHandler()  : bool
Has multi-handler
hasOption()  : bool
Has option
hasOptions()  : bool
Has options
hasQuery()  : bool
Has query
hasRequest()  : bool
Has request
hasResponse()  : bool
Get the response
hasType()  : bool
Has type
isAccepted()  : bool|null
Determine if the response is 202 accepted
isBadGateway()  : bool|null
Determine if the response is a 502 Bad Gateway
isBadRequest()  : bool|null
Determine if the response is a 400 Bad Request
isClientError()  : bool|null
Determine if the response is a client error
isComplete()  : bool
Determine if the response is complete
isConflict()  : bool|null
Determine if the response is a 409 Conflict
isContinue()  : bool|null
Determine if the response is a continue
isCreated()  : bool|null
Determine if the response is 201 created
isError()  : bool|null
Determine if the response is an error
isForbidden()  : bool|null
Determine if the response is a 403 Forbidden
isFound()  : bool|null
Determine if the response is a 302 Found
isInternalServerError()  : bool|null
Determine if the response is a 500 Internal Server Error
isLengthRequired()  : bool|null
Determine if the response is a 411 Length Required
isMethodNotAllowed()  : bool|null
Determine if the response is a 405 Method Not Allowed
isMovedPermanently()  : bool|null
Determine if the response is a 301 Moved Permanently
isNoContent()  : bool|null
Determine if the response is 204 No Content
isNotAcceptable()  : bool|null
Determine if the response is a 406 Not Acceptable
isNotFound()  : bool|null
Determine if the response is a 404 Not Found
isOk()  : bool|null
Determine if the response is 200 OK
isRedirect()  : bool|null
Determine if the response is a redirect
isRequestTimeout()  : bool|null
Determine if the response is a 408 Request Timeout
isServerError()  : bool|null
Determine if the response is a server error
isServiceUnavailable()  : bool|null
Determine if the response is a 503 Service Unavailable
isSuccess()  : bool|null
Determine if the response is a success
isTooManyRequests()  : bool|null
Determine if the response is a 429 Too Many Requests
isUnauthorized()  : bool|null
Determine if the response is a 401 Unauthorized
isUnprocessableEntity()  : bool|null
Determine if the response is a 422 Unprocessable Entity
isUnsupportedMediaType()  : bool|null
Determine if the response is a 415 Unsupported Media Type
prepare()  : Client
Prepare the client request
removeAllData()  : Client
Remove all data
removeAllHeaders()  : Client
Remove all headers
removeAllQuery()  : Client
Remove all query data
removeBody()  : Client
Remove the body
removeData()  : Client
Remove data
removeFile()  : Client
Remove file
removeFiles()  : Client
Remove all files
removeHeader()  : Client
Remove header
removeOption()  : Client
Remove option
removeOptions()  : Client
Remove options
removeQuery()  : Client
Remove query
removeType()  : Client
Remove type
render()  : string
Method to render the request as a string
reset()  : static
Method to reset the client
send()  : Response|Promise|array<string|int, mixed>|string
Send the client request
sendAsync()  : Promise
Method to send the request asynchronously
setAuth()  : Client
Set auth
setBody()  : Client
Set request body
setBodyFromFile()  : Client
Set request body from file
setData()  : Client
Set data
setFiles()  : Client
Set files
setHandler()  : Client
Set handler
setHeaders()  : Client
Set headers (clear out any existing headers)
setMethod()  : Client
Set method
setMultiHandler()  : Client
Set multi-handler
setOptions()  : Client
Set options
setQuery()  : Client
Set query
setRequest()  : AbstractHttp
Set the request
setResponse()  : AbstractHttp
Set the response
setType()  : Client
Set type
toCurlCommand()  : string
Method to convert client object to a Curl command for the CLI

Properties

$auth

HTTP auth object

protected Auth|null $auth = null

$multiHandler

Request multi-handler

protected CurlMulti|null $multiHandler = null

$options

Client options

protected array<string|int, mixed> $options = []

Methods

__call()

Magic method to send requests by the method name, i.e. $client->get('http://localhost/');

public __call(string $methodName, array<string|int, mixed> $arguments) : Response|Promise|array<string|int, mixed>|string
Parameters
$methodName : string
$arguments : array<string|int, mixed>
Tags
throws
Exception|Exception|Exception
Return values
Response|Promise|array<string|int, mixed>|string

__callStatic()

Magic method to send requests by the static method name, i.e. Client::get('http://localhost/');

public static __callStatic(string $methodName, array<string|int, mixed> $arguments) : Response|Promise|array<string|int, mixed>|string
Parameters
$methodName : string
$arguments : array<string|int, mixed>
Tags
throws
Exception|Exception|Exception
Return values
Response|Promise|array<string|int, mixed>|string

__construct()

Instantiate the client object

public __construct() : mixed

Optional parameters are

  • Request URI string
  • Client request instance
  • Client response instance
  • Client handler instance
  • Auth instance
  • Options array

__toString()

To string magic method to render the client request to a raw string

public __toString() : string
Return values
string

addData()

Add data

public addData(string $name, mixed $value) : Client
Parameters
$name : string
$value : mixed
Return values
Client

addFile()

Add file

public addFile(string $file[, string|null $name = null ]) : Client
Parameters
$file : string
$name : string|null = null
Tags
throws
Exception
Return values
Client

addHeader()

Add header

public addHeader(Header|string|int $header[, mixed $value = null ]) : Client
Parameters
$header : Header|string|int
$value : mixed = null
Return values
Client

addHeaders()

Add headers

public addHeaders(array<string|int, mixed> $headers) : Client
Parameters
$headers : array<string|int, mixed>
Return values
Client

addOption()

Add an option

public addOption(string $name, mixed $value) : Client
Parameters
$name : string
$value : mixed
Return values
Client

addOptions()

Add options

public addOptions(array<string|int, mixed> $options) : Client
Parameters
$options : array<string|int, mixed>
Return values
Client

addQuery()

Add query

public addQuery(string $name, mixed $value) : Client
Parameters
$name : string
$value : mixed
Return values
Client

createMulti()

Factory to create a multi-handler object

public static createMulti(array<string|int, mixed> $requests[, CurlMulti $multiHandler = new ClientHandlerCurlMulti() ]) : CurlMulti
Parameters
$requests : array<string|int, mixed>
$multiHandler : CurlMulti = new ClientHandlerCurlMulti()
Return values
CurlMulti

fromCurlCommand()

Method to convert Curl CLI command to a client object

public static fromCurlCommand(string $command) : Client
Parameters
$command : string
Tags
throws
Exception
Return values
Client

getAuth()

Get auth

public getAuth() : Auth|null
Return values
Auth|null

getBody()

Get request body

public getBody() : Body|null
Return values
Body|null

getBodyContent()

Get request body content

public getBodyContent() : string|null
Return values
string|null

getBodyContentLength()

Get request body content length

public getBodyContentLength([bool $mb = false ]) : int
Parameters
$mb : bool = false
Return values
int

getData()

Get data

public getData([string|null $key = null ]) : mixed
Parameters
$key : string|null = null

getFile()

Get file

public getFile(string $key) : string|null
Parameters
$key : string
Return values
string|null

getFiles()

Get files

public getFiles() : array<string|int, mixed>|null
Return values
array<string|int, mixed>|null

getHeader()

Get header

public getHeader(string $name) : mixed
Parameters
$name : string

getHeaders()

Get headers

public getHeaders() : mixed

getMethod()

Get method

public getMethod() : string|null
Return values
string|null

getOption()

Get options

public getOption(string $name) : mixed
Parameters
$name : string

getOptions()

Get options

public getOptions() : array<string|int, mixed>
Return values
array<string|int, mixed>

getQuery()

Get query

public getQuery([string|null $key = null ]) : mixed
Parameters
$key : string|null = null

getType()

Get type

public getType() : mixed

hasAuth()

Has auth

public hasAuth() : bool
Return values
bool

hasBody()

Has request body

public hasBody() : bool
Return values
bool

hasData()

Has data

public hasData([string|null $key = null ]) : bool
Parameters
$key : string|null = null
Return values
bool

hasFile()

Has file

public hasFile(string $key) : bool
Parameters
$key : string
Return values
bool

hasFiles()

Has files

public hasFiles() : bool
Return values
bool

hasHandler()

Has handler

public hasHandler() : bool
Return values
bool

hasHeader()

Has header

public hasHeader(string $name) : bool
Parameters
$name : string
Return values
bool

hasHeaders()

Has headers

public hasHeaders() : bool
Return values
bool

hasMethod()

Has method

public hasMethod() : bool
Return values
bool

hasMultiHandler()

Has multi-handler

public hasMultiHandler() : bool
Return values
bool

hasOption()

Has option

public hasOption(string $name) : bool
Parameters
$name : string
Return values
bool

hasOptions()

Has options

public hasOptions() : bool
Return values
bool

hasQuery()

Has query

public hasQuery([string|null $key = null ]) : bool
Parameters
$key : string|null = null
Return values
bool

hasRequest()

Has request

public hasRequest() : bool
Return values
bool

hasResponse()

Get the response

public hasResponse() : bool
Return values
bool

hasType()

Has type

public hasType() : bool
Return values
bool

isAccepted()

Determine if the response is 202 accepted

public isAccepted() : bool|null
Return values
bool|null

isBadGateway()

Determine if the response is a 502 Bad Gateway

public isBadGateway() : bool|null
Return values
bool|null

isBadRequest()

Determine if the response is a 400 Bad Request

public isBadRequest() : bool|null
Return values
bool|null

isClientError()

Determine if the response is a client error

public isClientError() : bool|null
Return values
bool|null

isComplete()

Determine if the response is complete

public isComplete() : bool
Return values
bool

isConflict()

Determine if the response is a 409 Conflict

public isConflict() : bool|null
Return values
bool|null

isContinue()

Determine if the response is a continue

public isContinue() : bool|null
Return values
bool|null

isCreated()

Determine if the response is 201 created

public isCreated() : bool|null
Return values
bool|null

isError()

Determine if the response is an error

public isError() : bool|null
Return values
bool|null

isForbidden()

Determine if the response is a 403 Forbidden

public isForbidden() : bool|null
Return values
bool|null

isFound()

Determine if the response is a 302 Found

public isFound() : bool|null
Return values
bool|null

isInternalServerError()

Determine if the response is a 500 Internal Server Error

public isInternalServerError() : bool|null
Return values
bool|null

isLengthRequired()

Determine if the response is a 411 Length Required

public isLengthRequired() : bool|null
Return values
bool|null

isMethodNotAllowed()

Determine if the response is a 405 Method Not Allowed

public isMethodNotAllowed() : bool|null
Return values
bool|null

isMovedPermanently()

Determine if the response is a 301 Moved Permanently

public isMovedPermanently() : bool|null
Return values
bool|null

isNoContent()

Determine if the response is 204 No Content

public isNoContent() : bool|null
Return values
bool|null

isNotAcceptable()

Determine if the response is a 406 Not Acceptable

public isNotAcceptable() : bool|null
Return values
bool|null

isNotFound()

Determine if the response is a 404 Not Found

public isNotFound() : bool|null
Return values
bool|null

isOk()

Determine if the response is 200 OK

public isOk() : bool|null
Return values
bool|null

isRedirect()

Determine if the response is a redirect

public isRedirect() : bool|null
Return values
bool|null

isRequestTimeout()

Determine if the response is a 408 Request Timeout

public isRequestTimeout() : bool|null
Return values
bool|null

isServerError()

Determine if the response is a server error

public isServerError() : bool|null
Return values
bool|null

isServiceUnavailable()

Determine if the response is a 503 Service Unavailable

public isServiceUnavailable() : bool|null
Return values
bool|null

isSuccess()

Determine if the response is a success

public isSuccess() : bool|null
Return values
bool|null

isTooManyRequests()

Determine if the response is a 429 Too Many Requests

public isTooManyRequests() : bool|null
Return values
bool|null

isUnauthorized()

Determine if the response is a 401 Unauthorized

public isUnauthorized() : bool|null
Return values
bool|null

isUnprocessableEntity()

Determine if the response is a 422 Unprocessable Entity

public isUnprocessableEntity() : bool|null
Return values
bool|null

isUnsupportedMediaType()

Determine if the response is a 415 Unsupported Media Type

public isUnsupportedMediaType() : bool|null
Return values
bool|null

prepare()

Prepare the client request

public prepare([string|null $uri = null ][, string|null $method = null ]) : Client
Parameters
$uri : string|null = null
$method : string|null = null
Tags
throws
Exception|Exception
Return values
Client

removeAllHeaders()

Remove all headers

public removeAllHeaders() : Client
Return values
Client

removeAllQuery()

Remove all query data

public removeAllQuery() : Client
Return values
Client

removeData()

Remove data

public removeData(string $key) : Client
Parameters
$key : string
Return values
Client

removeFile()

Remove file

public removeFile(string $key) : Client
Parameters
$key : string
Return values
Client

removeHeader()

Remove header

public removeHeader(string $name) : Client
Parameters
$name : string
Return values
Client

removeOption()

Remove option

public removeOption(string $name) : Client
Parameters
$name : string
Return values
Client

removeQuery()

Remove query

public removeQuery(string $key) : Client
Parameters
$key : string
Return values
Client

render()

Method to render the request as a string

public render() : string
Return values
string

reset()

Method to reset the client

public reset([bool $data = true ][, bool $headers = false ][, bool $clear = false ]) : static
Parameters
$data : bool = true
$headers : bool = false
$clear : bool = false
Return values
static

send()

Send the client request

public send([string|null $uri = null ][, string|null $method = null ]) : Response|Promise|array<string|int, mixed>|string
Parameters
$uri : string|null = null
$method : string|null = null
Tags
throws
Exception|Exception|Exception
Return values
Response|Promise|array<string|int, mixed>|string

sendAsync()

Method to send the request asynchronously

public sendAsync() : Promise
Return values
Promise

setBodyFromFile()

Set request body from file

public setBodyFromFile(string $file) : Client
Parameters
$file : string
Tags
throws
Exception
Return values
Client

setData()

Set data

public setData(array<string|int, mixed> $data) : Client
Parameters
$data : array<string|int, mixed>
Return values
Client

setFiles()

Set files

public setFiles(array<string|int, mixed>|string $files[, bool $multipart = true ]) : Client
Parameters
$files : array<string|int, mixed>|string
$multipart : bool = true
Tags
throws
Exception
Return values
Client

setHeaders()

Set headers (clear out any existing headers)

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

setMethod()

Set method

public setMethod(string $method) : Client
Parameters
$method : string
Return values
Client

setOptions()

Set options

public setOptions(array<string|int, mixed> $options) : Client

Supported options

  • 'base_uri'
  • 'method'
  • 'headers'
  • 'user_agent'
  • 'query' (can only be encoded query string on the URI)
  • 'data' (can be any request data)
  • 'files'
  • 'type'
  • 'auto'
  • 'async'
  • 'verify_peer'
  • 'allow_self_signed'
  • 'force_custom_method' (Curl only - forces CURLOPT_CUSTOMREQUEST)
Parameters
$options : array<string|int, mixed>
Return values
Client

setQuery()

Set query

public setQuery(array<string|int, mixed> $query) : Client
Parameters
$query : array<string|int, mixed>
Return values
Client

setType()

Set type

public setType(string $type) : Client
Parameters
$type : string
Return values
Client

toCurlCommand()

Method to convert client object to a Curl command for the CLI

public toCurlCommand() : string
Tags
throws
Exception|Exception
Return values
string

        
On this page

Search results