Zend Framework  1.12
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
Zend_Http_Client Class Reference

Public Member Functions

 __construct ($uri=null, $config=null)
 Constructor method.
 
 setUri ($uri)
 Set the URI for the next request.
 
 getUri ($as_string=false)
 Get the URI for the next request.
 
 setConfig ($config=array())
 Set configuration parameters for this HTTP client.
 
 setMethod ($method=self::GET)
 Set the next request's method.
 
 setHeaders ($name, $value=null)
 Set one or more request headers.
 
 getHeader ($key)
 Get the value of a specific header.
 
 setParameterGet ($name, $value=null)
 Set a GET parameter for the request.
 
 setParameterPost ($name, $value=null)
 Set a POST parameter for the request.
 
 getRedirectionsCount ()
 Get the number of redirections done on the last request.
 
 setAuth ($user, $password= '', $type=self::AUTH_BASIC)
 Set HTTP authentication parameters.
 
 setCookieJar ($cookiejar=true)
 Set the HTTP client's cookie jar.
 
 getCookieJar ()
 Return the current cookie jar or null if none.
 
 setCookie ($cookie, $value=null)
 Add a cookie to the request.
 
 setFileUpload ($filename, $formname, $data=null, $ctype=null)
 Set a file to upload (using a POST request)
 
 setEncType ($enctype=self::ENC_URLENCODED)
 Set the encoding type for POST data.
 
 setRawData ($data, $enctype=null)
 Set the raw (already encoded) POST data.
 
 setUnmaskStatus ($status=true)
 Set the unmask feature for GET parameters as array.
 
 getUnmaskStatus ()
 Returns the currently configured unmask status.
 
 resetParameters ($clearAll=false)
 Clear all GET and POST parameters.
 
 getLastRequest ()
 Get the last HTTP request as string.
 
 getLastResponse ()
 Get the last HTTP response received by this client.
 
 setAdapter ($adapter)
 Load the connection adapter.
 
 getAdapter ()
 Load the connection adapter.
 
 setStream ($streamfile=true)
 Set streaming for received data.
 
 getStream ()
 Get status of streaming for received data.
 
 request ($method=null)
 Send the HTTP request and return an HTTP response object.
 

Static Public Member Functions

static encodeFormData ($boundary, $name, $value, $filename=null, $headers=array())
 
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 GET = 'GET'
 HTTP request methods.
 
const POST = 'POST'
 
const PUT = 'PUT'
 
const HEAD = 'HEAD'
 
const DELETE = 'DELETE'
 
const TRACE = 'TRACE'
 
const OPTIONS = 'OPTIONS'
 
const CONNECT = 'CONNECT'
 
const MERGE = 'MERGE'
 
const PATCH = 'PATCH'
 
const AUTH_BASIC = 'basic'
 Supported HTTP Authentication methods.
 
const HTTP_1 = '1.1'
 HTTP protocol versions.
 
const HTTP_0 = '1.0'
 
const CONTENT_TYPE = 'Content-Type'
 Content attributes.
 
const CONTENT_LENGTH = 'Content-Length'
 
const ENC_URLENCODED = 'application/x-www-form-urlencoded'
 POST data encoding methods.
 
const ENC_FORMDATA = 'multipart/form-data'
 
const VTYPE_SCALAR = 'SCALAR'
 Value types for Body key/value pairs.
 
const VTYPE_FILE = 'FILE'
 

Protected Member Functions

 _setParameter ($type, $name, $value)
 Set a GET or POST parameter - used by SetParameterGet and SetParameterPost.
 
 _openTempStream ()
 Create temporary stream.
 
 _prepareHeaders ()
 Prepare the request headers.
 
 _prepareBody ()
 Prepare the request body (for POST and PUT requests)
 
 _getParametersRecursive ($parray, $urlencode=false)
 Helper method that gets a possibly multi-level parameters array (get or post) and flattens it.
 
 _detectFileMimeType ($file)
 Attempt to detect the MIME type of a file using available extensions.
 

Static Protected Member Functions

static _flattenParametersArray ($parray, $prefix=null)
 Convert an array of parameters into a flat array of (key, value) pairs.
 

Protected Attributes

 $config
 
 $adapter = null
 
 $uri = null
 
 $headers = array()
 
 $method = self::GET
 
 $paramsGet = array()
 
 $paramsPost = array()
 
 $enctype = null
 
 $raw_post_data = null
 
 $auth
 
 $files = array()
 
 $body_field_order = array()
 
 $cookiejar = null
 
 $last_request = null
 
 $last_response = null
 
 $redirectCounter = 0
 
 $_unmaskStatus = false
 
 $_queryBracketsEscaped = true
 

Static Protected Attributes

static $_fileInfoDb = null
 

Constructor & Destructor Documentation

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

Constructor method.

Will create a new HTTP client. Accepts the target URL and optionally configuration array.

Parameters
Zend_Uri_Http | string$uri
array$configConfiguration key-value pairs.

Member Function Documentation

_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 deprected 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
static _flattenParametersArray (   $parray,
  $prefix = null 
)
staticprotected

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
_getParametersRecursive (   $parray,
  $urlencode = false 
)
protected

Helper method that gets a possibly multi-level parameters array (get or post) and flattens it.

The method returns an array of (key, value) pairs (because keys are not necessarily unique. If one of the parameters in as array, it will also add a [] suffix to the key.

This method is deprecated since Zend Framework 1.9 in favour of self::_flattenParametersArray() and will be dropped in 2.0

Deprecated:
since 1.9
Parameters
array$parrayThe parameters array
bool$urlencodeWhether to urlencode the name and value
Returns
array
_openTempStream ( )
protected

Create temporary stream.

Returns
resource
_prepareBody ( )
protected

Prepare the request body (for POST and PUT requests)

Returns
string
Exceptions
Zend_Http_Client_Exception
See Also
Zend_Http_Client_Exception
_prepareHeaders ( )
protected

Prepare the request headers.

Returns
array
_setParameter (   $type,
  $name,
  $value 
)
protected

Set a GET or POST parameter - used by SetParameterGet and SetParameterPost.

Parameters
string$typeGET or POST
string$name
string$value
Returns
null
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
Zend_Http_Client_Exception
See Also
Zend_Http_Client_Exception
Todo:
Implement digest authentication
See Also
Zend_Http_Client_Exception
static encodeFormData (   $boundary,
  $name,
  $value,
  $filename = null,
  $headers = array() 
)
static
getAdapter ( )

Load the connection adapter.

Returns
Zend_Http_Client_Adapter_Interface $adapter
getCookieJar ( )

Return the current cookie jar or null if none.

Returns
Zend_Http_CookieJar|null
getHeader (   $key)

Get the value of a specific header.

Note that if the header has more than one value, an array will be returned.

Parameters
string$key
Returns
string|array|null The header value or null if it is not set
getLastRequest ( )

Get the last HTTP request as string.

Returns
string
getLastResponse ( )

Get the last HTTP response received by this client.

If $config['storeresponse'] is set to false, or no response was stored yet, will return null

Returns
Zend_Http_Response or null if none
getRedirectionsCount ( )

Get the number of redirections done on the last request.

Returns
int
getStream ( )

Get status of streaming for received data.

Returns
boolean|string
getUnmaskStatus ( )

Returns the currently configured unmask status.

Returns
boolean
getUri (   $as_string = false)

Get the URI for the next request.

Parameters
boolean$as_stringIf true, will return the URI as a string
Returns
Zend_Uri_Http|string
request (   $method = null)

Send the HTTP request and return an HTTP response object.

Parameters
string$method
Returns
Zend_Http_Response
Exceptions
Zend_Http_Client_Exception
See Also
Zend_Http_Client_Exception
Zend_Http_Client_Exception
Zend_Http_Client_Exception
Zend_Http_Client_Exception
resetParameters (   $clearAll = false)

Clear all GET and POST parameters.

Should be used to reset the request parameters if the client is used for several concurrent requests.

clearAll parameter controls if we clean just parameters or also headers and last_*

Parameters
bool$clearAllShould all data be cleared?
Returns
Zend_Http_Client
setAdapter (   $adapter)

Load the connection adapter.

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

Parameters
Zend_Http_Client_Adapter_Interface | string$adapter
Returns
null
Exceptions
Zend_Http_Client_Exception
See Also
Zend_Http_Client_Exception
Zend_Http_Client_Exception
setAuth (   $user,
  $password = '',
  $type = self::AUTH_BASIC 
)

Set HTTP authentication parameters.

$type should be one of the supported types - see the self::AUTH_* constants.

To enable authentication: $this->setAuth('shahar', 'secret', Zend_Http_Client::AUTH_BASIC);

To disable authentication: $this->setAuth(false);

See Also
http://www.faqs.org/rfcs/rfc2617.html
Parameters
string | false$userUser name or false disable authentication
string$passwordPassword
string$typeAuthentication type
Returns
Zend_Http_Client
Exceptions
Zend_Http_Client_Exception
See Also
Zend_Http_Client_Exception
setConfig (   $config = array())

Set configuration parameters for this HTTP client.

Parameters
Zend_Config  |  array$config
Returns
Zend_Http_Client
Exceptions
Zend_Http_Client_Exception
See Also
Zend_Http_Client_Exception
setCookie (   $cookie,
  $value = null 
)

Add a cookie to the request.

If the client has no Cookie Jar, the cookies will be added directly to the headers array as "Cookie" headers.

Parameters
Zend_Http_Cookie | string$cookie
string | null$valueIf "cookie" is a string, this is the cookie value.
Returns
Zend_Http_Client
Exceptions
Zend_Http_Client_Exception
See Also
Zend_Http_Client_Exception
setCookieJar (   $cookiejar = true)

Set the HTTP client's cookie jar.

A cookie jar is an object that holds and maintains cookies across HTTP requests and responses.

Parameters
Zend_Http_CookieJar | boolean$cookiejarExisting cookiejar object, true to create a new one, false to disable
Returns
Zend_Http_Client
Exceptions
Zend_Http_Client_Exception
See Also
Zend_Http_Client_Exception
setEncType (   $enctype = self::ENC_URLENCODED)

Set the encoding type for POST data.

Parameters
string$enctype
Returns
Zend_Http_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
Zend_Http_Client
Exceptions
Zend_Http_Client_Exception
See Also
Zend_Http_Client_Exception
setHeaders (   $name,
  $value = null 
)

Set one or more request headers.

This function can be used in several ways to set the client's request headers: 1. By providing two parameters: $name as the header to set (e.g. 'Host') and $value as it's value (e.g. 'www.example.com'). 2. By providing a single header string as the only parameter e.g. 'Host: www.example.com' 3. By providing an array of headers as the first parameter e.g. array('host' => 'www.example.com', 'x-foo: bar'). In This case the function will call itself recursively for each array item.

Parameters
string | array$nameHeader name, full header string ('Header: value') or an array of headers
mixed$valueHeader value or null
Returns
Zend_Http_Client
Exceptions
Zend_Http_Client_Exception
See Also
Zend_Http_Client_Exception
setMethod (   $method = self::GET)

Set the next request's method.

Validated the passed method and sets it. If we have files set for POST requests, and the new method is not POST, the files are silently dropped.

Parameters
string$method
Returns
Zend_Http_Client
Exceptions
Zend_Http_Client_Exception
See Also
Zend_Http_Client_Exception
setParameterGet (   $name,
  $value = null 
)

Set a GET parameter for the request.

Wrapper around _setParameter

Parameters
string | array$name
string$value
Returns
Zend_Http_Client
setParameterPost (   $name,
  $value = null 
)

Set a POST parameter for the request.

Wrapper around _setParameter

Parameters
string | array$name
string$value
Returns
Zend_Http_Client
setRawData (   $data,
  $enctype = null 
)

Set the raw (already encoded) POST data.

This function is here for two reasons: 1. For advanced user who would like to set their own data, already encoded 2. For backwards compatibilty: If someone uses the old post($data) method. this method will be used to set the encoded data.

$data can also be stream (such as file) from which the data will be read.

Parameters
string | resource$data
string$enctype
Returns
Zend_Http_Client
setStream (   $streamfile = true)

Set streaming for received data.

Parameters
string | boolean$streamfileStream file, true for temp file, false/null for no streaming
Returns
Zend_Http_Client
setUnmaskStatus (   $status = true)

Set the unmask feature for GET parameters as array.

Example: foo%5B0%5D=a&foo%5B1%5D=b becomes foo=a&foo=b

This is usefull for some services

Parameters
boolean$status
Returns
Zend_Http_Client
setUri (   $uri)

Set the URI for the next request.

Parameters
Zend_Uri_Http | string$uri
Returns
Zend_Http_Client
Exceptions
Zend_Http_Client_Exception
See Also
Zend_Http_Client_Exception

Member Data Documentation

$_fileInfoDb = null
staticprotected
$_queryBracketsEscaped = true
protected
$_unmaskStatus = false
protected
$adapter = null
protected
$auth
protected
$body_field_order = array()
protected
$config
protected
Initial value:
= array(
'maxredirects' => 5,
'strictredirects' => false,
'useragent' => 'Zend_Http_Client',
'timeout' => 10,
'adapter' => 'Zend_Http_Client_Adapter_Socket',
'httpversion' => self::HTTP_1,
'keepalive' => false,
'storeresponse' => true,
'strict' => true,
'output_stream' => false,
'encodecookies' => true,
'rfc3986_strict' => false
)
$cookiejar = null
protected
$enctype = null
protected
$files = array()
protected
$headers = array()
protected
$last_request = null
protected
$last_response = null
protected
$method = self::GET
protected
$paramsGet = array()
protected
$paramsPost = array()
protected
$raw_post_data = null
protected
$redirectCounter = 0
protected
$uri = null
protected
const AUTH_BASIC = 'basic'

Supported HTTP Authentication methods.

const CONNECT = 'CONNECT'
const CONTENT_LENGTH = 'Content-Length'
const CONTENT_TYPE = 'Content-Type'

Content attributes.

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

POST data encoding methods.

const GET = 'GET'

HTTP request methods.

const HEAD = 'HEAD'
const HTTP_0 = '1.0'
const HTTP_1 = '1.1'

HTTP protocol versions.

const MERGE = 'MERGE'
const OPTIONS = 'OPTIONS'
const PATCH = 'PATCH'
const POST = 'POST'
const PUT = 'PUT'
const TRACE = 'TRACE'
const VTYPE_FILE = 'FILE'
const VTYPE_SCALAR = 'SCALAR'

Value types for Body key/value pairs.