Gdata/HttpClient.php
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_Gdata
- Subpackage
- Gdata
- Version
- $Id: HttpClient.php 24593 2012-01-05 20:35:02Z matthew $
\Zend_Gdata_HttpClient
Class to extend the generic Zend Http Client with the ability to perform secure AuthSub requests
- Parent(s)
- \Zend_Http_Client
- Category
- Zend
- Copyright
- Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- License
- New BSD License
Constants
DELETE
= 'DELETE'- Inherited_from
- \Zend_Http_Client::DELETE
TRACE
= 'TRACE'- Inherited_from
- \Zend_Http_Client::TRACE
OPTIONS
= 'OPTIONS'- Inherited_from
- \Zend_Http_Client::OPTIONS
CONNECT
= 'CONNECT'- Inherited_from
- \Zend_Http_Client::CONNECT
MERGE
= 'MERGE'- Inherited_from
- \Zend_Http_Client::MERGE
HTTP_0
= '1.0'- Inherited_from
- \Zend_Http_Client::HTTP_0
CONTENT_LENGTH
= 'Content-Length'- Inherited_from
- \Zend_Http_Client::CONTENT_LENGTH
ENC_FORMDATA
= 'multipart/form-data'- Inherited_from
- \Zend_Http_Client::ENC_FORMDATA
VTYPE_FILE
= 'FILE'- Inherited_from
- \Zend_Http_Client::VTYPE_FILE
Properties

resource $_authSubPrivateKeyId = nullIf this value is set, it is assuemd that secure AuthSub is desired.
nullDetails- Type
- resource

string $_authSubToken = nullIf this token is set, AuthSub authentication is used.
nullDetails- Type
- string

string $_clientLoginKey = nullIf this token is set, and the AuthSub key is not set, ClientLogin authentication is used
nullDetails- Type
- string

string $_clientLoginToken = nullIf only this token is set, ClientLogin authentication is used.
nullDetails- Type
- string

resource $_fileInfoDb = nullThis variable is populated the first time _detectFileMimeType is called and is then reused on every call to this method
nullDetails- Type
- resource
- Inherited_from
- \Zend_Http_Client::$$_fileInfoDb

boolean $_queryBracketsEscaped = truetrueDetails- Type
- boolean
- Inherited_from
- \Zend_Http_Client::$$_queryBracketsEscaped

bool $_streamingRequest = nullnullDetails- Type
- bool

boolean $_unmaskStatus = falsefalseDetails- Type
- boolean
- Inherited_from
- \Zend_Http_Client::$$_unmaskStatus

\Zend_Http_Client_Adapter_Interface $adapter = nullnullDetails- Type
- \Zend_Http_Client_Adapter_Interface
- Inherited_from
- \Zend_Http_Client::$$adapter

array|null $authExpected 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.
- Type
- array | null
- Inherited_from
- \Zend_Http_Client::$$auth

array $body_field_order = array()An associative array, where each element is of the format:
'
array()Details- Type
- array
- Inherited_from
- \Zend_Http_Client::$$body_field_order

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)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)Details- Type
- array
- Inherited_from
- \Zend_Http_Client::$$config

\Zend_Http_CookieJar $cookiejar = nullnullDetails- Type
- \Zend_Http_CookieJar
- Inherited_from
- \Zend_Http_Client::$$cookiejar

array $files = array()An associative array, where each element is of the format: 'name' => array('filename.txt', 'text/plain', 'This is the actual file contents')
array()Details- Type
- array
- Inherited_from
- \Zend_Http_Client::$$files

array $headers = array()array()Details- Type
- array
- Inherited_from
- \Zend_Http_Client::$$headers

string $last_request = nullnullDetails- Type
- string
- Inherited_from
- \Zend_Http_Client::$$last_request

\Zend_Http_Response $last_response = nullnullDetails- Type
- \Zend_Http_Response
- Inherited_from
- \Zend_Http_Client::$$last_response

string $method = self::GETself::GETDetails- Type
- string
- Inherited_from
- \Zend_Http_Client::$$method

array $paramsGet = array()array()Details- Type
- array
- Inherited_from
- \Zend_Http_Client::$$paramsGet

array $paramsPost = array()array()Details- Type
- array
- Inherited_from
- \Zend_Http_Client::$$paramsPost

string $raw_post_data = nullCould be set by setRawData($data, $enctype).
nullDetails- Type
- string
- Inherited_from
- \Zend_Http_Client::$$raw_post_data

int $redirectCounter = 00Details- Type
- int
- Inherited_from
- \Zend_Http_Client::$$redirectCounter

\Zend_Uri_Http $uri = nullnullDetails- Type
- \Zend_Uri_Http
- Inherited_from
- \Zend_Http_Client::$$uri
Methods

__construct(\Zend_Uri_Http | string $uri = null, array $config = null) : voidWill create a new HTTP client. Accepts the target URL and optionally configuration array.
| Name | Type | Description |
|---|---|---|
| $uri | \Zend_Uri_Http | string | |
| $config | array | Configuration key-value pairs. |

_detectFileMimeType(string $file) : stringAttempt to detect the MIME type of a file using available extensions
Inherited from: \Zend_Http_Client::_detectFileMimeType()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
| Name | Type | Description |
|---|---|---|
| $file | string | File path |
| Type | Description |
|---|---|
| string | MIME type |

_flattenParametersArray(array $parray, string $prefix = null) : arrayConvert an array of parameters into a flat array of (key, value) pairs
Inherited from: \Zend_Http_Client::_flattenParametersArray()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.
| Name | Type | Description |
|---|---|---|
| $parray | array | |
| $prefix | string |
| Type | Description |
|---|---|
| array |
- Since
- 1.9

_getParametersRecursive(array $parray, bool $urlencode = false) : arrayHelper method that gets a possibly multi-level parameters array (get or post) and flattens it.
Inherited from: \Zend_Http_Client::_getParametersRecursive()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
| Name | Type | Description |
|---|---|---|
| $parray | array | The parameters array |
| $urlencode | bool | Whether to urlencode the name and value |
| Type | Description |
|---|---|
| array |

_prepareBody() : stringPrepare the request body (for POST and PUT requests)
| Type | Description |
|---|---|
| string |
| Exception | Description |
|---|---|
| \Zend_Http_Client_Exception |

_setParameter(string $type, string $name, string $value) : nullSet a GET or POST parameter - used by SetParameterGet and SetParameterPost
Inherited from: \Zend_Http_Client::_setParameter()| Name | Type | Description |
|---|---|---|
| $type | string | GET or POST |
| $name | string | |
| $value | string |
| Type | Description |
|---|---|
| null |

encodeAuthHeader(string $user, string $password, string $type = self::AUTH_BASIC) : stringCreate a HTTP authentication "Authorization:" header according to the specified user, password and authentication method.
Inherited from: \Zend_Http_Client::encodeAuthHeader()| Name | Type | Description |
|---|---|---|
| $user | string | |
| $password | string | |
| $type | string |
| Type | Description |
|---|---|
| string |
| Exception | Description |
|---|---|
| \Zend_Http_Client_Exception |
- See
- \http://www.faqs.org/rfcs/rfc2617.html

encodeFormData(string $boundary, string $name, mixed $value, string $filename = null, array $headers = array()) : stringEncode data to a multipart/form-data part suitable for a POST request.
Inherited from: \Zend_Http_Client::encodeFormData()| Name | Type | Description |
|---|---|---|
| $boundary | string | |
| $name | string | |
| $value | mixed | |
| $filename | string | |
| $headers | array | Associative array of optional headers @example ("Content-Transfer-Encoding" => "binary") |
| Type | Description |
|---|---|
| string |

filterHttpRequest(string $method, string $url, array $headers = array(), string $body = null, string $contentType = null) : arrayFilters the HTTP requests being sent to add the Authorization header.
If both AuthSub and ClientLogin tokens are set, AuthSub takes precedence. If an AuthSub key is set, then secure AuthSub authentication is used, and the request is signed. Requests must be signed only with the private key corresponding to the public key registered with Google. If an AuthSub key is set, but openssl support is not enabled in the PHP installation, an exception is thrown.
| Name | Type | Description |
|---|---|---|
| $method | string | The HTTP method |
| $url | string | The URL |
| $headers | array | An associate array of headers to be sent with the request or null |
| $body | string | The body of the request or null |
| $contentType | string | The MIME content type of the body or null |
| Type | Description |
|---|---|
| array | The processed values in an associative array, using the same names as the params |
| Exception | Description |
|---|---|
| \Zend_Gdata_App_Exception | if there was a signing failure |

filterHttpResponse(\Zend_Http_Response $response) : \Zend_Http_ResponseMethod for filtering the HTTP response, though no filtering is currently done.
| Name | Type | Description |
|---|---|---|
| $response | \Zend_Http_Response | The response object to filter |
| Type | Description |
|---|---|
| \Zend_Http_Response | The filterd response object |

getAdapter() : \Zend_Http_Client_Adapter_Interface | stringReturn the current connection adapter
| Type | Description |
|---|---|
| \Zend_Http_Client_Adapter_Interface | string | $adapter |

getAuthSubPrivateKeyId() : stringGets the openssl private key id
| Type | Description |
|---|---|
| string | The private key |

getAuthSubToken() : stringGets the AuthSub token used for authentication
| Type | Description |
|---|---|
| string | The token |

getClientLoginToken() : stringGets the ClientLogin token used for authentication
| Type | Description |
|---|---|
| string | The token |

getCookieJar() : \Zend_Http_CookieJar | nullReturn the current cookie jar or null if none.
Inherited from: \Zend_Http_Client::getCookieJar()| Type | Description |
|---|---|
| \Zend_Http_CookieJar | null |

getHeader(string $key) : string | array | nullNote that if the header has more than one value, an array will be returned.
| Name | Type | Description |
|---|---|---|
| $key | string |
| Type | Description |
|---|---|
| string | array | null | The header value or null if it is not set |

getLastResponse() : \Zend_Http_ResponseGet the last HTTP response received by this client
Inherited from: \Zend_Http_Client::getLastResponse()If $config['storeresponse'] is set to false, or no response was stored yet, will return null
| Type | Description |
|---|---|
| \Zend_Http_Response | or null if none |

getRedirectionsCount() : intGet the number of redirections done on the last request
Inherited from: \Zend_Http_Client::getRedirectionsCount()| Type | Description |
|---|---|
| int |

getStreamingRequest() : booleanCheck whether the client is set to perform streaming requests.
| Type | Description |
|---|---|
| boolean | True if yes, false otherwise. |

getUnmaskStatus() : booleanReturns the currently configured unmask status
Inherited from: \Zend_Http_Client::getUnmaskStatus()| Type | Description |
|---|---|
| boolean |

getUri(boolean $as_string = false) : \Zend_Uri_Http | string| Name | Type | Description |
|---|---|---|
| $as_string | boolean | If true, will return the URI as a string |
| Type | Description |
|---|---|
| \Zend_Uri_Http | string |

request(string $method = null) : \Zend_Http_ResponseSend the HTTP request and return an HTTP response object
Inherited from: \Zend_Http_Client::request()| Name | Type | Description |
|---|---|---|
| $method | string |
| Type | Description |
|---|---|
| \Zend_Http_Response |
| Exception | Description |
|---|---|
| \Zend_Http_Client_Exception |

resetParameters(bool $clearAll = false) : \Zend_Http_ClientClear all custom parameters we set.
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_*
| Name | Type | Description |
|---|---|---|
| $clearAll | bool | Should all data be cleared? |
| Type | Description |
|---|---|
| \Zend_Http_Client |

setAdapter(\Zend_Http_Client_Adapter_Interface $adapter) : voidLoad 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
| Name | Type | Description |
|---|---|---|
| $adapter | \Zend_Http_Client_Adapter_Interface |
| Exception | Description |
|---|---|
| \Zend_Http_Client_Exception |

setAuth(string | false $user, string $password = '', string $type = self::AUTH_BASIC) : \Zend_Http_Client$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);
| Name | Type | Description |
|---|---|---|
| $user | string | false | User name or false disable authentication |
| $password | string | Password |
| $type | string | Authentication type |
| Type | Description |
|---|---|
| \Zend_Http_Client |
| Exception | Description |
|---|---|
| \Zend_Http_Client_Exception |
- See
- \http://www.faqs.org/rfcs/rfc2617.html

setAuthSubPrivateKey(string $key, string $passphrase = null) : \Zend_Gdata_HttpClientSets the PEM formatted private key to be used for secure AuthSub auth.
In order to call this method, openssl must be enabled in your PHP installation. Otherwise, a Zend_Gdata_App_InvalidArgumentException will be thrown.
| Name | Type | Description |
|---|---|---|
| $key | string | The private key |
| $passphrase | string | The optional private key passphrase |
| Type | Description |
|---|---|
| \Zend_Gdata_HttpClient | Provides a fluent interface |
| Exception | Description |
|---|---|
| \Zend_Gdata_App_InvalidArgumentException |

setAuthSubPrivateKeyFile(string $file, string $passphrase = null, bool $useIncludePath = false) : voidSets the PEM formatted private key, as read from a file.
This method reads the file and then calls setAuthSubPrivateKey() with the file contents.
| Name | Type | Description |
|---|---|---|
| $file | string | The location of the file containing the PEM key |
| $passphrase | string | The optional private key passphrase |
| $useIncludePath | bool | Whether to search the include_path for the file |

setAuthSubToken(string $token) : \Zend_Gdata_HttpClientSets the AuthSub token used for authentication
| Name | Type | Description |
|---|---|---|
| $token | string | The token |
| Type | Description |
|---|---|
| \Zend_Gdata_HttpClient | Provides a fluent interface |

setClientLoginToken(string $token) : \Zend_Gdata_HttpClientSets the ClientLogin token used for authentication
| Name | Type | Description |
|---|---|---|
| $token | string | The token |
| Type | Description |
|---|---|
| \Zend_Gdata_HttpClient | Provides a fluent interface |

setConfig(\Zend_Config $config = array()) : \Zend_Http_ClientSet configuration parameters for this HTTP client
Inherited from: \Zend_Http_Client::setConfig()| Name | Type | Description |
|---|---|---|
| $config | \Zend_Config | | array $config |
| Type | Description |
|---|---|
| \Zend_Http_Client |
| Exception | Description |
|---|---|
| \Zend_Http_Client_Exception |

setCookie(\Zend_Http_Cookie | string $cookie, string | null $value = null) : \Zend_Http_ClientIf the client has no Cookie Jar, the cookies will be added directly to the headers array as "Cookie" headers.
| Name | Type | Description |
|---|---|---|
| $cookie | \Zend_Http_Cookie | string | |
| $value | string | null | If "cookie" is a string, this is the cookie value. |
| Type | Description |
|---|---|
| \Zend_Http_Client |
| Exception | Description |
|---|---|
| \Zend_Http_Client_Exception |

setCookieJar(\Zend_Http_CookieJar | boolean $cookiejar = true) : \Zend_Http_ClientA cookie jar is an object that holds and maintains cookies across HTTP requests and responses.
| Name | Type | Description |
|---|---|---|
| $cookiejar | \Zend_Http_CookieJar | boolean | Existing cookiejar object, true to create a new one, false to disable |
| Type | Description |
|---|---|
| \Zend_Http_Client |
| Exception | Description |
|---|---|
| \Zend_Http_Client_Exception |

setEncType(string $enctype = self::ENC_URLENCODED) : \Zend_Http_Client| Name | Type | Description |
|---|---|---|
| $enctype | string |
| Type | Description |
|---|---|
| \Zend_Http_Client |

setFileUpload(string $filename, string $formname, string $data = null, string $ctype = null) : \Zend_Http_ClientSet a file to upload (using a POST request)
Inherited from: \Zend_Http_Client::setFileUpload()Can be used in two ways:
- $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().
- $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.
| Name | Type | Description |
|---|---|---|
| $filename | string | Name of file to upload, or name to save as |
| $formname | string | Name of form element to send as |
| $data | string | Data to send (if null, $filename is read and sent) |
| $ctype | string | Content type to use (if $data is set and $ctype is null, will be application/octet-stream) |
| Type | Description |
|---|---|
| \Zend_Http_Client |
| Exception | Description |
|---|---|
| \Zend_Http_Client_Exception |

setHeaders(string | array $name, mixed $value = null) : \Zend_Http_ClientThis 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.
| Name | Type | Description |
|---|---|---|
| $name | string | array | Header name, full header string ('Header: value') or an array of headers |
| $value | mixed | Header value or null |
| Type | Description |
|---|---|
| \Zend_Http_Client |
| Exception | Description |
|---|---|
| \Zend_Http_Client_Exception |

setMethod(string $method = self::GET) : \Zend_Http_ClientValidated 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.
| Name | Type | Description |
|---|---|---|
| $method | string |
| Type | Description |
|---|---|
| \Zend_Http_Client |
| Exception | Description |
|---|---|
| \Zend_Http_Client_Exception |

setParameterGet(string | array $name, string $value = null) : \Zend_Http_ClientWrapper around _setParameter
| Name | Type | Description |
|---|---|---|
| $name | string | array | |
| $value | string |
| Type | Description |
|---|---|
| \Zend_Http_Client |

setParameterPost(string | array $name, string $value = null) : \Zend_Http_ClientWrapper around _setParameter
| Name | Type | Description |
|---|---|---|
| $name | string | array | |
| $value | string |
| Type | Description |
|---|---|
| \Zend_Http_Client |

setRawData(string | resource $data, string $enctype = null) : \Zend_Http_ClientThis 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.
| Name | Type | Description |
|---|---|---|
| $data | string | resource | |
| $enctype | string |
| Type | Description |
|---|---|
| \Zend_Http_Client |

setRawDataStream(string $data, string $enctype = null) : \Zend_Http_ClientSet the raw (already encoded) POST data from a stream source.
This is used to support POSTing from open file handles without caching the entire body into memory. It is a wrapper around Zend_Http_Client::setRawData().
| Name | Type | Description |
|---|---|---|
| $data | string | The request data |
| $enctype | string | The encoding type |
| Type | Description |
|---|---|
| \Zend_Http_Client |

setStream(string | boolean $streamfile = true) : \Zend_Http_Client| Name | Type | Description |
|---|---|---|
| $streamfile | string | boolean | Stream file, true for temp file, false/null for no streaming |
| Type | Description |
|---|---|
| \Zend_Http_Client |

setStreamingRequest(boolean $value) : voidSet the streamingRequest variable which controls whether we are sending the raw (already encoded) POST data from a stream source.
| Name | Type | Description |
|---|---|---|
| $value | boolean | The value to set. |

setUnmaskStatus(boolean $status = true) : \Zend_Http_ClientSet the unmask feature for GET parameters as array
Inherited from: \Zend_Http_Client::setUnmaskStatus()Example: foo%5B0%5D=a&foo%5B1%5D=b becomes foo=a&foo=b
This is usefull for some services
| Name | Type | Description |
|---|---|---|
| $status | boolean |
| Type | Description |
|---|---|
| \Zend_Http_Client |

setUri(\Zend_Uri_Http | string $uri) : \Zend_Http_Client| Name | Type | Description |
|---|---|---|
| $uri | \Zend_Uri_Http | string |
| Type | Description |
|---|---|
| \Zend_Http_Client |
| Exception | Description |
|---|---|
| \Zend_Http_Client_Exception |