API Documentation

Gdata/HttpClient.php

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-2011 Zend Technologies USA Inc. (http://www.zend.com)  
license
New BSD License  
package
Zend_Gdata  
subpackage
Gdata  
version
$Id: HttpClient.php 23775 2011-03-01 17:25:24Z ralph $  

\Zend_Gdata_HttpClient

Gdata Http Client object.

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

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

Properties

Propertyprivateresource  $_authSubPrivateKeyId= '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.

Default valuenullDetails
Type
resource
Propertyprivatestring  $_authSubToken= 'null'

Token for AuthSub authentication.

If this token is set, AuthSub authentication is used.

Default valuenullDetails
Type
string
Propertyprivatestring  $_clientLoginKey= 'null'

Token for ClientLogin authentication.

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

Default valuenullDetails
Type
string
Propertyprivatestring  $_clientLoginToken= 'null'

Token for ClientLogin authentication.

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

Default valuenullDetails
Type
string
Propertyprotectedbool  $_streamingRequest= 'null'

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

Default valuenullDetails
Type
bool

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.

Inherited from: \Zend_Http_Client::__construct()
Parameters
Name Type Description
$uri \Zend_Uri_Http|string
$config array

Configuration key-value pairs.

methodprotected_detectFileMimeType( string $file ) : string

Attempt 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

Parameters
Name Type Description
$file string File path
Returns
Type Description
string MIME type
methodprotected_flattenParametersArray( array $parray, string $prefix = null ) : array
static

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

Parameters
Name Type Description
$parray array
$prefix string
Returns
Type Description
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.

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

Parameters
Name Type Description
$parray array The parameters array
$urlencode bool Whether to urlencode the name and value
Returns
Type Description
array
Details
deprecated
since 1.9  
methodprotected_openTempStream( ) : resource

Create temporary stream

Inherited from: \Zend_Http_Client::_openTempStream()
Returns
Type Description
resource
methodprotected_prepareBody( ) : string

Prepare the request body (for POST and PUT requests)

Returns
Type Description
string
Throws
Exception Description
\Zend_Http_Client_Exception
methodprotected_prepareHeaders( ) : array

Prepare the request headers

Inherited from: \Zend_Http_Client::_prepareHeaders()
Returns
Type Description
array
methodprotected_setParameter( string $type, string $name, string $value ) : null

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

Inherited from: \Zend_Http_Client::_setParameter()
Parameters
Name Type Description
$type string GET or POST
$name string
$value string
Returns
Type Description
null
methodpublicencodeAuthHeader( string $user, string $password, string $type = self ) : string
static

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

Inherited from: \Zend_Http_Client::encodeAuthHeader()
Parameters
Name Type Description
$user string
$password string
$type string
Returns
Type Description
string
Throws
Exception Description
\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.

Inherited from: \Zend_Http_Client::encodeFormData()
Parameters
Name Type Description
$boundary string
$name string
$value mixed
$filename string
$headers array

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

Returns
Type Description
string
methodpublicfilterHttpRequest( string $method, string $url, array $headers = array, string $body = null, string $contentType = null ) : array

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

Parameters
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
Returns
Type Description
array The processed values in an associative array, using the same names as the params
Throws
Exception Description
\Zend_Gdata_App_Exception if there was a signing failure
methodpublicfilterHttpResponse( \Zend_Http_Response $response ) : \Zend_Http_Response

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

Parameters
Name Type Description
$response \Zend_Http_Response The response object to filter
Returns
Type Description
\Zend_Http_Response The filterd response object
methodpublicgetAdapter( ) : \Zend_Http_Client_Adapter_Interface|string

Return the current connection adapter

Returns
Type Description
\Zend_Http_Client_Adapter_Interfacestring $adapter
methodpublicgetAuthSubPrivateKeyId( ) : string

Gets the openssl private key id

Returns
Type Description
string The private key
methodpublicgetAuthSubToken( ) : string

Gets the AuthSub token used for authentication

Returns
Type Description
string The token
methodpublicgetClientLoginToken( ) : string

Gets the ClientLogin token used for authentication

Returns
Type Description
string The token
methodpublicgetCookieJar( ) : \Zend_Http_CookieJar|null

Return the current cookie jar or null if none.

Inherited from: \Zend_Http_Client::getCookieJar()
Returns
Type Description
\Zend_Http_CookieJarnull
methodpublicgetHeader( string $key ) : string|array|null

Get the value of a specific header

Inherited from: \Zend_Http_Client::getHeader()

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

Parameters
Name Type Description
$key string
Returns
Type Description
stringarraynull The header value or null if it is not set
methodpublicgetLastRequest( ) : string

Get the last HTTP request as string

Inherited from: \Zend_Http_Client::getLastRequest()
Returns
Type Description
string
methodpublicgetLastResponse( ) : \Zend_Http_Response

Get 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

Returns
Type Description
\Zend_Http_Response or null if none
methodpublicgetRedirectionsCount( ) : int

Get the number of redirections done on the last request

Inherited from: \Zend_Http_Client::getRedirectionsCount()
Returns
Type Description
int
methodpublicgetStream( ) : boolean|string

Get status of streaming for received data

Inherited from: \Zend_Http_Client::getStream()
Returns
Type Description
booleanstring
methodpublicgetStreamingRequest( ) : boolean

Check whether the client is set to perform streaming requests.

Returns
Type Description
boolean True if yes, false otherwise.
methodpublicgetUnmaskStatus( ) : boolean

Returns the currently configured unmask status

Inherited from: \Zend_Http_Client::getUnmaskStatus()
Returns
Type Description
boolean
methodpublicgetUri( boolean $as_string = false ) : \Zend_Uri_Http|string

Get the URI for the next request

Inherited from: \Zend_Http_Client::getUri()
Parameters
Name Type Description
$as_string boolean If true, will return the URI as a string
Returns
Type Description
\Zend_Uri_Httpstring
methodpublicrequest( string $method = null ) : \Zend_Http_Response

Send the HTTP request and return an HTTP response object

Inherited from: \Zend_Http_Client::request()
Parameters
Name Type Description
$method string
Returns
Type Description
\Zend_Http_Response
Throws
Exception Description
\Zend_Http_Client_Exception
methodpublicresetParameters( bool $clearAll = false ) : \Zend_Http_Client

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

Parameters
Name Type Description
$clearAll bool

Should all data be cleared?

Returns
Type Description
\Zend_Http_Client
methodpublicsetAdapter( \Zend_Http_Client_Adapter_Interface $adapter ) : void

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
Name Type Description
$adapter \Zend_Http_Client_Adapter_Interface
Throws
Exception Description
\Zend_Http_Client_Exception
methodpublicsetAuth( string|false $user, string $password, string $type = self ) : \Zend_Http_Client

Set HTTP authentication parameters

Inherited from: \Zend_Http_Client::setAuth()

$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
Name Type Description
$user string|false User name or false disable authentication
$password string Password
$type string Authentication type
Returns
Type Description
\Zend_Http_Client
Throws
Exception Description
\Zend_Http_Client_Exception
Details
see
\http://www.faqs.org/rfcs/rfc2617.html  
methodpublicsetAuthSubPrivateKey( string $key, string $passphrase = null ) : \Zend_Gdata_HttpClient

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

Parameters
Name Type Description
$key string The private key
$passphrase string The optional private key passphrase
Returns
Type Description
\Zend_Gdata_HttpClient Provides a fluent interface
Throws
Exception Description
\Zend_Gdata_App_InvalidArgumentException
methodpublicsetAuthSubPrivateKeyFile( string $file, string $passphrase = null, bool $useIncludePath = false ) : void

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

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

Parameters
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
methodpublicsetAuthSubToken( string $token ) : \Zend_Gdata_HttpClient

Sets the AuthSub token used for authentication

Parameters
Name Type Description
$token string The token
Returns
Type Description
\Zend_Gdata_HttpClient Provides a fluent interface
methodpublicsetClientLoginToken( string $token ) : \Zend_Gdata_HttpClient

Sets the ClientLogin token used for authentication

Parameters
Name Type Description
$token string The token
Returns
Type Description
\Zend_Gdata_HttpClient Provides a fluent interface
methodpublicsetConfig( \Zend_Config $config = array ) : \Zend_Http_Client

Set configuration parameters for this HTTP client

Inherited from: \Zend_Http_Client::setConfig()
Parameters
Name Type Description
$config \Zend_Config

| array $config

Returns
Type Description
\Zend_Http_Client
Throws
Exception Description
\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.

Inherited from: \Zend_Http_Client::setCookie()
Parameters
Name Type Description
$cookie \Zend_Http_Cookie|string
$value string|null

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

Returns
Type Description
\Zend_Http_Client
Throws
Exception Description
\Zend_Http_Client_Exception
methodpublicsetCookieJar( \Zend_Http_CookieJar|boolean $cookiejar = true ) : \Zend_Http_Client

Set the HTTP client's cookie jar.

Inherited from: \Zend_Http_Client::setCookieJar()

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

Parameters
Name Type Description
$cookiejar \Zend_Http_CookieJar|boolean Existing cookiejar object, true to create a new one, false to disable
Returns
Type Description
\Zend_Http_Client
Throws
Exception Description
\Zend_Http_Client_Exception
methodpublicsetEncType( string $enctype = self ) : \Zend_Http_Client

Set the encoding type for POST data

Inherited from: \Zend_Http_Client::setEncType()
Parameters
Name Type Description
$enctype string
Returns
Type Description
\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)

Inherited from: \Zend_Http_Client::setFileUpload()

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

Returns
Type Description
\Zend_Http_Client
Throws
Exception Description
\Zend_Http_Client_Exception
methodpublicsetHeaders( string|array $name, mixed $value = null ) : \Zend_Http_Client

Set one or more request headers

Inherited from: \Zend_Http_Client::setHeaders()

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 Type Description
$name string|array

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

$value mixed Header value or null
Returns
Type Description
\Zend_Http_Client
Throws
Exception Description
\Zend_Http_Client_Exception
methodpublicsetMethod( string $method = self ) : \Zend_Http_Client

Set the next request's method

Inherited from: \Zend_Http_Client::setMethod()

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
Name Type Description
$method string
Returns
Type Description
\Zend_Http_Client
Throws
Exception Description
\Zend_Http_Client_Exception
methodpublicsetParameterGet( string|array $name, string $value = null ) : \Zend_Http_Client

Set a GET parameter for the request. Wrapper around _setParameter

Inherited from: \Zend_Http_Client::setParameterGet()
Parameters
Name Type Description
$name string|array
$value string
Returns
Type Description
\Zend_Http_Client
methodpublicsetParameterPost( string|array $name, string $value = null ) : \Zend_Http_Client

Set a POST parameter for the request. Wrapper around _setParameter

Inherited from: \Zend_Http_Client::setParameterPost()
Parameters
Name Type Description
$name string|array
$value string
Returns
Type Description
\Zend_Http_Client
methodpublicsetRawData( string|resource $data, string $enctype = null ) : \Zend_Http_Client

Set the raw (already encoded) POST data.

Inherited from: \Zend_Http_Client::setRawData()

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

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

Parameters
Name Type Description
$data string The request data
$enctype string The encoding type
Returns
Type Description
\Zend_Http_Client
methodpublicsetStream( string|boolean $streamfile = true ) : \Zend_Http_Client

Set streaming for received data

Inherited from: \Zend_Http_Client::setStream()
Parameters
Name Type Description
$streamfile string|boolean

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

Returns
Type Description
\Zend_Http_Client
methodpublicsetStreamingRequest( boolean $value ) : void

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

Parameters
Name Type Description
$value boolean The value to set.
methodpublicsetUnmaskStatus( boolean $status = true ) : \Zend_Http_Client

Set 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

Parameters
Name Type Description
$status boolean
Returns
Type Description
\Zend_Http_Client
methodpublicsetUri( \Zend_Uri_Http|string $uri ) : \Zend_Http_Client

Set the URI for the next request

Inherited from: \Zend_Http_Client::setUri()
Parameters
Name Type Description
$uri \Zend_Uri_Http|string
Returns
Type Description
\Zend_Http_Client
Throws
Exception Description
\Zend_Http_Client_Exception
Documentation was generated by DocBlox 0.13.3.