Http client

package Default

 Methods

Constructor

__construct(string $uri = null, array|\Traversable $options = null

Parameters

$uri

string

$options

array\Traversable

Add a cookie

addCookie(array|\ArrayIterator|\Zend\Http\Header\SetCookie|string $cookie, string $value = null, string $expire = null, string $path = null, string $domain = null, boolean $secure = false, boolean $httponly = true, string $maxAge = null, string $version = null) : \Zend\Http\Client

Parameters

$cookie

array\ArrayIterator\Zend\Http\Header\SetCookiestring

$value

string

$expire

string

$path

string

$domain

string

$secure

boolean

$httponly

boolean

$maxAge

string

$version

string

Exceptions

\Zend\Http\Exception\InvalidArgumentException

Returns

\Zend\Http\Client

Clear http authentication

clearAuth() 

Clear all the cookies

clearCookies() 

Dispatch

dispatch(\Zend\Stdlib\RequestInterface $request, \Zend\Stdlib\ResponseInterface $response = null) : \Zend\Stdlib\ResponseInterface
inherited_from \Zend\Stdlib\DispatchableInterface::dispatch()

Parameters

$request

\Zend\Stdlib\RequestInterface

$response

\Zend\Stdlib\ResponseInterface

Returns

\Zend\Stdlib\ResponseInterface

Create a HTTP authentication "Authorization:" header according to the specified user, password and authentication method.

encodeAuthHeader(string $user, string $password, string $type = self::AUTH_BASIC) : string
Static
see

Parameters

$user

string

$password

string

$type

string

Exceptions

\Zend\Http\Client\Exception\InvalidArgumentException

Returns

string

Encode data to a multipart/form-data part suitable for a POST request.

encodeFormData(string $boundary, string $name, mixed $value, string $filename = null, array $headers = array()) : string

Parameters

$boundary

string

$name

string

$value

mixed

$filename

string

$headers

array

Associative array of optional headers @example ("Content-Transfer-Encoding" => "binary")

Returns

string

Load the connection adapter

getAdapter() : \Zend\Http\Client\Adapter\AdapterInterface

Returns

\Zend\Http\Client\Adapter\AdapterInterface$adapter

Get the query string argument separator

getArgSeparator() : string

Returns

string

Return the current cookies

getCookies() : array

Returns

array

Get the encoding type

getEncType() : string

Returns

string

Get the header value of the request

getHeader(string $name) : string | boolean

Parameters

$name

string

Returns

stringboolean

Get the last request (as a string)

getLastRawRequest() : string

Returns

string

Get the last response (as a string)

getLastRawResponse() : string

Returns

string

Get the HTTP method

getMethod() : string

Returns

string

Get the redirections count

getRedirectionsCount() : integer

Returns

integer

Get Request

getRequest() : \Zend\Http\Request

Returns

\Zend\Http\Request

Get Response

getResponse() : \Zend\Http\Response

Returns

\Zend\Http\Response

Get status of streaming for received data

getStream() : boolean | string

Returns

booleanstring

Get uri (from the request)

getUri() : \Zend\Uri\Http

Returns

\Zend\Uri\Http

Check if exists the header type specified

hasHeader(string $name) : boolean

Parameters

$name

string

Returns

boolean

Remove a file to upload

removeFileUpload(string $filename) : boolean

Parameters

$filename

string

Returns

boolean

Fully reset the HTTP client (auth, cookies, request, response, etc.)

reset() : \Zend\Http\Client

Returns

\Zend\Http\Client

Reset all the HTTP parameters (request, response, etc)

resetParameters(boolean $clearCookies = false) : \Zend\Http\Client

Parameters

$clearCookies

boolean

Also clear all valid cookies? (defaults to false)

Returns

\Zend\Http\Client

Send HTTP request

send(\Zend\Http\Request $request = null) : \Zend\Http\Response

Parameters

$request

\Zend\Http\Request

Exceptions

\Zend\Http\Exception\RuntimeException
\Zend\Http\Client\Exception\RuntimeException

Returns

\Zend\Http\Response

Load the connection adapter

setAdapter(\Zend\Http\Client\Adapter\AdapterInterface|string $adapter) : \Zend\Http\Client

While this method is not called more than one for a client, it is separated from ->request() to preserve logic and readability

Parameters

$adapter

\Zend\Http\Client\Adapter\AdapterInterfacestring

Exceptions

\Zend\Http\Client\Exception\InvalidArgumentException

Returns

\Zend\Http\Client

Set the query string argument separator

setArgSeparator(string $argSeparator) : \Zend\Http\Client

Parameters

$argSeparator

string

Returns

\Zend\Http\Client

Create a HTTP authentication "Authorization:" header according to the specified user, password and authentication method.

setAuth(string $user, string $password, string $type = self::AUTH_BASIC) : \Zend\Http\Client

Parameters

$user

string

$password

string

$type

string

Exceptions

\Zend\Http\Exception\InvalidArgumentException

Returns

\Zend\Http\Client

Set an array of cookies

setCookies(array $cookies) : \Zend\Http\Client

Parameters

$cookies

array

Exceptions

\Zend\Http\Exception\InvalidArgumentException

Returns

\Zend\Http\Client

Set the encoding type and the boundary (if any)

setEncType(string $encType, string $boundary = null) : \Zend\Http\Client

Parameters

$encType

string

$boundary

string

Returns

\Zend\Http\Client

Set a file to upload (using a POST request)

setFileUpload(string $filename, string $formname, string $data = null, string $ctype = null) : \Zend\Http\Client

Can be used in two ways:

  1. $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().
  2. $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

Data to send (if null, $filename is read and sent)

$ctype

string

Content type to use (if $data is set and $ctype is null, will be application/octet-stream)

Exceptions

\Zend\Http\Exception\RuntimeException

Returns

\Zend\Http\Client

Set the headers (for the request)

setHeaders(\Zend\Http\Headers|array $headers) : \Zend\Http\Client

Parameters

$headers

\Zend\Http\Headersarray

Exceptions

\Zend\Http\Exception\InvalidArgumentException

Returns

\Zend\Http\Client

Set the HTTP method (to the request)

setMethod(string $method) : \Zend\Http\Client

Parameters

$method

string

Returns

\Zend\Http\Client

Set configuration parameters for this HTTP client

setOptions(array|\Traversable $options = array()) : \Zend\Http\Client

Parameters

$options

array\Traversable

Exceptions

\Zend\Http\Client\Exception\InvalidArgumentException

Returns

\Zend\Http\Client

Set the GET parameters

setParameterGet(array $query) : \Zend\Http\Client

Parameters

$query

array

Returns

\Zend\Http\Client

Set the POST parameters

setParameterPost(array $post) : \Zend\Http\Client

Parameters

$post

array

Returns

\Zend\Http\Client

Set raw body (for advanced use cases)

setRawBody(string $body) : \Zend\Http\Client

Parameters

$body

string

Returns

\Zend\Http\Client

Set request

setRequest(\Zend\Http\Request $request) : \Zend\Http\Client

Parameters

$request

\Zend\Http\Request

Returns

\Zend\Http\Client

Set response

setResponse(\Zend\Http\Response $response) : \Zend\Http\Client

Parameters

$response

\Zend\Http\Response

Returns

\Zend\Http\Client

Set streaming for received data

setStream(string|boolean $streamfile = true) : \Zend\Http\Client

Parameters

$streamfile

stringboolean

Stream file, true for temp file, false/null for no streaming

Returns

\Zend\Http\Client

Set Uri (to the request)

setUri(string|\Zend\Uri\Http $uri) : \Zend\Http\Client

Parameters

$uri

string\Zend\Uri\Http

Returns

\Zend\Http\Client

Calculate the response value according to the HTTP authentication type

calcAuthDigest(string $user, string $password, string $type = self::AUTH_BASIC, array $digest = array(), null|string $entityBody = null) : string | boolean
see

Parameters

$user

string

$password

string

$type

string

$digest

array

$entityBody

nullstring

Exceptions

\Zend\Http\Exception\InvalidArgumentException

Returns

stringboolean

Attempt to detect the MIME type of a file using available extensions

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

Returns

stringMIME type

Separating this from send method allows subclasses to wrap the interaction with the adapter

doRequest(\Zend\Uri\Http $uri, string $method, boolean $secure = false, array $headers = array(), string $body = '') : string

Parameters

$uri

\Zend\Uri\Http

$method

string

$secure

boolean

$headers

array

$body

string

Exceptions

\Zend\Http\Exception\RuntimeException

Returns

stringthe raw response

Convert an array of parameters into a flat array of (key, value) pairs

flattenParametersArray(array $parray, string $prefix = null) : array

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.

since 1.9

Parameters

$parray

array

$prefix

string

Returns

array

Get the cookie Id (name+domain+path)

getCookieId(\Zend\Http\Header\SetCookie|\Zend\Http\Header\Cookie $cookie) : string | boolean

Parameters

$cookie

\Zend\Http\Header\SetCookie\Zend\Http\Header\Cookie

Returns

stringboolean

Create temporary stream

openTempStream() : resource

Exceptions

\Zend\Http\Exception\RuntimeException

Returns

resource

Prepare the request body (for PATCH, POST and PUT requests)

prepareBody() : string

Exceptions

\Zend\Http\Client\Exception\RuntimeException

Returns

string

Prepare Cookies

prepareCookies(string $domain, string $path, boolean $secure) : \Zend\Http\Header\Cookie | boolean

Parameters

$domain

string

$path

string

$secure

boolean

Returns

\Zend\Http\Header\Cookieboolean

Prepare the request headers

prepareHeaders(resource|string $body, \Zend\Uri\Http $uri) : array

Parameters

$body

resourcestring

$uri

\Zend\Uri\Http

Exceptions

\Zend\Http\Exception\RuntimeException

Returns

array

 Properties

 

$adapter

$adapter : \Zend\Http\Client\Adapter\AdapterInterface

Default

 

$auth

$auth : array

Default

array()
 

Configuration array, set using the constructor or using ::setOptions()

$config : array

Default

array('maxredirects' => 5, 'strictredirects' => false, 'useragent' => 'Zend\Http\Client', 'timeout' => 10, 'adapter' => 'Zend\Http\Client\Adapter\Socket', 'httpversion' => \Zend\Http\Request::VERSION_11, 'storeresponse' => true, 'keepalive' => false, 'outputstream' => false, 'encodecookies' => true, 'argseparator' => null, 'rfc3986strict' => false)
 

of Header\SetCookie

$cookies : array

Default

array()
 

$encType

$encType : string

Default

''
 

Fileinfo magic database resource

$fileInfoDb : resource

Default

null
Static

This variable is populated the first time _detectFileMimeType is called and is then reused on every call to this method

 

$lastRawRequest

$lastRawRequest : \Zend\Http\Request

Default

null
 

$lastRawResponse

$lastRawResponse : \Zend\Http\Response

Default

null
 

$redirectCounter

$redirectCounter : integer

Default

0
 

$request

$request : \Zend\Http\Request

Default

 

$response

$response : \Zend\Http\Response

Default

 

$streamName

$streamName : string

Default

null

 Constants

 

AUTH_BASIC

AUTH_BASIC = 'basic' 
const string Supported HTTP Authentication methods
 

AUTH_DIGEST

AUTH_DIGEST = 'digest' 
 

DIGEST_CNONCE

DIGEST_CNONCE = 'cnonce' 
 

DIGEST_NC

DIGEST_NC = 'nc' 
 

DIGEST_NONCE

DIGEST_NONCE = 'nonce' 
 

DIGEST_OPAQUE

DIGEST_OPAQUE = 'opaque' 
 

DIGEST_QOP

DIGEST_QOP = 'qop' 
 

DIGEST_REALM

DIGEST_REALM = 'realm' 
const string DIGEST Authentication
 

ENC_FORMDATA

ENC_FORMDATA = 'multipart/form-data' 
 

ENC_URLENCODED

ENC_URLENCODED = 'application/x-www-form-urlencoded' 
const string POST data encoding methods