Client
in package
implements
DispatchableInterface
Http client
Table of Contents
Interfaces
Constants
- AUTH_BASIC = 'basic'
- AUTH_DIGEST = 'digest'
- DIGEST_CNONCE = 'cnonce'
- DIGEST_NC = 'nc'
- DIGEST_NONCE = 'nonce'
- DIGEST_OPAQUE = 'opaque'
- DIGEST_QOP = 'qop'
- DIGEST_REALM = 'realm'
- ENC_FORMDATA = 'multipart/form-data'
- ENC_URLENCODED = 'application/x-www-form-urlencoded'
Properties
- $adapter : AdapterInterface
- $auth : array<string|int, mixed>
- $config : array<string|int, mixed>
- Configuration array, set using the constructor or using ::setOptions()
- $cookies : array<string|int, mixed>
- $encType : string
- $fileInfoDb : resource
- Fileinfo magic database resource
- $lastRawRequest : Request
- $lastRawResponse : Response
- $redirectCounter : int
- $request : Request
- $response : Response
- $streamHandle : resource|null
- $streamName : string
Methods
- __construct() : mixed
- Constructor
- addCookie() : $this
- Add a cookie
- clearAuth() : mixed
- Clear http authentication
- clearCookies() : mixed
- Clear all the cookies
- dispatch() : ResponseInterface
- Dispatch
- encodeAuthHeader() : string
- Create a HTTP authentication "Authorization:" header according to the specified user, password and authentication method.
- encodeFormData() : string
- Encode data to a multipart/form-data part suitable for a POST request.
- getAdapter() : AdapterInterface
- Load the connection adapter
- getArgSeparator() : string
- Get the query string argument separator
- getCookies() : array<string|int, mixed>
- Return the current cookies
- getEncType() : string
- Get the encoding type
- getHeader() : string|bool
- Get the header value of the request
- getLastRawRequest() : string
- Get the last request (as a string)
- getLastRawResponse() : string
- Get the last response (as a string)
- getMethod() : string
- Get the HTTP method
- getRedirectionsCount() : int
- Get the redirections count
- getRequest() : Request
- Get Request
- getResponse() : Response
- Get Response
- getStream() : bool|string
- Get status of streaming for received data
- getUri() : Http
- Get uri (from the request)
- hasHeader() : bool
- Check if exists the header type specified
- removeFileUpload() : bool
- Remove a file to upload
- reset() : $this
- Fully reset the HTTP client (auth, cookies, request, response, etc.)
- resetParameters() : $this
- Reset all the HTTP parameters (request, response, etc)
- send() : Response
- Send HTTP request
- setAdapter() : $this
- Load the connection adapter
- setArgSeparator() : $this
- Set the query string argument separator
- setAuth() : $this
- Create a HTTP authentication "Authorization:" header according to the specified user, password and authentication method.
- setCookies() : $this
- Set an array of cookies
- setEncType() : $this
- Set the encoding type and the boundary (if any)
- setFileUpload() : $this
- Set a file to upload (using a POST request)
- setHeaders() : $this
- Set the headers (for the request)
- setMethod() : $this
- Set the HTTP method (to the request)
- setOptions() : $this
- Set configuration parameters for this HTTP client
- setParameterGet() : $this
- Set the GET parameters
- setParameterPost() : $this
- Set the POST parameters
- setRawBody() : $this
- Set raw body (for advanced use cases)
- setRequest() : $this
- Set request
- setResponse() : $this
- Set response
- setStream() : $this
- Set streaming for received data
- setUri() : $this
- Set Uri (to the request)
- calcAuthDigest() : string|bool
- Calculate the response value according to the HTTP authentication type
- detectFileMimeType() : string
- Attempt to detect the MIME type of a file using available extensions
- doRequest() : string
- Separating this from send method allows subclasses to wrap the interaction with the adapter
- flattenParametersArray() : array<string|int, mixed>
- Convert an array of parameters into a flat array of (key, value) pairs
- getCookieId() : string|bool
- Get the cookie Id (name+domain+path)
- openTempStream() : resource
- Create temporary stream
- prepareBody() : string
- Prepare the request body (for PATCH, POST and PUT requests)
- prepareCookies() : Cookie|bool
- Prepare Cookies
- prepareHeaders() : array<string|int, mixed>
- Prepare the request headers
Constants
AUTH_BASIC
public
mixed
AUTH_BASIC
= 'basic'
Tags
AUTH_DIGEST
public
mixed
AUTH_DIGEST
= 'digest'
DIGEST_CNONCE
public
mixed
DIGEST_CNONCE
= 'cnonce'
DIGEST_NC
public
mixed
DIGEST_NC
= 'nc'
DIGEST_NONCE
public
mixed
DIGEST_NONCE
= 'nonce'
DIGEST_OPAQUE
public
mixed
DIGEST_OPAQUE
= 'opaque'
DIGEST_QOP
public
mixed
DIGEST_QOP
= 'qop'
DIGEST_REALM
public
mixed
DIGEST_REALM
= 'realm'
Tags
ENC_FORMDATA
public
mixed
ENC_FORMDATA
= 'multipart/form-data'
ENC_URLENCODED
public
mixed
ENC_URLENCODED
= 'application/x-www-form-urlencoded'
Tags
Properties
$adapter
protected
AdapterInterface
$adapter
$auth
protected
array<string|int, mixed>
$auth
= []
$config
Configuration array, set using the constructor or using ::setOptions()
protected
array<string|int, mixed>
$config
= ['maxredirects' => 5, 'strictredirects' => false, 'useragent' => 'Laminas_Http_Client', 'timeout' => 10, 'connecttimeout' => null, 'adapter' => \Laminas\Http\Client\Adapter\Socket::class, 'httpversion' => \Laminas\Http\Request::VERSION_11, 'storeresponse' => true, 'keepalive' => false, 'outputstream' => false, 'encodecookies' => true, 'argseparator' => null, 'rfc3986strict' => false, 'sslcafile' => null, 'sslcapath' => null]
$cookies
protected
array<string|int, mixed>
$cookies
= []
of Header\SetCookie
$encType
protected
string
$encType
= ''
$fileInfoDb
Fileinfo magic database resource
protected
static resource
$fileInfoDb
This variable is populated the first time _detectFileMimeType is called and is then reused on every call to this method
$lastRawRequest
protected
Request
$lastRawRequest
$lastRawResponse
protected
Response
$lastRawResponse
$redirectCounter
protected
int
$redirectCounter
= 0
$request
protected
Request
$request
$response
protected
Response
$response
$streamHandle
protected
resource|null
$streamHandle
$streamName
protected
string
$streamName
Methods
__construct()
Constructor
public
__construct([string $uri = null ][, array<string|int, mixed>|Traversable $options = null ]) : mixed
Parameters
- $uri : string = null
- $options : array<string|int, mixed>|Traversable = null
addCookie()
Add a cookie
public
addCookie(array<string|int, mixed>|ArrayIterator|SetCookie|string $cookie[, string $value = null ][, string $expire = null ][, string $path = null ][, string $domain = null ][, bool $secure = false ][, bool $httponly = true ][, string $maxAge = null ][, string $version = null ]) : $this
Parameters
- $cookie : array<string|int, mixed>|ArrayIterator|SetCookie|string
- $value : string = null
- $expire : string = null
- $path : string = null
- $domain : string = null
- $secure : bool = false
- $httponly : bool = true
- $maxAge : string = null
- $version : string = null
Tags
Return values
$thisclearAuth()
Clear http authentication
public
clearAuth() : mixed
clearCookies()
Clear all the cookies
public
clearCookies() : mixed
dispatch()
Dispatch
public
dispatch(RequestInterface $request[, ResponseInterface|null $response = null ]) : ResponseInterface
Parameters
- $request : RequestInterface
- $response : ResponseInterface|null = null
Return values
ResponseInterfaceencodeAuthHeader()
Create a HTTP authentication "Authorization:" header according to the specified user, password and authentication method.
public
static encodeAuthHeader(string $user, string $password[, string $type = self::AUTH_BASIC ]) : string
Parameters
- $user : string
- $password : string
- $type : string = self::AUTH_BASIC
Tags
Return values
stringencodeFormData()
Encode data to a multipart/form-data part suitable for a POST request.
public
encodeFormData(string $boundary, string $name, mixed $value[, string $filename = null ][, array<string|int, mixed> $headers = [] ]) : string
Parameters
- $boundary : string
- $name : string
- $value : mixed
- $filename : string = null
- $headers : array<string|int, mixed> = []
-
Associative array of optional headers @example ("Content-Transfer-Encoding" => "binary")
Return values
stringgetAdapter()
Load the connection adapter
public
getAdapter() : AdapterInterface
Return values
AdapterInterfacegetArgSeparator()
Get the query string argument separator
public
getArgSeparator() : string
Return values
stringgetCookies()
Return the current cookies
public
getCookies() : array<string|int, mixed>
Return values
array<string|int, mixed>getEncType()
Get the encoding type
public
getEncType() : string
Return values
stringgetHeader()
Get the header value of the request
public
getHeader(string $name) : string|bool
Parameters
- $name : string
Return values
string|boolgetLastRawRequest()
Get the last request (as a string)
public
getLastRawRequest() : string
Return values
stringgetLastRawResponse()
Get the last response (as a string)
public
getLastRawResponse() : string
Return values
stringgetMethod()
Get the HTTP method
public
getMethod() : string
Return values
stringgetRedirectionsCount()
Get the redirections count
public
getRedirectionsCount() : int
Return values
intgetRequest()
Get Request
public
getRequest() : Request
Return values
RequestgetResponse()
Get Response
public
getResponse() : Response
Return values
ResponsegetStream()
Get status of streaming for received data
public
getStream() : bool|string
Return values
bool|stringgetUri()
Get uri (from the request)
public
getUri() : Http
Return values
HttphasHeader()
Check if exists the header type specified
public
hasHeader(string $name) : bool
Parameters
- $name : string
Return values
boolremoveFileUpload()
Remove a file to upload
public
removeFileUpload(string $filename) : bool
Parameters
- $filename : string
Return values
boolreset()
Fully reset the HTTP client (auth, cookies, request, response, etc.)
public
reset() : $this
Return values
$thisresetParameters()
Reset all the HTTP parameters (request, response, etc)
public
resetParameters([bool $clearCookies = false ]) : $this
Parameters
- $clearCookies : bool = false
-
Also clear all valid cookies? (defaults to false)
Return values
$thissend()
Send HTTP request
public
send([Request|null $request = null ]) : Response
Parameters
- $request : Request|null = null
Tags
Return values
ResponsesetAdapter()
Load the connection adapter
public
setAdapter(AdapterInterface|string $adapter) : $this
While this method is not called more than one for a client, it is separated from ->request() to preserve logic and readability
Parameters
- $adapter : AdapterInterface|string
Tags
Return values
$thissetArgSeparator()
Set the query string argument separator
public
setArgSeparator(string $argSeparator) : $this
Parameters
- $argSeparator : string
Return values
$thissetAuth()
Create a HTTP authentication "Authorization:" header according to the specified user, password and authentication method.
public
setAuth(string $user, string $password[, string $type = self::AUTH_BASIC ]) : $this
Parameters
- $user : string
- $password : string
- $type : string = self::AUTH_BASIC
Tags
Return values
$thissetCookies()
Set an array of cookies
public
setCookies(array<string|int, mixed>|array<string|int, SetCookie> $cookies) : $this
Parameters
- $cookies : array<string|int, mixed>|array<string|int, SetCookie>
-
Cookies as name=>value pairs or instances of SetCookie.
Tags
Return values
$thissetEncType()
Set the encoding type and the boundary (if any)
public
setEncType(string $encType[, string $boundary = null ]) : $this
Parameters
- $encType : string
- $boundary : string = null
Return values
$thissetFileUpload()
Set a file to upload (using a POST request)
public
setFileUpload(string $filename, string $formname[, string $data = null ][, string $ctype = null ]) : $this
Can be used in two ways:
- $data is null (default): $filename is treated as the name if a local file which will be read and sent. Will try to guess the content type using mime_content_type().
- $data is set - $filename is sent as the file name, but $data is sent as the file contents and no file is read from the file system. In this case, you need to manually set the Content-Type ($ctype) or it will default to application/octet-stream.
Parameters
- $filename : string
-
Name of file to upload, or name to save as
- $formname : string
-
Name of form element to send as
- $data : string = null
-
Data to send (if null, $filename is read and sent)
- $ctype : string = null
-
Content type to use (if $data is set and $ctype is null, will be application/octet-stream)
Tags
Return values
$thissetHeaders()
Set the headers (for the request)
public
setHeaders(Headers|array<string|int, mixed> $headers) : $this
Parameters
- $headers : Headers|array<string|int, mixed>
Tags
Return values
$thissetMethod()
Set the HTTP method (to the request)
public
setMethod(string $method) : $this
Parameters
- $method : string
Return values
$thissetOptions()
Set configuration parameters for this HTTP client
public
setOptions([array<string|int, mixed>|Traversable $options = [] ]) : $this
Parameters
- $options : array<string|int, mixed>|Traversable = []
Tags
Return values
$thissetParameterGet()
Set the GET parameters
public
setParameterGet(array<string|int, mixed> $query) : $this
Parameters
- $query : array<string|int, mixed>
Return values
$thissetParameterPost()
Set the POST parameters
public
setParameterPost(array<string|int, mixed> $post) : $this
Parameters
- $post : array<string|int, mixed>
Return values
$thissetRawBody()
Set raw body (for advanced use cases)
public
setRawBody(string $body) : $this
Parameters
- $body : string
Return values
$thissetRequest()
Set request
public
setRequest(Request $request) : $this
Parameters
- $request : Request
Return values
$thissetResponse()
Set response
public
setResponse(Response $response) : $this
Parameters
- $response : Response
Return values
$thissetStream()
Set streaming for received data
public
setStream([string|bool $streamfile = true ]) : $this
Parameters
- $streamfile : string|bool = true
-
Stream file, true for temp file, false/null for no streaming
Return values
$thissetUri()
Set Uri (to the request)
public
setUri(string|Http $uri) : $this
Parameters
- $uri : string|Http
Return values
$thiscalcAuthDigest()
Calculate the response value according to the HTTP authentication type
protected
calcAuthDigest(string $user, string $password[, string $type = self::AUTH_BASIC ][, array<string|int, mixed> $digest = [] ][, null|string $entityBody = null ]) : string|bool
Parameters
- $user : string
- $password : string
- $type : string = self::AUTH_BASIC
- $digest : array<string|int, mixed> = []
- $entityBody : null|string = null
Tags
Return values
string|booldetectFileMimeType()
Attempt to detect the MIME type of a file using available extensions
protected
detectFileMimeType(string $file) : string
This method will try to detect the MIME type of a file. If the fileinfo extension is available, it will be used. If not, the mime_magic extension which is deprecated but is still available in many PHP setups will be tried.
If neither extension is available, the default application/octet-stream MIME type will be returned
Parameters
- $file : string
-
File path
Return values
string —MIME type
doRequest()
Separating this from send method allows subclasses to wrap the interaction with the adapter
protected
doRequest(Http $uri, string $method[, bool $secure = false ][, array<string|int, mixed> $headers = [] ][, string $body = '' ]) : string
Parameters
- $uri : Http
- $method : string
- $secure : bool = false
- $headers : array<string|int, mixed> = []
- $body : string = ''
Tags
Return values
string —the raw response
flattenParametersArray()
Convert an array of parameters into a flat array of (key, value) pairs
protected
flattenParametersArray(array<string|int, mixed> $parray[, string $prefix = null ]) : array<string|int, mixed>
Will flatten a potentially multi-dimentional array of parameters (such as POST parameters) into a flat array of (key, value) paris. In case of multi-dimentional arrays, square brackets ([]) will be added to the key to indicate an array.
Parameters
- $parray : array<string|int, mixed>
- $prefix : string = null
Tags
Return values
array<string|int, mixed>getCookieId()
Get the cookie Id (name+domain+path)
protected
getCookieId(SetCookie|Cookie $cookie) : string|bool
Parameters
Return values
string|boolopenTempStream()
Create temporary stream
protected
openTempStream() : resource
Tags
Return values
resourceprepareBody()
Prepare the request body (for PATCH, POST and PUT requests)
protected
prepareBody() : string
Tags
Return values
stringprepareCookies()
Prepare Cookies
protected
prepareCookies(string $domain, string $path, bool $secure) : Cookie|bool
Parameters
- $domain : string
- $path : string
- $secure : bool
Return values
Cookie|boolprepareHeaders()
Prepare the request headers
protected
prepareHeaders(resource|string $body, Http $uri) : array<string|int, mixed>
Parameters
- $body : resource|string
- $uri : Http