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-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
 class Zend_Gdata_HttpClient Gdata Http Client object.
 class Zend_Oauth_Client
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
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])
 string|array|null getHeader (string $key)
 string getLastRequest ()
 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])
 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])
 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 240)

Fileinfo magic database resource

This varaiable 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 129)

The adapter used to preform the actual connection to the server

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

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,
)
(line 110)

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

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

The client's cookie jar

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

Request body content type (for POST requests)

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

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

Associative array of request headers

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

The last HTTP request sent by the client, as string

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

The last HTTP response received by the client

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

HTTP request method

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

Associative array of GET parameters

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

Assiciative array of POST parameters

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

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

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

Redirection counter

  • access: protected
Zend_Uri_Http $uri = null (line 136)

Request URI

  • access: protected
Methods
static encodeAuthHeader (line 1383)

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

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

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

Contructor 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.

Redefined in descendants as:
  • Zend_Oauth_Client::__construct() : Constructor; creates a new HTTP Client instance which itself is just a typical Zend_Http_Client subclass with some OAuth icing to assist in automating OAuth parameter generation, addition and cryptographioc signing of requests.
getAdapter (line 873)

Load the connection adapter

  • access: public

Redefined in descendants as:
getCookieJar (line 612)

Return the current cookie jar or null if none.

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

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

Get the last HTTP request as string

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

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

Get the number of redirections done on the last request

  • access: public
int getRedirectionsCount ()
getStream (line 894)

Get status of streaming for received data

  • access: public
boolean|string getStream ()
getUri (line 299)

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

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

Redefined in descendants as:
  • Zend_Oauth_Client::request() : Same as Zend_Http_Client::request() except just before the request is executed, we automatically append any necessary OAuth parameters and sign the request using the relevant signature method.
resetParameters (line 781)

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:
setAdapter (line 839)

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)

Redefined in descendants as:
setAuth (line 546)

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

Set configuration parameters for this HTTP client

  • throws: Zend_Http_Client_Exception
  • access: public
Zend_Http_Client setConfig ([Zend_Config $config = array()])
setCookie (line 626)

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

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

Set the encoding type for POST data

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

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

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.

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

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

Redefined in descendants as:
setParameterGet (line 454)

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

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

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

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

Set the URI for the next request

  • throws: Zend_Http_Client_Exception
  • access: public
Zend_Http_Client setUri (Zend_Uri_Http|string $uri)
_detectFileMimeType (line 1317)

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

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

Create temporary stream

  • access: protected
resource _openTempStream ()
_prepareBody (line 1160)

Prepare the request body (for POST and PUT requests)

  • throws: Zend_Http_Client_Exception
  • access: protected
string _prepareBody ()

Redefined in descendants as:
_prepareHeaders (line 1079)

Prepare the request headers

  • access: protected
array _prepareHeaders ()
_setParameter (line 493)

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 Mon, 21 Jun 2010 15:11:50 -0400 by phpDocumentor 1.4.3