Class Zend_Http_Client

Description

Zend_Http_Client is an implemetation 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 persistance (using a Zend_Http_CookieJar object)

  • copyright: Copyright (c) 2005-2007 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 44)


	
			
Class Constant Summary
 AUTH_BASIC = 'basic'
 CONNECT = 'CONNECT'
 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'
 OPTIONS = 'OPTIONS'
 POST = 'POST'
 PUT = 'PUT'
 TRACE = 'TRACE'
Variable Summary
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()])
 Zend_Http_Client __construct ([Zend_Uri_Http|string $uri = null], [ $config = null], array $headers)
 string|array|null getHeader (unknown_type $key)
 string getLastRequest ()
 Zend_Uri_Http|string getUri ([boolean $as_string = false])
 string prepare_body ()
 array prepare_headers ()
 Zend_Http_Response request ([string $method = null])
 Zend_Http_Client setAuth (string|false $user, [string $password = ''], [string $type = self::AUTH_BASIC])
 Zend_Http_Client setConfig ([array $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 $data, [string $enctype = null])
 array _getParametersRecursive (array $parray, [bool $urlencode = false])
 void _setParameter (string $type, string $name, string $value)
Variables
Zend_Http_Client_Adapter_Interface $adapter = null (line 97)

The adapter used to preform the actual connection to the server

  • access: protected
array|null $auth (line 160)

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
unknown_type $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
)
(line 81)

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

  • access: protected
Zend_Http_CookieJar $cookiejar = null (line 177)

The client's cookie jar

  • access: protected
string $enctype = null (line 139)

Request body content type (for POST requests)

  • access: protected
array $files = array() (line 170)

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 111)

Associative array of request headers

  • access: protected
string $last_request = null (line 184)

The last HTTP request sent by the client, as string

  • access: protected
Zend_Http_Response $last_response = null (line 191)

The last HTTP response received by the client

  • access: protected
string $method = self::GET (line 118)

HTTP request method

  • access: protected
array $paramsGet = array() (line 125)

Associative array of GET parameters

  • access: protected
array $paramsPost = array() (line 132)

Assiciative array of POST parameters

  • access: protected
string $raw_post_data = null (line 146)

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

  • access: protected
int $redirectCounter = 0 (line 198)

Redirection counter

  • access: protected
Zend_Uri_Http $uri (line 104)

Request URI

  • access: protected
Methods
static encodeAuthHeader (line 1022)

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

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

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")
Constructor __construct (line 207)

Contructor method. Will create a new HTTP client. Accepts the target URL and optionally and array of headers.

  • access: public
Zend_Http_Client __construct ([Zend_Uri_Http|string $uri = null], [ $config = null], array $headers)
  • Zend_Uri_Http|string $uri
  • array $headers: Optional request headers to set
  • $config
getCookieJar (line 519)

Return the current cookie jar or null if none.

  • access: public
Zend_Http_CookieJar|null getCookieJar ()
getHeader (line 358)

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 (unknown_type $key)
  • unknown_type $key
getLastRequest (line 675)

Get the last HTTP request as string

  • access: public
string getLastRequest ()
getLastResponse (line 688)

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 438)

Get the number of redirections done on the last request

  • access: public
int getRedirectionsCount ()
getUri (line 245)

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
prepare_body (line 891)

Prepare the request body (for POST and PUT requests)

  • access: protected
string prepare_body ()
prepare_headers (line 829)

Prepare the request headers

  • access: protected
array prepare_headers ()
request (line 728)

Send the HTTP request and return an HTTP response object

  • access: public
Zend_Http_Response request ([string $method = null])
  • string $method
resetParameters (line 655)

Clear all GET and POST parameters

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

  • access: public
Zend_Http_Client resetParameters ()
setAdapter (line 701)

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

  • access: public
void setAdapter (Zend_Http_Client_Adapter_Interface|string $adapter)
setAuth (line 465)

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);

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 260)

Set configuration parameters for this HTTP client

  • access: public
Zend_Http_Client setConfig ([array $config = array()])
  • array $config
setCookie (line 532)

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.

  • 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 496)

Set the HTTP client's cookie jar.

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

  • 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 620)

Set the encoding type for POST data

  • access: public
Zend_Http_Client setEncType ([string $enctype = self::ENC_URLENCODED])
  • string $enctype
setFileUpload (line 596)

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.

  • 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 312)

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 (eg. 'Host') and $value as it's value (eg. 'www.example.com').
  2. By providing a single header string as the only parameter eg. 'Host: www.example.com'
  3. By providing an array of headers as the first parameter eg. array('host' => 'www.example.com', 'x-foo: bar'). In This case the function will call itself recursively for each array item.

  • 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 281)

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.

  • access: public
Zend_Http_Client setMethod ([string $method = self::GET])
  • string $method
setParameterGet (line 375)

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 394)

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 639)

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.

  • access: public
Zend_Http_Client setRawData (string $data, [string $enctype = null])
  • string $data
  • string $enctype
setUri (line 219)

Set the URI for the next request

  • access: public
Zend_Http_Client setUri (Zend_Uri_Http|string $uri)
_getParametersRecursive (line 961)

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.

  • access: protected
array _getParametersRecursive (array $parray, [bool $urlencode = false])
  • array $parray: The parameters array
  • bool $urlencode: Whether to urlencode the name and value
_setParameter (line 413)

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

  • access: protected
void _setParameter (string $type, string $name, string $value)
  • string $type: GET or POST
  • string $name
  • string $value
Class Constants
AUTH_BASIC = 'basic' (line 61)

Supported HTTP Authentication methods

CONNECT = 'CONNECT' (line 56)
DELETE = 'DELETE' (line 53)
ENC_FORMDATA = 'multipart/form-data' (line 74)
ENC_URLENCODED = 'application/x-www-form-urlencoded' (line 73)

POST data encoding methods

GET = 'GET' (line 49)

HTTP request methods

HEAD = 'HEAD' (line 52)
HTTP_0 = '1.0' (line 68)
HTTP_1 = '1.1' (line 67)

HTTP protocol versions

OPTIONS = 'OPTIONS' (line 55)
POST = 'POST' (line 50)
PUT = 'PUT' (line 51)
TRACE = 'TRACE' (line 54)

Documentation generated on Sun, 27 May 2007 23:21:39 -0700 by phpDocumentor 1.3.2