Gdata Http Client object.

Class to extend the generic Zend Http Client with the ability to perform secure AuthSub requests

category Zend
package Zend_Gdata
subpackage Gdata
copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
license New BSD License
inherited_from \Zend_Http_Client

 Methods

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

__construct(\Zend_Uri_Http|string $uri = null, array $config = null
Inherited

Parameters

$uri

\Zend_Uri_Httpstring

$config

array

Configuration key-value pairs.

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

encodeAuthHeader(string $user, string $password, string $type = self::AUTH_BASIC) : string
InheritedStatic
see

Parameters

$user

string

$password

string

$type

string

Exceptions

\Zend_Http_Client_Exception

Returns

string

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

encodeFormData(string $boundary, string $name, mixed $value, string $filename = null, array $headers = array()) : string
InheritedStatic

Parameters

$boundary

string

$name

string

$value

mixed

$filename

string

$headers

array

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

Returns

string

Filters the HTTP requests being sent to add the Authorization header.

filterHttpRequest(string $method, string $url, array $headers = array(), string $body = null, string $contentType = null) : array

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.

Parameters

$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

Exceptions

\Zend_Gdata_App_Exception if there was a signing failure

Returns

arrayThe processed values in an associative array, using the same names as the params

Method for filtering the HTTP response, though no filtering is currently done.

filterHttpResponse(\Zend_Http_Response $response) : \Zend_Http_Response

Parameters

$response

\Zend_Http_Response

The response object to filter

Returns

\Zend_Http_ResponseThe filterd response object

Return the current connection adapter

getAdapter() : \Zend_Http_Client_Adapter_Interface | string
inherited_from \Zend_Http_Client::getAdapter()

Returns

\Zend_Http_Client_Adapter_Interfacestring$adapter

Load the connection adapter

getAdapter() : \Zend_Http_Client_Adapter_Interface
Inherited

Returns

\Zend_Http_Client_Adapter_Interface$adapter

Gets the openssl private key id

getAuthSubPrivateKeyId() : string

Returns

stringThe private key

Gets the AuthSub token used for authentication

getAuthSubToken() : string

Returns

stringThe token

Gets the ClientLogin token used for authentication

getClientLoginToken() : string

Returns

stringThe token

Return the current cookie jar or null if none.

getCookieJar() : \Zend_Http_CookieJar | null
Inherited

Returns

\Zend_Http_CookieJarnull

Get the value of a specific header

getHeader(string $key) : string | array | null
Inherited

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

Parameters

$key

string

Returns

stringarraynullThe header value or null if it is not set

Get the last HTTP request as string

getLastRequest() : string
Inherited

Returns

string

Get the last HTTP response received by this client

getLastResponse() : \Zend_Http_Response
Inherited

If $config['storeresponse'] is set to false, or no response was stored yet, will return null

Returns

\Zend_Http_Responseor null if none

Get the number of redirections done on the last request

getRedirectionsCount() : integer
Inherited

Returns

integer

Get status of streaming for received data

getStream() : boolean | string
Inherited

Returns

booleanstring

Check whether the client is set to perform streaming requests.

getStreamingRequest() : boolean

Returns

booleanTrue if yes, false otherwise.

Returns the currently configured unmask status

getUnmaskStatus() : boolean
Inherited

Returns

boolean

Get the URI for the next request

getUri(boolean $as_string = false) : \Zend_Uri_Http | string
Inherited

Parameters

$as_string

boolean

If true, will return the URI as a string

Returns

\Zend_Uri_Httpstring

Send the HTTP request and return an HTTP response object

request(string $method = null) : \Zend_Http_Response
Inherited

Parameters

$method

string

Exceptions

\Zend_Http_Client_Exception

Returns

\Zend_Http_Response

Clear all custom parameters we set.

resetParameters(boolean $clearAll = false) : \Zend_Http_Client

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

inherited_from \Zend_Http_Client::resetParameters()

Parameters

$clearAll

Returns

\Zend_Http_Client

Clear all GET and POST parameters

resetParameters(boolean $clearAll = false) : \Zend_Http_Client
Inherited

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

$clearAll

boolean

Should all data be cleared?

Returns

\Zend_Http_Client

Load the connection adapter

setAdapter(\Zend_Http_Client_Adapter_Interface $adapter) : void

While this method is not called more than one for a client, it is seperated from ->request() to preserve logic and readability

inherited_from \Zend_Http_Client::setAdapter()

Parameters

$adapter

\Zend_Http_Client_Adapter_Interface

Load the connection adapter

setAdapter(\Zend_Http_Client_Adapter_Interface|string $adapter) : null
Inherited

While this method is not called more than one for a client, it is seperated from ->request() to preserve logic and readability

Parameters

$adapter

\Zend_Http_Client_Adapter_Interfacestring

Exceptions

\Zend_Http_Client_Exception

Returns

null

Set HTTP authentication parameters

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

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

see

Parameters

$user

stringfalse

User name or false disable authentication

$password

string

Password

$type

string

Authentication type

Exceptions

\Zend_Http_Client_Exception

Returns

\Zend_Http_Client

Sets the PEM formatted private key to be used for secure AuthSub auth.

setAuthSubPrivateKey(string $key, string $passphrase = null) : \Zend_Gdata_HttpClient

In order to call this method, openssl must be enabled in your PHP installation. Otherwise, a Zend_Gdata_App_InvalidArgumentException will be thrown.

Parameters

$key

string

The private key

$passphrase

string

The optional private key passphrase

Exceptions

\Zend_Gdata_App_InvalidArgumentException

Returns

\Zend_Gdata_HttpClientProvides a fluent interface

Sets the PEM formatted private key, as read from a file.

setAuthSubPrivateKeyFile(string $file, string $passphrase = null, boolean $useIncludePath = false) : void

This method reads the file and then calls setAuthSubPrivateKey() with the file contents.

Parameters

$file

string

The location of the file containing the PEM key

$passphrase

string

The optional private key passphrase

$useIncludePath

boolean

Whether to search the include_path for the file

Sets the AuthSub token used for authentication

setAuthSubToken(string $token) : \Zend_Gdata_HttpClient

Parameters

$token

string

The token

Returns

\Zend_Gdata_HttpClientProvides a fluent interface

Sets the ClientLogin token used for authentication

setClientLoginToken(string $token) : \Zend_Gdata_HttpClient

Parameters

$token

string

The token

Returns

\Zend_Gdata_HttpClientProvides a fluent interface

Set configuration parameters for this HTTP client

setConfig($config = array()) : \Zend_Http_Client
Inherited

Parameters

$config

Exceptions

\Zend_Http_Client_Exception

Returns

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

setCookie(\Zend_Http_Cookie|string $cookie, string|null $value = null) : \Zend_Http_Client
Inherited

Parameters

$cookie

\Zend_Http_Cookiestring

$value

stringnull

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

Exceptions

\Zend_Http_Client_Exception

Returns

\Zend_Http_Client

Set the HTTP client's cookie jar.

setCookieJar(\Zend_Http_CookieJar|boolean $cookiejar = true) : \Zend_Http_Client
Inherited

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

Parameters

$cookiejar

\Zend_Http_CookieJarboolean

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

Exceptions

\Zend_Http_Client_Exception

Returns

\Zend_Http_Client

Set the encoding type for POST data

setEncType(string $enctype = self::ENC_URLENCODED) : \Zend_Http_Client
Inherited

Parameters

$enctype

string

Returns

\Zend_Http_Client

Set a file to upload (using a POST request)

setFileUpload(string $filename, string $formname, string $data = null, string $ctype = null) : \Zend_Http_Client
Inherited

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

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

Exceptions

\Zend_Http_Client_Exception

Returns

\Zend_Http_Client

Set one or more request headers

setHeaders(string|array $name, mixed $value = null) : \Zend_Http_Client
Inherited

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

$name

stringarray

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

$value

mixed

Header value or null

Exceptions

\Zend_Http_Client_Exception

Returns

\Zend_Http_Client

Set the next request's method

setMethod(string $method = self::GET) : \Zend_Http_Client
Inherited

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

$method

string

Exceptions

\Zend_Http_Client_Exception

Returns

\Zend_Http_Client

Set a GET parameter for the request. Wrapper around _setParameter

setParameterGet(string|array $name, string $value = null) : \Zend_Http_Client
Inherited

Parameters

$name

stringarray

$value

string

Returns

\Zend_Http_Client

Set a POST parameter for the request. Wrapper around _setParameter

setParameterPost(string|array $name, string $value = null) : \Zend_Http_Client
Inherited

Parameters

$name

stringarray

$value

string

Returns

\Zend_Http_Client

Set the raw (already encoded) POST data.

setRawData(string|resource $data, string $enctype = null) : \Zend_Http_Client
Inherited

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

$data

stringresource

$enctype

string

Returns

\Zend_Http_Client

Set the raw (already encoded) POST data from a stream source.

setRawDataStream(string $data, string $enctype = null) : \Zend_Http_Client

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().

Parameters

$data

string

The request data

$enctype

string

The encoding type

Returns

\Zend_Http_Client

Set streaming for received data

setStream(string|boolean $streamfile = true) : \Zend_Http_Client
Inherited

Parameters

$streamfile

stringboolean

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

Returns

\Zend_Http_Client

Set the streamingRequest variable which controls whether we are sending the raw (already encoded) POST data from a stream source.

setStreamingRequest(boolean $value) : void

Parameters

$value

boolean

The value to set.

Set the unmask feature for GET parameters as array

setUnmaskStatus(boolean $status = true) : \Zend_Http_Client
Inherited

Example: foo%5B0%5D=a&foo%5B1%5D=b becomes foo=a&foo=b

This is usefull for some services

Parameters

$status

boolean

Returns

\Zend_Http_Client

Set the URI for the next request

setUri(\Zend_Uri_Http|string $uri) : \Zend_Http_Client
Inherited

Parameters

$uri

\Zend_Uri_Httpstring

Exceptions

\Zend_Http_Client_Exception

Returns

\Zend_Http_Client

Attempt to detect the MIME type of a file using available extensions

_detectFileMimeType(string $file) : string
Inherited

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

$file

string

File path

Returns

stringMIME type

Convert an array of parameters into a flat array of (key, value) pairs

_flattenParametersArray(array $parray, string $prefix = null) : array
InheritedStatic

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

Parameters

$parray

array

$prefix

string

Returns

array

Helper method that gets a possibly multi-level parameters array (get or post) and flattens it.

_getParametersRecursive(array $parray, boolean $urlencode = false) : array
Inherited

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

Parameters

$parray

array

The parameters array

$urlencode

boolean

Whether to urlencode the name and value

Returns

array

Create temporary stream

_openTempStream() : resource
Inherited

Returns

resource

Prepare the request body (for POST and PUT requests)

_prepareBody() : string
inherited_from \Zend_Http_Client::_prepareBody()

Exceptions

\Zend_Http_Client_Exception

Returns

string

Prepare the request body (for POST and PUT requests)

_prepareBody() : string
Inherited

Exceptions

\Zend_Http_Client_Exception

Returns

string

Prepare the request headers

_prepareHeaders() : array
Inherited

Returns

array

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

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

Parameters

$type

string

GET or POST

$name

string

$value

string

Returns

null

Ensure a header value is valid per RFC 7230.

_validateHeaderValue(string|object|array $value, boolean $recurse = true
Inherited
see

Parameters

$value

stringobjectarray

$recurse

boolean

 Properties

 

Fileinfo magic database resource

$_fileInfoDb : resource

Default

null
Static

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

 

Status if the http_build_query function escapes brackets

$_queryBracketsEscaped : boolean

Default

true
 

True if this request is being made with data supplied by a stream object instead of a raw encoded string.

$_streamingRequest : boolean

Default

null
 

Status for unmasking GET array params

$_unmaskStatus : boolean

Default

false
 

The adapter used to perform the actual connection to the server

$adapter : \Zend_Http_Client_Adapter_Interface

Default

null
 

HTTP Authentication settings

$auth : array | null

Default

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.

 

Ordered list of keys from key/value pair data to include in body

$body_field_order : array

Default

array()

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

 

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

$config : array

Default

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)
 

The client's cookie jar

$cookiejar : \Zend_Http_CookieJar

Default

null
 

Request body content type (for POST requests)

$enctype : string

Default

null
 

File upload arrays (used in POST requests)

$files : array

Default

array()

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

 

Associative array of request headers

$headers : array

Default

array()
 

The last HTTP request sent by the client, as string

$last_request : string

Default

null
 

The last HTTP response received by the client

$last_response : \Zend_Http_Response

Default

null
 

HTTP request method

$method : string

Default

self::GET
 

Associative array of GET parameters

$paramsGet : array

Default

array()
 

Associative array of POST parameters

$paramsPost : array

Default

array()
 

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

$raw_post_data : string

Default

null
 

Redirection counter

$redirectCounter : integer

Default

0
 

Request URI

$uri : \Zend_Uri_Http

Default

null
 

OpenSSL private key resource id This key is used for AuthSub authentication. If this value is set, it is assuemd that secure AuthSub is desired.

$_authSubPrivateKeyId : resource

Default

null
 

Token for AuthSub authentication.

$_authSubToken : string

Default

null

If this token is set, AuthSub authentication is used.

 

Token for ClientLogin authentication.

$_clientLoginKey : string

Default

null

If this token is set, and the AuthSub key is not set, ClientLogin authentication is used

 

Token for ClientLogin authentication.

$_clientLoginToken : string

Default

null

If only this token is set, ClientLogin authentication is used.

 Constants

 

Supported HTTP Authentication methods

AUTH_BASIC = 'basic' 
 

CONNECT

CONNECT = 'CONNECT' 
 

CONTENT_LENGTH

CONTENT_LENGTH = 'Content-Length' 
 

Content attributes

CONTENT_TYPE = 'Content-Type' 
 

DELETE

DELETE = 'DELETE' 
 

ENC_FORMDATA

ENC_FORMDATA = 'multipart/form-data' 
 

POST data encoding methods

ENC_URLENCODED = 'application/x-www-form-urlencoded' 
 

HTTP request methods

GET = 'GET' 
 

HEAD

HEAD = 'HEAD' 
 

HTTP_0

HTTP_0 = '1.0' 
 

HTTP protocol versions

HTTP_1 = '1.1' 
 

MERGE

MERGE = 'MERGE' 
 

OPTIONS

OPTIONS = 'OPTIONS' 
 

PATCH

PATCH = 'PATCH' 
 

POST

POST = 'POST' 
 

PUT

PUT = 'PUT' 
 

TRACE

TRACE = 'TRACE' 
 

VTYPE_FILE

VTYPE_FILE = 'FILE' 
 

Value types for Body key/value pairs

VTYPE_SCALAR = 'SCALAR'