AdsHttpClient extends HttpClient
Table of Contents
Constants
- DEFAULT_SOCKET_TIMEOUT = 30
- DEFAULT_STREAM_TIMEOUT = 60
- DEFAULT_STREAM_TIMEOUT_NO_WAIT = 1
- HTTP_1_0 = '1.0'
- HTTP_1_1 = '1.1'
- HTTP_DELETE = "DELETE"
- HTTP_GET = 'GET'
- HTTP_HEAD = 'HEAD'
- HTTP_OPTIONS = 'OPTIONS'
- HTTP_PATCH = 'PATCH'
- HTTP_POST = 'POST'
- HTTP_PUT = 'PUT'
Methods
- __construct() : mixed
- clearHeaders() : mixed
- Clears all HTTP request header fields.
- delete() : mixed
- disableSslVerification() : $this
- Disables ssl certificate verification.
- download() : bool
- Downloads and saves a file.
- get() : string|bool
- Performs GET request.
- getCharset() : string
- Returns response content encoding.
- getContentType() : string
- Returns response content type.
- getCookies() : HttpCookies
- Returns parsed HTTP response cookies.
- getDebugLevel() : int
- Returns the current level.
- getEffectiveUrl() : string
- Returns URL of the last redirect if request was redirected, or initial URL if request was not redirected.
- getError() : array<string|int, mixed>
- Returns array of errors on failure.
- getHeaders() : HttpHeaders
- Returns parsed HTTP response headers.
- getPeerAddress() : string|false
- Returns remote peer ip address (only if privateIp = false).
- getRequestHeaders() : HttpHeaders
- Returns HTTP request headers.
- getResponse() : Response|null
- Returns PSR-7 response.
- getResult() : string
- Returns HTTP response entity string. Note, if outputStream is set, the result will be the empty string.
- getStatus() : int
- Returns HTTP response status code.
- head() : HttpHeaders|bool
- Performs HEAD request.
- post() : string|bool
- Performs POST request.
- query() : bool
- Perfoms HTTP request.
- saveFile() : mixed
- Saves a downloaded file.
- sendAsyncRequest() : Promise
- sendRequest() : ResponseInterface
- setAuthorization() : $this
- Sets Basic Authorization request header field.
- setBodyLengthMax() : $this
- Sets the maximum body length that will be received in $this->readBody().
- setCharset() : $this
- Sets charset for the entity-body (used in the Content-Type request header field for POST and PUT).
- setCompress() : $this
- Sets compression option.
- setContextOptions() : $this
- Sets context options and parameters.
- setCookies() : $this
- Sets an array of cookies for HTTP request. Warning! Replaces 'Cookie' header.
- setDebugLevel() : mixed
- Sets debug level using HttpDebug::* constants.
- setHeader() : $this
- Sets an HTTP request header.
- setHeaders() : $this
- Sets an array of headers for HTTP request.
- setOutputStream() : $this
- Sets the response output to the stream instead of the string result. Useful for large responses.
- setPrivateIp() : $this
- Enables or disables requests to private IPs.
- setProxy() : $this
- Sets HTTP proxy for request.
- setRedirect() : $this
- Sets redirect options.
- setResponseBuilder() : $this
- Sets a builder for a response.
- setStreamTimeout() : $this
- Sets socket stream reading timeout.
- setTimeout() : $this
- Sets connection timeout.
- setVersion() : $this
- Sets HTTP protocol version. In version 1.1 chunked response is possible.
- shouldFetchBody() : $this
- Sets a callback called before fetching a message body.
- wait() : array<string|int, ResponseInterface>
- Waits for async promises and returns responses from processed promises.
- waitResponse() : $this
- Sets response body waiting option.
Constants
DEFAULT_SOCKET_TIMEOUT
public
mixed
DEFAULT_SOCKET_TIMEOUT
= 30
DEFAULT_STREAM_TIMEOUT
public
mixed
DEFAULT_STREAM_TIMEOUT
= 60
DEFAULT_STREAM_TIMEOUT_NO_WAIT
public
mixed
DEFAULT_STREAM_TIMEOUT_NO_WAIT
= 1
HTTP_1_0
public
mixed
HTTP_1_0
= '1.0'
HTTP_1_1
public
mixed
HTTP_1_1
= '1.1'
HTTP_DELETE
public
mixed
HTTP_DELETE
= "DELETE"
HTTP_GET
public
mixed
HTTP_GET
= 'GET'
HTTP_HEAD
public
mixed
HTTP_HEAD
= 'HEAD'
HTTP_OPTIONS
public
mixed
HTTP_OPTIONS
= 'OPTIONS'
HTTP_PATCH
public
mixed
HTTP_PATCH
= 'PATCH'
HTTP_POST
public
mixed
HTTP_POST
= 'POST'
HTTP_PUT
public
mixed
HTTP_PUT
= 'PUT'
Methods
__construct()
public
__construct([array<string|int, mixed>|null $options = null ]) : mixed
Parameters
- $options : array<string|int, mixed>|null = null
-
Optional array with options: "redirect" bool Follow redirects (default true). "redirectMax" int Maximum number of redirects (default 5). "waitResponse" bool Read the body or disconnect just after reading headers (default true). "socketTimeout" int Connection timeout in seconds (default 30). "streamTimeout" int Stream reading timeout in seconds (default 60 for waitResponse == true and 1 for waitResponse == false). "version" string HTTP version (HttpClient::HTTP_1_0, HttpClient::HTTP_1_1) (default "1.1"). "proxyHost" string Proxy host name/address. "proxyPort" int Proxy port number. "proxyUser" string Proxy username. "proxyPassword" string Proxy password. "compress" bool Accept gzip encoding (default false). "charset" string Charset for body in POST and PUT. "disableSslVerification" bool Pass true to disable ssl check. "bodyLengthMax" int Maximum length of the body. "privateIp" bool Enable or disable requests to private IPs (default true). "debugLevel" int Debug level using HttpDebug::* constants. "cookies" array of cookies for HTTP request. "headers" array of headers for HTTP request. "useCurl" bool Enable CURL (default false). "curlLogFile" string Full path to CURL log file. "responseBuilder" Http\ResponseBuilderInterface Response builder. Almost all options can be set separately with setters.
clearHeaders()
Clears all HTTP request header fields.
public
clearHeaders() : mixed
delete()
public
delete(mixed $url[, mixed $postData = null ][, mixed $multipart = false ]) : mixed
Parameters
- $url : mixed
- $postData : mixed = null
- $multipart : mixed = false
disableSslVerification()
Disables ssl certificate verification.
public
disableSslVerification() : $this
Return values
$thisdownload()
Downloads and saves a file.
public
download(string $url, string $filePath[, string $method = HttpMethod::GET ][, mixed $entityBody = null ]) : bool
Parameters
- $url : string
-
URI to download.
- $filePath : string
-
Absolute file path.
- $method : string = HttpMethod::GET
- $entityBody : mixed = null
Return values
boolget()
Performs GET request.
public
get(string $url) : string|bool
Parameters
- $url : string
-
Absolute URI e.g. "http://user:pass @ host:port/path/?query".
Return values
string|bool —Response entity string or false on error. Note, it's empty string if outputStream is set.
getCharset()
Returns response content encoding.
public
getCharset() : string
Return values
stringgetContentType()
Returns response content type.
public
getContentType() : string
Return values
stringgetCookies()
Returns parsed HTTP response cookies.
public
getCookies() : HttpCookies
Return values
HttpCookiesgetDebugLevel()
Returns the current level.
public
getDebugLevel() : int
Return values
int —HttpDebug::* constants
getEffectiveUrl()
Returns URL of the last redirect if request was redirected, or initial URL if request was not redirected.
public
getEffectiveUrl() : string
Return values
stringgetError()
Returns array of errors on failure.
public
getError() : array<string|int, mixed>
Return values
array<string|int, mixed> —Array with "error_code" => "error_message" pair
getHeaders()
Returns parsed HTTP response headers.
public
getHeaders() : HttpHeaders
Return values
HttpHeadersgetPeerAddress()
Returns remote peer ip address (only if privateIp = false).
public
getPeerAddress() : string|false
Return values
string|falsegetRequestHeaders()
Returns HTTP request headers.
public
getRequestHeaders() : HttpHeaders
Return values
HttpHeadersgetResponse()
Returns PSR-7 response.
public
getResponse() : Response|null
Return values
Response|nullgetResult()
Returns HTTP response entity string. Note, if outputStream is set, the result will be the empty string.
public
getResult() : string
Return values
stringgetStatus()
Returns HTTP response status code.
public
getStatus() : int
Return values
inthead()
Performs HEAD request.
public
head(string $url) : HttpHeaders|bool
Parameters
- $url : string
-
Absolute URI e.g. "http://user:pass @ host:port/path/?query"
Return values
HttpHeaders|bool —Response headers or false on error.
post()
Performs POST request.
public
post(string $url[, array<string|int, mixed>|string|resource $postData = null ][, bool $multipart = false ]) : string|bool
Parameters
- $url : string
-
Absolute URI e.g. "http://user:pass @ host:port/path/?query".
- $postData : array<string|int, mixed>|string|resource = null
-
Entity of POST/PUT request. If it's resource handler then data will be read directly from the stream.
- $multipart : bool = false
-
Whether to use multipart/form-data encoding. If true, method accepts file as a resource or as an array with keys 'resource' (or 'content') and optionally 'filename' and 'contentType'
Return values
string|bool —Response entity string or false on error. Note, it's empty string if outputStream is set.
query()
Perfoms HTTP request.
public
query(string $method, string $url[, array<string|int, mixed>|string|resource|Stream $entityBody = null ]) : bool
Parameters
- $method : string
-
HTTP method (GET, POST, etc.). Note, it must be in UPPERCASE.
- $url : string
-
Absolute URI e.g. "http://user:pass @ host:port/path/?query".
- $entityBody : array<string|int, mixed>|string|resource|Stream = null
-
Entity body of the request. If it's resource handler then data will be read directly from the stream.
Return values
bool —Query result (true or false). Response entity string can be got via getResult() method. Note, it's empty string if outputStream is set.
saveFile()
Saves a downloaded file.
public
saveFile(string $filePath) : mixed
Parameters
- $filePath : string
-
Absolute file path.
sendAsyncRequest()
public
sendAsyncRequest(RequestInterface $request) : Promise
Parameters
- $request : RequestInterface
Tags
Return values
PromisesendRequest()
public
sendRequest(RequestInterface $request) : ResponseInterface
Parameters
- $request : RequestInterface
Tags
Return values
ResponseInterfacesetAuthorization()
Sets Basic Authorization request header field.
public
setAuthorization(string $user, string $pass) : $this
Parameters
- $user : string
-
Username.
- $pass : string
-
Password.
Return values
$thissetBodyLengthMax()
Sets the maximum body length that will be received in $this->readBody().
public
setBodyLengthMax(int $bodyLengthMax) : $this
Parameters
- $bodyLengthMax : int
Return values
$thissetCharset()
Sets charset for the entity-body (used in the Content-Type request header field for POST and PUT).
public
setCharset(string $value) : $this
Parameters
- $value : string
-
Charset.
Return values
$thissetCompress()
Sets compression option.
public
setCompress(bool $value) : $this
Consider not to use the "compress" option with the output stream if a content can be large. Note, that compressed response is processed anyway if Content-Encoding response header field is set
Parameters
- $value : bool
-
If true, "Accept-Encoding: gzip" will be sent.
Return values
$thissetContextOptions()
Sets context options and parameters.
public
setContextOptions(array<string|int, mixed> $options) : $this
Parameters
- $options : array<string|int, mixed>
-
Context options and parameters
Return values
$thissetCookies()
Sets an array of cookies for HTTP request. Warning! Replaces 'Cookie' header.
public
setCookies(array<string|int, mixed> $cookies) : $this
Parameters
- $cookies : array<string|int, mixed>
-
Array of cookie_name => value pairs.
Return values
$thissetDebugLevel()
Sets debug level using HttpDebug::* constants.
public
setDebugLevel(int $debugLevel) : mixed
Parameters
- $debugLevel : int
setHeader()
Sets an HTTP request header.
public
setHeader(string $name, string $value[, bool $replace = true ]) : $this
Parameters
- $name : string
-
Name of the header field.
- $value : string
-
Value of the field.
- $replace : bool = true
-
Replace existing header field with the same name or add one more.
Return values
$thissetHeaders()
Sets an array of headers for HTTP request.
public
setHeaders(array<string|int, mixed> $headers) : $this
Parameters
- $headers : array<string|int, mixed>
-
Array of header_name => value pairs.
Return values
$thissetOutputStream()
Sets the response output to the stream instead of the string result. Useful for large responses.
public
setOutputStream(resource $handler) : $this
Note, the stream must be readable/writable to support a compressed response. Note, in this mode the result string is empty. Note, only Http\Stream response body is supported.
Parameters
- $handler : resource
-
File or stream handler.
Return values
$thissetPrivateIp()
Enables or disables requests to private IPs.
public
setPrivateIp(bool $value) : $this
Parameters
- $value : bool
Return values
$thissetProxy()
Sets HTTP proxy for request.
public
setProxy(string $proxyHost[, null|int $proxyPort = null ][, null|string $proxyUser = null ][, null|string $proxyPassword = null ]) : $this
Parameters
- $proxyHost : string
-
Proxy host name or address (without "http://").
- $proxyPort : null|int = null
-
Proxy port number.
- $proxyUser : null|string = null
-
Proxy username.
- $proxyPassword : null|string = null
-
Proxy password.
Return values
$thissetRedirect()
Sets redirect options.
public
setRedirect(bool $value[, null|int $max = null ]) : $this
Parameters
- $value : bool
-
If true, do redirect (default true).
- $max : null|int = null
-
Maximum allowed redirect count.
Return values
$thissetResponseBuilder()
Sets a builder for a response.
public
setResponseBuilder(ResponseBuilderInterface $responseBuilder) : $this
Parameters
- $responseBuilder : ResponseBuilderInterface
Return values
$thissetStreamTimeout()
Sets socket stream reading timeout.
public
setStreamTimeout(int $value) : $this
Parameters
- $value : int
-
Stream reading timeout in seconds; "0" means no timeout (default 60).
Return values
$thissetTimeout()
Sets connection timeout.
public
setTimeout(int $value) : $this
Parameters
- $value : int
-
Connection timeout in seconds (default 30).
Return values
$thissetVersion()
Sets HTTP protocol version. In version 1.1 chunked response is possible.
public
setVersion(string $value) : $this
Parameters
- $value : string
-
Version "1.0" or "1.1" (default "1.0").
Return values
$thisshouldFetchBody()
Sets a callback called before fetching a message body.
public
shouldFetchBody(callable $callback) : $this
Parameters
- $callback : callable
Return values
$thiswait()
Waits for async promises and returns responses from processed promises.
public
wait() : array<string|int, ResponseInterface>
Return values
array<string|int, ResponseInterface>waitResponse()
Sets response body waiting option.
public
waitResponse(bool $value) : $this
Parameters
- $value : bool
-
If true, wait for response body. If false, disconnect just after reading headers (default true).