Http/Client.php

Show: PublicProtectedPrivateinherited
Table of Contents
Zend Framework

LICENSE

This source file is subject to the new BSD license that is bundled with this package in the file LICENSE.txt. It is also available through the world-wide-web at this URL: http://framework.zend.com/license/new-bsd If you did not receive a copy of the license and are unable to obtain it through the world-wide-web, please send an email to license@zend.com so we can send you a copy immediately.

Category
Zend  
Copyright
Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)  
License
New BSD License  
Package
Zend_Http  
Subpackage
Client  
Version
$Id: Client.php 24593 2012-01-05 20:35:02Z matthew $  

\Zend_Http_Client

Package: Zend\Http\Client
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)

Children
\Zend_Gdata_HttpClient
Category
Zend  
Copyright
Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)  
License
New BSD License  
\Zend_Http_Client_Exception
Todo
Implement proxy settings  

Constants

>VConstant  GET = 'GET'
HTTP request methods
>VConstant  POST = 'POST'
>VConstant  PUT = 'PUT'
>VConstant  HEAD = 'HEAD'
>VConstant  DELETE = 'DELETE'
>VConstant  TRACE = 'TRACE'
>VConstant  OPTIONS = 'OPTIONS'
>VConstant  CONNECT = 'CONNECT'
>VConstant  MERGE = 'MERGE'
>VConstant  AUTH_BASIC = 'basic'
Supported HTTP Authentication methods
>VConstant  HTTP_1 = '1.1'
HTTP protocol versions
>VConstant  HTTP_0 = '1.0'
>VConstant  CONTENT_TYPE = 'Content-Type'
Content attributes
>VConstant  CONTENT_LENGTH = 'Content-Length'
>VConstant  ENC_URLENCODED = 'application/x-www-form-urlencoded'
POST data encoding methods
>VConstant  ENC_FORMDATA = 'multipart/form-data'
>VConstant  VTYPE_SCALAR = 'SCALAR'
Value types for Body key/value pairs
>VConstant  VTYPE_FILE = 'FILE'

Properties

>VPropertyprotectedresource $_fileInfoDb = null
static
Fileinfo magic database resource

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

Default valuenullDetails
Type
resource
>VPropertyprotectedboolean $_queryBracketsEscaped = true
Status if the http_build_query function escapes brackets
Default valuetrueDetails
Type
boolean
>VPropertyprotectedboolean $_unmaskStatus = false
Status for unmasking GET array params
Default valuefalseDetails
Type
boolean
>VPropertyprotected\Zend_Http_Client_Adapter_Interface $adapter = null
The adapter used to perform the actual connection to the server
Default valuenullDetails
Type
\Zend_Http_Client_Adapter_Interface
>VPropertyprotectedarray|null $auth
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.

Details
Type
array | null
>VPropertyprotectedarray $body_field_order = array()
Ordered list of keys from key/value pair data to include in body

An associative array, where each element is of the format: '' => VTYPE_SCALAR | VTYPE_FILE

Default valuearray()Details
Type
array
>VPropertyprotectedarray $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)
Configuration array, set using the constructor or using ::setConfig()
Default valuearray('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)Details
Type
array
>VPropertyprotected\Zend_Http_CookieJar $cookiejar = null
The client's cookie jar
Default valuenullDetails
Type
\Zend_Http_CookieJar
>VPropertyprotectedstring $enctype = null
Request body content type (for POST requests)
Default valuenullDetails
Type
string
>VPropertyprotectedarray $files = array()
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')

Default valuearray()Details
Type
array
>VPropertyprotectedarray $headers = array()
Associative array of request headers
Default valuearray()Details
Type
array
>VPropertyprotectedstring $last_request = null
The last HTTP request sent by the client, as string
Default valuenullDetails
Type
string
>VPropertyprotected\Zend_Http_Response $last_response = null
The last HTTP response received by the client
Default valuenullDetails
Type
\Zend_Http_Response
>VPropertyprotectedstring $method = self::GET
HTTP request method
Default valueself::GETDetails
Type
string
>VPropertyprotectedarray $paramsGet = array()
Associative array of GET parameters
Default valuearray()Details
Type
array
>VPropertyprotectedarray $paramsPost = array()
Associative array of POST parameters
Default valuearray()Details
Type
array
>VPropertyprotectedstring $raw_post_data = null
The raw post data to send.

Could be set by setRawData($data, $enctype).

Default valuenullDetails
Type
string
>VPropertyprotectedint $redirectCounter = 0
Redirection counter
Default value0Details
Type
int
>VPropertyprotected\Zend_Uri_Http $uri = null
Request URI
Default valuenullDetails
Type
\Zend_Uri_Http

Methods

methodpublic__construct(\Zend_Uri_Http | string $uri = null, array $config = null) : void

Constructor method.

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

Parameters
NameTypeDescription
$uri\Zend_Uri_Http | string
$configarray

Configuration key-value pairs.

methodprotected_detectFileMimeType(string $file) : string

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
NameTypeDescription
$filestring

File path

Returns
TypeDescription
stringMIME type
methodprotected_flattenParametersArray(array $parray, string $prefix = null) : array
static

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.

Parameters
NameTypeDescription
$parrayarray
$prefixstring
Returns
TypeDescription
array
Details
Since
1.9  
methodprotected_getParametersRecursive(array $parray, bool $urlencode = false) : array

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

Parameters
NameTypeDescription
$parrayarray

The parameters array

$urlencodebool

Whether to urlencode the name and value

Returns
TypeDescription
array
Details
Deprecated
since 1.9  
methodprotected_openTempStream() : resource

Create temporary stream

Returns
TypeDescription
resource
methodprotected_prepareBody() : string

Prepare the request body (for POST and PUT requests)

Returns
TypeDescription
string
Throws
ExceptionDescription
\Zend_Http_Client_Exception
methodprotected_prepareHeaders() : array

Prepare the request headers

Returns
TypeDescription
array
methodprotected_setParameter(string $type, string $name, string $value) : null

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

Parameters
NameTypeDescription
$typestring

GET or POST

$namestring
$valuestring
Returns
TypeDescription
null
methodpublicencodeAuthHeader(string $user, string $password, string $type = self::AUTH_BASIC) : string
static

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

Parameters
NameTypeDescription
$userstring
$passwordstring
$typestring
Returns
TypeDescription
string
Throws
ExceptionDescription
\Zend_Http_Client_Exception
Details
See
\http://www.faqs.org/rfcs/rfc2617.html  
methodpublicencodeFormData(string $boundary, string $name, mixed $value, string $filename = null, array $headers = array()) : string
static

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

Parameters
NameTypeDescription
$boundarystring
$namestring
$valuemixed
$filenamestring
$headersarray

Associative array of optional headers @example ("Content-Transfer-Encoding" => "binary")

Returns
TypeDescription
string
methodpublicgetAdapter() : \Zend_Http_Client_Adapter_Interface

Load the connection adapter

Returns
TypeDescription
\Zend_Http_Client_Adapter_Interface$adapter
methodpublicgetCookieJar() : \Zend_Http_CookieJar | null

Return the current cookie jar or null if none.

Returns
TypeDescription
\Zend_Http_CookieJar | null
methodpublicgetHeader(string $key) : string | array | null

Get the value of a specific header

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

Parameters
NameTypeDescription
$keystring
Returns
TypeDescription
string | array | nullThe header value or null if it is not set
methodpublicgetLastRequest() : string

Get the last HTTP request as string

Returns
TypeDescription
string
methodpublicgetLastResponse() : \Zend_Http_Response

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
TypeDescription
\Zend_Http_Responseor null if none
methodpublicgetRedirectionsCount() : int

Get the number of redirections done on the last request

Returns
TypeDescription
int
methodpublicgetStream() : boolean | string

Get status of streaming for received data

Returns
TypeDescription
boolean | string
methodpublicgetUnmaskStatus() : boolean

Returns the currently configured unmask status

Returns
TypeDescription
boolean
methodpublicgetUri(boolean $as_string = false) : \Zend_Uri_Http | string

Get the URI for the next request

Parameters
NameTypeDescription
$as_stringboolean

If true, will return the URI as a string

Returns
TypeDescription
\Zend_Uri_Http | string
methodpublicrequest(string $method = null) : \Zend_Http_Response

Send the HTTP request and return an HTTP response object

Parameters
NameTypeDescription
$methodstring
Returns
TypeDescription
\Zend_Http_Response
Throws
ExceptionDescription
\Zend_Http_Client_Exception
methodpublicresetParameters(bool $clearAll = false) : \Zend_Http_Client

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
NameTypeDescription
$clearAllbool

Should all data be cleared?

Returns
TypeDescription
\Zend_Http_Client
methodpublicsetAdapter(\Zend_Http_Client_Adapter_Interface | string $adapter) : null

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
NameTypeDescription
$adapter\Zend_Http_Client_Adapter_Interface | string
Returns
TypeDescription
null
Throws
ExceptionDescription
\Zend_Http_Client_Exception
methodpublicsetAuth(string | false $user, string $password = '', string $type = self::AUTH_BASIC) : \Zend_Http_Client

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);
Parameters
NameTypeDescription
$userstring | false

User name or false disable authentication

$passwordstring

Password

$typestring

Authentication type

Returns
TypeDescription
\Zend_Http_Client
Throws
ExceptionDescription
\Zend_Http_Client_Exception
Details
See
\http://www.faqs.org/rfcs/rfc2617.html  
methodpublicsetConfig(\Zend_Config $config = array()) : \Zend_Http_Client

Set configuration parameters for this HTTP client

Parameters
NameTypeDescription
$config\Zend_Config

| array $config

Returns
TypeDescription
\Zend_Http_Client
Throws
ExceptionDescription
\Zend_Http_Client_Exception
methodpublicsetCookie(\Zend_Http_Cookie | string $cookie, string | null $value = null) : \Zend_Http_Client

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
NameTypeDescription
$cookie\Zend_Http_Cookie | string
$valuestring | null

If "cookie" is a string, this is the cookie value.

Returns
TypeDescription
\Zend_Http_Client
Throws
ExceptionDescription
\Zend_Http_Client_Exception
methodpublicsetCookieJar(\Zend_Http_CookieJar | boolean $cookiejar = true) : \Zend_Http_Client

Set the HTTP client's cookie jar.

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

Parameters
NameTypeDescription
$cookiejar\Zend_Http_CookieJar | boolean

Existing cookiejar object, true to create a new one, false to disable

Returns
TypeDescription
\Zend_Http_Client
Throws
ExceptionDescription
\Zend_Http_Client_Exception
methodpublicsetEncType(string $enctype = self::ENC_URLENCODED) : \Zend_Http_Client

Set the encoding type for POST data

Parameters
NameTypeDescription
$enctypestring
Returns
TypeDescription
\Zend_Http_Client
methodpublicsetFileUpload(string $filename, string $formname, string $data = null, string $ctype = null) : \Zend_Http_Client

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
NameTypeDescription
$filenamestring

Name of file to upload, or name to save as

$formnamestring

Name of form element to send as

$datastring

Data to send (if null, $filename is read and sent)

$ctypestring

Content type to use (if $data is set and $ctype is null, will be application/octet-stream)

Returns
TypeDescription
\Zend_Http_Client
Throws
ExceptionDescription
\Zend_Http_Client_Exception
methodpublicsetHeaders(string | array $name, mixed $value = null) : \Zend_Http_Client

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
NameTypeDescription
$namestring | array

Header name, full header string ('Header: value') or an array of headers

$valuemixed

Header value or null

Returns
TypeDescription
\Zend_Http_Client
Throws
ExceptionDescription
\Zend_Http_Client_Exception
methodpublicsetMethod(string $method = self::GET) : \Zend_Http_Client

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
NameTypeDescription
$methodstring
Returns
TypeDescription
\Zend_Http_Client
Throws
ExceptionDescription
\Zend_Http_Client_Exception
methodpublicsetParameterGet(string | array $name, string $value = null) : \Zend_Http_Client

Set a GET parameter for the request.

Wrapper around _setParameter

Parameters
NameTypeDescription
$namestring | array
$valuestring
Returns
TypeDescription
\Zend_Http_Client
methodpublicsetParameterPost(string | array $name, string $value = null) : \Zend_Http_Client

Set a POST parameter for the request.

Wrapper around _setParameter

Parameters
NameTypeDescription
$namestring | array
$valuestring
Returns
TypeDescription
\Zend_Http_Client
methodpublicsetRawData(string | resource $data, string $enctype = null) : \Zend_Http_Client

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
NameTypeDescription
$datastring | resource
$enctypestring
Returns
TypeDescription
\Zend_Http_Client
methodpublicsetStream(string | boolean $streamfile = true) : \Zend_Http_Client

Set streaming for received data

Parameters
NameTypeDescription
$streamfilestring | boolean

Stream file, true for temp file, false/null for no streaming

Returns
TypeDescription
\Zend_Http_Client
methodpublicsetUnmaskStatus(boolean $status = true) : \Zend_Http_Client

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
NameTypeDescription
$statusboolean
Returns
TypeDescription
\Zend_Http_Client
methodpublicsetUri(\Zend_Uri_Http | string $uri) : \Zend_Http_Client

Set the URI for the next request

Parameters
NameTypeDescription
$uri\Zend_Uri_Http | string
Returns
TypeDescription
\Zend_Http_Client
Throws
ExceptionDescription
\Zend_Http_Client_Exception
Documentation was generated by phpDocumentor 2.0.0a12.