## Zend_Http_Client

Description

Zend_Http_Client is an implementation of an HTTP client in PHP. The client

supports basic features like sending different HTTP requests and handling redirections, as well as more advanced features like proxy settings, HTTP authentication and cookie persistence (using a Zend_Http_CookieJar object)

- copyright: Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
- todo: Implement proxy settings
- throws: Zend_Http_Client_Exception
- license: New BSD License

Located in /Http/Client.php (line 66)

Direct descendents

Class | Description
--- | ---
Zend_Gdata_HttpClient | Gdata Http Client object.

Class Constant Summary
AUTH_BASIC = 'basic'
CONNECT = 'CONNECT'
CONTENT_LENGTH = 'Content-Length'
CONTENT_TYPE = 'Content-Type'
DELETE = 'DELETE'
ENC_FORMDATA = 'multipart/form-data'
ENC_URLENCODED = 'application/x-www-form-urlencoded'
GET = 'GET'
HEAD = 'HEAD'
HTTP_0 = '1.0'
HTTP_1 = '1.1'
MERGE = 'MERGE'
OPTIONS = 'OPTIONS'
POST = 'POST'
PUT = 'PUT'
TRACE = 'TRACE'
Variable Summary
static resource $_fileInfoDb
Zend_Http_Client_Adapter_Interface $adapter
array|null $auth
array $config
Zend_Http_CookieJar $cookiejar
string $enctype
array $files
array $headers
string $last_request
Zend_Http_Response $last_response
string $method
array $paramsGet
array $paramsPost
string $raw_post_data
int $redirectCounter
Zend_Uri_Http $uri
Method Summary
static string encodeAuthHeader (string $user, string $password, [string $type = self::AUTH_BASIC])
static string encodeFormData (string $boundary, string $name, mixed $value, [string $filename = null], [array $headers = array()])
static array _flattenParametersArray (array $parray, [string $prefix = null])
Zend_Http_Client __construct ([Zend_Uri_Http|string $uri = null], [array $config = null])
Zend_Http_Client_Adapter_Interface getAdapter ()
Zend_Http_CookieJar|null getCookieJar ()
string|array|null getHeader (string $key)
string getLastRequest ()
Zend_Http_Response getLastResponse ()
int getRedirectionsCount ()
boolean|string getStream ()
Zend_Uri_Http|string getUri ([boolean $as_string = false])
Zend_Http_Response request ([string $method = null])
Zend_Http_Client resetParameters ([bool $clearAll = false])
null setAdapter (Zend_Http_Client_Adapter_Interface|string $adapter)
Zend_Http_Client setAuth (string|false $user, [string $password = ''], [string $type = self::AUTH_BASIC])
Zend_Http_Client setConfig ([Zend_Config $config = array()])
Zend_Http_Client setCookie (Zend_Http_Cookie|string $cookie, [string|null $value = null])
Zend_Http_Client setCookieJar ([Zend_Http_CookieJar|boolean $cookiejar = true])
Zend_Http_Client setEncType ([string $enctype = self::ENC_URLENCODED])
Zend_Http_Client setFileUpload (string $filename, string $formname, [string $data = null], [string $ctype = null])
Zend_Http_Client setHeaders (string|array $name, [mixed $value = null])
Zend_Http_Client setMethod ([string $method = self::GET])
Zend_Http_Client setParameterGet (string|array $name, [string $value = null])
Zend_Http_Client setParameterPost (string|array $name, [string $value = null])
Zend_Http_Client setRawData (string|resource $data, [string $enctype = null])
Zend_Http_Client setStream ([string|boolean $streamfile = true])
Zend_Http_Client setUri (Zend_Uri_Http|string $uri)
string _detectFileMimeType (string $file)
array _getParametersRecursive (array $parray, [bool $urlencode = false])
resource _openTempStream ()
string _prepareBody ()
array _prepareHeaders ()
null _setParameter (string $type, string $name, string $value)
Variables
static resource $_fileInfoDb = null (line 241)

Fileinfo magic database resource

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

- access: protected

Zend_Http_Client_Adapter_Interface $adapter = null (line 130)

The adapter used to perform the actual connection to the server

- access: protected

array|null $auth (line 193)

HTTP Authentication settings

Expected to be an associative array with this structure: $this->auth = array('user' => 'username', 'password' => 'password', 'type' => 'basic') Where 'type' should be one of the supported authentication types (see the AUTH_* constants), for example 'basic' or 'digest'.

If null, no authentication will be used.

- access: protected

array $config = 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
) (line 110)

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

- access: protected

Zend_Http_CookieJar $cookiejar = null (line 210)

The client's cookie jar

- access: protected

string $enctype = null (line 172)

Request body content type (for POST requests)

- access: protected

array $files = array() (line 203)

File upload arrays (used in POST requests)

An associative array, where each element is of the format: 'name' => array('filename.txt', 'text/plain', 'This is the actual file contents')

- access: protected

array $headers = array() (line 144)

Associative array of request headers

- access: protected

string $last_request = null (line 217)

The last HTTP request sent by the client, as string

- access: protected

Zend_Http_Response $last_response = null (line 224)

The last HTTP response received by the client

- access: protected

string $method = self::GET (line 151)

HTTP request method

- access: protected

array $paramsGet = array() (line 158)

Associative array of GET parameters

- access: protected

array $paramsPost = array() (line 165)

Associative array of POST parameters

- access: protected

string $raw_post_data = null (line 179)

The raw post data to send. Could be set by setRawData($data, $enctype).

- access: protected

int $redirectCounter = 0 (line 231)

Redirection counter

- access: protected

Zend_Uri_Http $uri = null (line 137)

Request URI

- access: protected

Methods
static encodeAuthHeader (line 1380)

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

- see: http://www.faqs.org/rfcs/rfc2617.html
- throws: Zend_Http_Client_Exception
- access: public

static string encodeAuthHeader (string $user, string $password, [string $type = self::AUTH_BASIC])

- string $user
- string $password
- string $type

static encodeFormData (line 1350)

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

- access: public

static string encodeFormData (string $boundary, string $name, mixed $value, [string $filename = null], [array $headers = array()])

- string $boundary
- string $name
- mixed $value
- string $filename
- array $headers: Associative array of optional headers @example ("Content-Transfer-Encoding" => "binary")

static _flattenParametersArray (line 1425)

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
- access: protected

static array _flattenParametersArray (array $parray, [string $prefix = null])

- array $parray
- string $prefix

Constructor __construct (line 250)

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

- access: public

Zend_Http_Client __construct ([Zend_Uri_Http|string $uri = null], [array $config = null])

- Zend_Uri_Http|string $uri
- array $config: Configuration key-value pairs.

getAdapter (line 867)

Load the connection adapter

- access: public

Zend_Http_Client_Adapter_Interface getAdapter ()

Redefined in descendants as:

- Zend_Gdata_HttpClient::getAdapter() : Return the current connection adapter

getCookieJar (line 611)

Return the current cookie jar or null if none.

- access: public

Zend_Http_CookieJar|null getCookieJar ()
getHeader (line 438)

Get the value of a specific header

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

- return: The header value or null if it is not set
- access: public

string|array|null getHeader (string $key)

- string $key

getLastRequest (line 808)

Get the last HTTP request as string

- access: public

string getLastRequest ()
getLastResponse (line 821)

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

- return: or null if none
- access: public

Zend_Http_Response getLastResponse ()
getRedirectionsCount (line 519)

Get the number of redirections done on the last request

- access: public

int getRedirectionsCount ()
getStream (line 888)

Get status of streaming for received data

- access: public

boolean|string getStream ()
getUri (line 300)

Get the URI for the next request

- access: public

Zend_Uri_Http|string getUri ([boolean $as_string = false])

- boolean $as_string: If true, will return the URI as a string

request (line 925)

Send the HTTP request and return an HTTP response object

- throws: Zend_Http_Client_Exception
- access: public

Zend_Http_Response request ([string $method = null])

- string $method

resetParameters (line 778)

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_*

- access: public

Zend_Http_Client resetParameters ([bool $clearAll = false])

- bool $clearAll: Should all data be cleared?

Redefined in descendants as:

- Zend_Gdata_HttpClient::resetParameters() : Clear all custom parameters we set.

setAdapter (line 836)

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

- throws: Zend_Http_Client_Exception
- access: public

null setAdapter (Zend_Http_Client_Adapter_Interface|string $adapter)

- Zend_Http_Client_Adapter_Interface|string $adapter

Redefined in descendants as:

- Zend_Gdata_HttpClient::setAdapter() : Load the connection adapter

setAuth (line 547)

Set HTTP authentication parameters

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

To enable authentication:

1. $this->setAuth('shahar', 'secret', Zend_Http_Client::AUTH_BASIC);

To disable authentication:

1. $this->setAuth(false);

- see: http://www.faqs.org/rfcs/rfc2617.html
- throws: Zend_Http_Client_Exception
- access: public

Zend_Http_Client setAuth (string|false $user, [string $password = ''], [string $type = self::AUTH_BASIC])

- string|false $user: User name or false disable authentication
- string $password: Password
- string $type: Authentication type

setConfig (line 316)

Set configuration parameters for this HTTP client

- throws: Zend_Http_Client_Exception
- access: public

Zend_Http_Client setConfig ([Zend_Config $config = array()])

- Zend_Config $config: | array $config

setCookie (line 625)

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.

- throws: Zend_Http_Client_Exception
- access: public

Zend_Http_Client setCookie (Zend_Http_Cookie|string $cookie, [string|null $value = null])

- Zend_Http_Cookie|string $cookie
- string|null $value: If "cookie" is a string, this is the cookie value.

setCookieJar (line 587)

Set the HTTP client's cookie jar.

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

- throws: Zend_Http_Client_Exception
- access: public

Zend_Http_Client setCookieJar ([Zend_Http_CookieJar|boolean $cookiejar = true])

- Zend_Http_CookieJar|boolean $cookiejar: Existing cookiejar object, true to create a new one, false to disable

setEncType (line 731)

Set the encoding type for POST data

- access: public

Zend_Http_Client setEncType ([string $enctype = self::ENC_URLENCODED])

- string $enctype

setFileUpload (line 698)

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.

- throws: Zend_Http_Client_Exception
- access: public

Zend_Http_Client setFileUpload (string $filename, string $formname, [string $data = null], [string $ctype = null])

- string $filename: Name of file to upload, or name to save as
- string $formname: Name of form element to send as
- string $data: Data to send (if null, $filename is read and sent)
- string $ctype: Content type to use (if $data is set and $ctype is null, will be application/octet-stream)

setHeaders (line 386)

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.

- throws: Zend_Http_Client_Exception
- access: public

Zend_Http_Client setHeaders (string|array $name, [mixed $value = null])

- string|array $name: Header name, full header string ('Header: value') or an array of headers
- mixed $value: Header value or null

setMethod (line 350)

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.

- throws: Zend_Http_Client_Exception
- access: public

Zend_Http_Client setMethod ([string $method = self::GET])

- string $method

setParameterGet (line 455)

Set a GET parameter for the request. Wrapper around _setParameter

- access: public

Zend_Http_Client setParameterGet (string|array $name, [string $value = null])

- string|array $name
- string $value

setParameterPost (line 474)

Set a POST parameter for the request. Wrapper around _setParameter

- access: public

Zend_Http_Client setParameterPost (string|array $name, [string $value = null])

- string|array $name
- string $value

setRawData (line 752)

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.

- access: public

Zend_Http_Client setRawData (string|resource $data, [string $enctype = null])

- string|resource $data
- string $enctype

setStream (line 878)

Set streaming for received data

- access: public

Zend_Http_Client setStream ([string|boolean $streamfile = true])

- string|boolean $streamfile: Stream file, true for temp file, false/null for no streaming

setUri (line 267)

Set the URI for the next request

- throws: Zend_Http_Client_Exception
- access: public

Zend_Http_Client setUri (Zend_Uri_Http|string $uri)

- Zend_Uri_Http|string $uri

_detectFileMimeType (line 1314)

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

- return: MIME type
- access: protected

string _detectFileMimeType (string $file)

- string $file: File path

_getParametersRecursive (line 1264)

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
- access: protected

array _getParametersRecursive (array $parray, [bool $urlencode = false])

- array $parray: The parameters array
- bool $urlencode: Whether to urlencode the name and value

_openTempStream (line 898)

Create temporary stream

- access: protected

resource _openTempStream ()
_prepareBody (line 1157)

Prepare the request body (for POST and PUT requests)

- throws: Zend_Http_Client_Exception
- access: protected

string _prepareBody ()

Redefined in descendants as:

- Zend_Gdata_HttpClient::_prepareBody() : Prepare the request body (for POST and PUT requests)

_prepareHeaders (line 1076)

Prepare the request headers

- access: protected

array _prepareHeaders ()
_setParameter (line 494)

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

- access: protected

null _setParameter (string $type, string $name, string $value)

- string $type: GET or POST
- string $name
- string $value

Class Constants
AUTH_BASIC = 'basic' (line 84)

Supported HTTP Authentication methods

CONNECT = 'CONNECT' (line 78)
CONTENT_LENGTH = 'Content-Length' (line 97)
CONTENT_TYPE = 'Content-Type' (line 96)

Content attributes

DELETE = 'DELETE' (line 75)
ENC_FORMDATA = 'multipart/form-data' (line 103)
ENC_URLENCODED = 'application/x-www-form-urlencoded' (line 102)

POST data encoding methods

GET = 'GET' (line 71)

HTTP request methods

HEAD = 'HEAD' (line 74)
HTTP_0 = '1.0' (line 91)
HTTP_1 = '1.1' (line 90)

HTTP protocol versions

MERGE = 'MERGE' (line 79)
OPTIONS = 'OPTIONS' (line 77)
POST = 'POST' (line 72)
PUT = 'PUT' (line 73)
TRACE = 'TRACE' (line 76)

Documentation generated on Tue, 26 Oct 2010 20:08:28 +0400 by phpDocumentor 1.4.2