Zend Framework  3.0
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
Client Class Reference

Http client. More...

Public Member Functions

 __construct ($uri=null, $options=null)
 Constructor.
 
 setOptions ($options=[])
 Set configuration parameters for this HTTP client.
 
 setAdapter ($adapter)
 Load the connection adapter.
 
 getAdapter ()
 Load the connection adapter.
 
 setRequest (Request $request)
 Set request.
 
 getRequest ()
 Get Request.
 
 setResponse (Response $response)
 Set response.
 
 getResponse ()
 Get Response.
 
 getLastRawRequest ()
 Get the last request (as a string)
 
 getLastRawResponse ()
 Get the last response (as a string)
 
 getRedirectionsCount ()
 Get the redirections count.
 
 setUri ($uri)
 Set Uri (to the request)
 
 getUri ()
 Get uri (from the request)
 
 setMethod ($method)
 Set the HTTP method (to the request)
 
 getMethod ()
 Get the HTTP method.
 
 setArgSeparator ($argSeparator)
 Set the query string argument separator.
 
 getArgSeparator ()
 Get the query string argument separator.
 
 setEncType ($encType, $boundary=null)
 Set the encoding type and the boundary (if any)
 
 getEncType ()
 Get the encoding type.
 
 setRawBody ($body)
 Set raw body (for advanced use cases)
 
 setParameterPost (array $post)
 Set the POST parameters.
 
 setParameterGet (array $query)
 Set the GET parameters.
 
 resetParameters ($clearCookies=false)
 Reset all the HTTP parameters (request, response, etc)
 
 getCookies ()
 Return the current cookies.
 
 addCookie ($cookie, $value=null, $expire=null, $path=null, $domain=null, $secure=false, $httponly=true, $maxAge=null, $version=null)
 Add a cookie.
 
 setCookies ($cookies)
 Set an array of cookies.
 
 clearCookies ()
 Clear all the cookies.
 
 setHeaders ($headers)
 Set the headers (for the request)
 
 hasHeader ($name)
 Check if exists the header type specified.
 
 getHeader ($name)
 Get the header value of the request.
 
 setStream ($streamfile=true)
 Set streaming for received data.
 
 getStream ()
 Get status of streaming for received data.
 
 setAuth ($user, $password, $type=self::AUTH_BASIC)
 Create a HTTP authentication "Authorization:" header according to the specified user, password and authentication method.
 
 clearAuth ()
 Clear http authentication.
 
 dispatch (Stdlib\RequestInterface $request, Stdlib\ResponseInterface $response=null)
 Dispatch.
 
 send (Request $request=null)
 Send HTTP request.
 
 reset ()
 Fully reset the HTTP client (auth, cookies, request, response, etc.)
 
 setFileUpload ($filename, $formname, $data=null, $ctype=null)
 Set a file to upload (using a POST request)
 
 removeFileUpload ($filename)
 Remove a file to upload.
 
 encodeFormData ($boundary, $name, $value, $filename=null, $headers=[])
 
- Public Member Functions inherited from DispatchableInterface
 dispatch (RequestInterface $request, ResponseInterface $response=null)
 Dispatch a request.
 

Static Public Member Functions

static encodeAuthHeader ($user, $password, $type=self::AUTH_BASIC)
 Create a HTTP authentication "Authorization:" header according to the specified user, password and authentication method.
 

Public Attributes

const AUTH_BASIC = 'basic'
 string Supported HTTP Authentication methods
 
const AUTH_DIGEST = 'digest'
 
const ENC_URLENCODED = 'application/x-www-form-urlencoded'
 string POST data encoding methods
 
const ENC_FORMDATA = 'multipart/form-data'
 
const DIGEST_REALM = 'realm'
 string DIGEST Authentication
 
const DIGEST_QOP = 'qop'
 
const DIGEST_NONCE = 'nonce'
 
const DIGEST_OPAQUE = 'opaque'
 
const DIGEST_NC = 'nc'
 
const DIGEST_CNONCE = 'cnonce'
 

Protected Member Functions

 getCookieId ($cookie)
 Get the cookie Id (name+domain+path)
 
 openTempStream ()
 Create temporary stream.
 
 calcAuthDigest ($user, $password, $type=self::AUTH_BASIC, $digest=[], $entityBody=null)
 Calculate the response value according to the HTTP authentication type.
 
 prepareCookies ($domain, $path, $secure)
 Prepare Cookies.
 
 prepareHeaders ($body, $uri)
 Prepare the request headers.
 
 prepareBody ()
 Prepare the request body (for PATCH, POST and PUT requests)
 
 detectFileMimeType ($file)
 Attempt to detect the MIME type of a file using available extensions.
 
 flattenParametersArray ($parray, $prefix=null)
 Convert an array of parameters into a flat array of (key, value) pairs.
 
 doRequest (Http $uri, $method, $secure=false, $headers=[], $body= '')
 Separating this from send method allows subclasses to wrap the interaction with the adapter.
 

Protected Attributes

 $response
 
 $request
 
 $adapter
 
 $auth = []
 
 $streamName = null
 
 $cookies = []
 
 $encType = ''
 
 $lastRawRequest = null
 
 $lastRawResponse = null
 
 $redirectCounter = 0
 
 $config
 

Static Protected Attributes

static $fileInfoDb = null
 

Detailed Description

Http client.

Constructor & Destructor Documentation

__construct (   $uri = null,
  $options = null 
)

Constructor.

Parameters
string$uri
array | Traversable$options

Member Function Documentation

addCookie (   $cookie,
  $value = null,
  $expire = null,
  $path = null,
  $domain = null,
  $secure = false,
  $httponly = true,
  $maxAge = null,
  $version = null 
)

Add a cookie.

Parameters
array | ArrayIterator | Header\SetCookie | string$cookie
string$value
string$expire
string$path
string$domain
bool$secure
bool$httponly
string$maxAge
string$version
Exceptions
Exception\InvalidArgumentException
Returns
Client
calcAuthDigest (   $user,
  $password,
  $type = self::AUTH_BASIC,
  $digest = [],
  $entityBody = null 
)
protected

Calculate the response value according to the HTTP authentication type.

See Also
http://www.faqs.org/rfcs/rfc2617.html
Parameters
string$user
string$password
string$type
array$digest
null | string$entityBody
Exceptions
Exception\InvalidArgumentException
Returns
string|bool
clearAuth ( )

Clear http authentication.

clearCookies ( )

Clear all the cookies.

detectFileMimeType (   $file)
protected

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

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
string$fileFile path
Returns
string MIME type
dispatch ( Stdlib\RequestInterface  $request,
Stdlib\ResponseInterface  $response = null 
)

Dispatch.

Parameters
Stdlib\RequestInterface$request
Stdlib\ResponseInterface$response
Returns
Stdlib
doRequest ( Http  $uri,
  $method,
  $secure = false,
  $headers = [],
  $body = '' 
)
protected

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

Parameters
Http$uri
string$method
bool$secure
array$headers
string$body
Returns
string the raw response
Exceptions
Exception\RuntimeException
static encodeAuthHeader (   $user,
  $password,
  $type = self::AUTH_BASIC 
)
static

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

See Also
http://www.faqs.org/rfcs/rfc2617.html
Parameters
string$user
string$password
string$type
Returns
string
Exceptions
Client\Exception\InvalidArgumentException
Todo:
Implement digest authentication
encodeFormData (   $boundary,
  $name,
  $value,
  $filename = null,
  $headers = [] 
)
flattenParametersArray (   $parray,
  $prefix = null 
)
protected

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

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
array$parray
string$prefix
Returns
array
getAdapter ( )

Load the connection adapter.

Returns
Client $adapter
getArgSeparator ( )

Get the query string argument separator.

Returns
string
getCookieId (   $cookie)
protected

Get the cookie Id (name+domain+path)

Parameters
Header\SetCookie | Header\Cookie$cookie
Returns
string|bool
getCookies ( )

Return the current cookies.

Returns
array
getEncType ( )

Get the encoding type.

Returns
string
getHeader (   $name)

Get the header value of the request.

Parameters
string$name
Returns
string|bool
getLastRawRequest ( )

Get the last request (as a string)

Returns
string
getLastRawResponse ( )

Get the last response (as a string)

Returns
string
getMethod ( )

Get the HTTP method.

Returns
string
getRedirectionsCount ( )

Get the redirections count.

Returns
int
getRequest ( )

Get Request.

Returns
Request
getResponse ( )

Get Response.

Returns
Response
getStream ( )

Get status of streaming for received data.

Returns
bool|string
getUri ( )

Get uri (from the request)

Returns
Http
hasHeader (   $name)

Check if exists the header type specified.

Parameters
string$name
Returns
bool
openTempStream ( )
protected

Create temporary stream.

Exceptions
Exception\RuntimeException
Returns
resource
prepareBody ( )
protected

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

Returns
string
Exceptions
\Zend\Http\Client\Exception\RuntimeException
prepareCookies (   $domain,
  $path,
  $secure 
)
protected

Prepare Cookies.

Parameters
string$domain
string$path
bool$secure
Returns
Header|bool
prepareHeaders (   $body,
  $uri 
)
protected

Prepare the request headers.

Parameters
resource | string$body
Http$uri
Exceptions
Exception\RuntimeException
Returns
array
removeFileUpload (   $filename)

Remove a file to upload.

Parameters
string$filename
Returns
bool
reset ( )

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

Returns
Client
resetParameters (   $clearCookies = false)

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

Parameters
bool$clearCookiesAlso clear all valid cookies? (defaults to false)
bool$clearAuthAlso clear http authentication? (defaults to true)
Returns
Client
send ( Request  $request = null)

Send HTTP request.

Parameters
Request$request
Returns
Response
Exceptions
Exception\RuntimeException
Client\Exception\RuntimeException
setAdapter (   $adapter)

Load the connection adapter.

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

Parameters
Client\Adapter\AdapterInterface | string$adapter
Returns
Client
Exceptions
Client\Exception\InvalidArgumentException
setArgSeparator (   $argSeparator)

Set the query string argument separator.

Parameters
string$argSeparator
Returns
Client
setAuth (   $user,
  $password,
  $type = self::AUTH_BASIC 
)

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

Parameters
string$user
string$password
string$type
Exceptions
Exception\InvalidArgumentException
Returns
Client
setCookies (   $cookies)

Set an array of cookies.

Parameters
array$cookies
Exceptions
Exception\InvalidArgumentException
Returns
Client
setEncType (   $encType,
  $boundary = null 
)

Set the encoding type and the boundary (if any)

Parameters
string$encType
string$boundary
Returns
Client
setFileUpload (   $filename,
  $formname,
  $data = null,
  $ctype = null 
)

Set a file to upload (using a POST request)

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
string$filenameName of file to upload, or name to save as
string$formnameName of form element to send as
string$dataData to send (if null, $filename is read and sent)
string$ctypeContent type to use (if $data is set and $ctype is null, will be application/octet-stream)
Returns
Client
Exceptions
Exception\RuntimeException
setHeaders (   $headers)

Set the headers (for the request)

Parameters
Headers | array$headers
Exceptions
Exception\InvalidArgumentException
Returns
Client
setMethod (   $method)

Set the HTTP method (to the request)

Parameters
string$method
Returns
Client
setOptions (   $options = [])

Set configuration parameters for this HTTP client.

Parameters
array | Traversable$options
Returns
Client
Exceptions
Client\Exception\InvalidArgumentException

Config Key Normalization

setParameterGet ( array  $query)

Set the GET parameters.

Parameters
array$query
Returns
Client
setParameterPost ( array  $post)

Set the POST parameters.

Parameters
array$post
Returns
Client
setRawBody (   $body)

Set raw body (for advanced use cases)

Parameters
string$body
Returns
Client
setRequest ( Request  $request)

Set request.

Parameters
Request$request
Returns
Client
setResponse ( Response  $response)

Set response.

Parameters
Response$response
Returns
Client
setStream (   $streamfile = true)

Set streaming for received data.

Parameters
string | bool$streamfileStream file, true for temp file, false/null for no streaming
Returns
setUri (   $uri)

Set Uri (to the request)

Parameters
string | Http$uri
Returns
Client

Member Data Documentation

$adapter
protected
$auth = []
protected
$config
protected
Initial value:
= [
'maxredirects' => 5
$cookies = []
protected
$encType = ''
protected
$fileInfoDb = null
staticprotected
$lastRawRequest = null
protected
$lastRawResponse = null
protected
$redirectCounter = 0
protected
$request
protected
$response
protected
$streamName = null
protected
const AUTH_BASIC = 'basic'

string Supported HTTP Authentication methods

const AUTH_DIGEST = 'digest'
const DIGEST_CNONCE = 'cnonce'
const DIGEST_NC = 'nc'
const DIGEST_NONCE = 'nonce'
const DIGEST_OPAQUE = 'opaque'
const DIGEST_QOP = 'qop'
const DIGEST_REALM = 'realm'

string DIGEST Authentication

const ENC_FORMDATA = 'multipart/form-data'
const ENC_URLENCODED = 'application/x-www-form-urlencoded'

string POST data encoding methods