API Documentation

Http/Client.php

Includes Classes 
category
Zend
copyright
Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
license
http://framework.zend.com/license/new-bsd New BSD License
package
Zend_Http
subpackage
Client
version
$Id: Client.php 22539 2010-07-08 12:47:44Z shahar $
Classes
Zend_Http_Client

Description

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.

Zend_Http_Client

category
Zend
copyright
Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
license
http://framework.zend.com/license/new-bsd New BSD License
package
Zend_Http
subpackage
Client
throws
todo
Implement proxy settings
Constants
GET
POST
PUT
HEAD
DELETE
TRACE
OPTIONS
CONNECT
MERGE
AUTH_BASIC
HTTP_1
HTTP_0
CONTENT_TYPE
CONTENT_LENGTH
ENC_URLENCODED
ENC_FORMDATA
Properties
$config
$adapter
$uri
$headers
$method
$paramsGet
$paramsPost
$enctype
$raw_post_data
$auth
$files
$cookiejar
$last_request
$last_response
$redirectCounter
$_fileInfoDb
Methods
__construct
setUri
getUri
setConfig
setMethod
setHeaders
getHeader
setParameterGet
setParameterPost
_setParameter
getRedirectionsCount
setAuth
setCookieJar
getCookieJar
setCookie
setFileUpload
setEncType
setRawData
resetParameters
getLastRequest
getLastResponse
setAdapter
getAdapter
setStream
getStream
_openTempStream
request
_prepareHeaders
_prepareBody
_getParametersRecursive
_detectFileMimeType
encodeFormData
encodeAuthHeader
_flattenParametersArray

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)

Constants

GET

 GET = 'GET'

HTTP request methods

Details

value
GET

POST

 POST = 'POST'

Details

value
POST

PUT

 PUT = 'PUT'

Details

value
PUT

HEAD

 HEAD = 'HEAD'

Details

value
HEAD

DELETE

 DELETE = 'DELETE'

Details

value
DELETE

TRACE

 TRACE = 'TRACE'

Details

value
TRACE

OPTIONS

 OPTIONS = 'OPTIONS'

Details

value
OPTIONS

CONNECT

 CONNECT = 'CONNECT'

Details

value
CONNECT

MERGE

 MERGE = 'MERGE'

Details

value
MERGE

AUTH_BASIC

 AUTH_BASIC = 'basic'

Supported HTTP Authentication methods

Details

value
basic

HTTP_1

 HTTP_1 = '1.1'

HTTP protocol versions

Details

value
1.1

HTTP_0

 HTTP_0 = '1.0'

Details

value
1.0

CONTENT_TYPE

 CONTENT_TYPE = 'Content-Type'

Content attributes

Details

value
Content-Type

CONTENT_LENGTH

 CONTENT_LENGTH = 'Content-Length'

Details

value
Content-Length

ENC_URLENCODED

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

POST data encoding methods

Details

value
application/x-www-form-urlencoded

ENC_FORMDATA

 ENC_FORMDATA = 'multipart/form-data'

Details

value
multipart/form-data

Properties

$_fileInfoDb

resource $_fileInfoDb = 'null'

Fileinfo magic database resource

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

Details

$_fileInfoDb
resource
visibility
protected
default
null
final
false
static
true

$adapter

Zend_Http_Client_Adapter_Interface $adapter = 'null'

The adapter used to preform the actual connection to the server

Details

$adapter
Zend_Http_Client_Adapter_Interface
visibility
protected
default
null
final
false
static
false

$auth

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

$auth
array|null
visibility
protected
default
final
false
static
false

$config

array $config = 'array'

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

Details

$config
array
visibility
protected
default
array
final
false
static
false

$cookiejar

Zend_Http_CookieJar $cookiejar = 'null'

The client's cookie jar

Details

$cookiejar
Zend_Http_CookieJar
visibility
protected
default
null
final
false
static
false

$enctype

string $enctype = 'null'

Request body content type (for POST requests)

Details

$enctype
string
visibility
protected
default
null
final
false
static
false

$files

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

Details

$files
array
visibility
protected
default
array
final
false
static
false

$headers

array $headers = 'array'

Associative array of request headers

Details

$headers
array
visibility
protected
default
array
final
false
static
false

$last_request

string $last_request = 'null'

The last HTTP request sent by the client, as string

Details

$last_request
string
visibility
protected
default
null
final
false
static
false

$last_response

Zend_Http_Response $last_response = 'null'

The last HTTP response received by the client

Details

$last_response
Zend_Http_Response
visibility
protected
default
null
final
false
static
false

$method

string $method = 'self'

HTTP request method

Details

$method
string
visibility
protected
default
self
final
false
static
false

$paramsGet

array $paramsGet = 'array'

Associative array of GET parameters

Details

$paramsGet
array
visibility
protected
default
array
final
false
static
false

$paramsPost

array $paramsPost = 'array'

Assiciative array of POST parameters

Details

$paramsPost
array
visibility
protected
default
array
final
false
static
false

$raw_post_data

string $raw_post_data = 'null'

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

Details

$raw_post_data
string
visibility
protected
default
null
final
false
static
false

$redirectCounter

int $redirectCounter = '0'

Redirection counter

Details

$redirectCounter
int
visibility
protected
default
0
final
false
static
false

$uri

Zend_Uri_Http $uri = 'null'

Request URI

Details

$uri
Zend_Uri_Http
visibility
protected
default
null
final
false
static
false

Methods

__construct

__construct( Zend_Uri_Http|string $uri = null, array $config = null ) :

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

Arguments
$uri
Zend_Uri_Httpstring
$config
array
Configuration key-value pairs.
Details
visibility
public
final
false
static
false

_detectFileMimeType

_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

Arguments
$file
string
File path
Output
string
MIME type
Details
visibility
protected
final
false
static
false

_flattenParametersArray

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

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.

Arguments
$parray
array
$prefix
string
Output
array
Details
visibility
protected
final
false
static
true
since
1.9

_getParametersRecursive

_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

Arguments
$parray
array
The parameters array
$urlencode
bool
Whether to urlencode the name and value
Output
array
Details
visibility
protected
final
false
static
false
deprecated
since 1.9

_openTempStream

_openTempStream( ) : resource

Create temporary stream

Output
resource
Details
visibility
protected
final
false
static
false

_prepareBody

_prepareBody( ) : string

Prepare the request body (for POST and PUT requests)

Output
string
Details
visibility
protected
final
false
static
false
throws

_prepareHeaders

_prepareHeaders( ) : array

Prepare the request headers

Output
array
Details
visibility
protected
final
false
static
false

_setParameter

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

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

Arguments
$type
string
GET or POST
$name
string
$value
string
Output
null
Details
visibility
protected
final
false
static
false

encodeAuthHeader

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

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

Arguments
$user
string
$password
string
$type
string
Output
string
Details
visibility
public
final
false
static
true
see
http://www.faqs.org/rfcs/rfc2617.html
throws

encodeFormData

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

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

Arguments
$boundary
string
$name
string
$value
mixed
$filename
string
$headers
array
Associative array of optional headers @example ("Content-Transfer-Encoding" => "binary")
Output
string
Details
visibility
public
final
false
static
true

getAdapter

getAdapter( ) : Zend_Http_Client_Adapter_Interface

Load the connection adapter

Details
visibility
public
final
false
static
false

getCookieJar

getCookieJar( ) : Zend_Http_CookieJar|null

Return the current cookie jar or null if none.

Output
Zend_Http_CookieJar|null
Details
visibility
public
final
false
static
false

getHeader

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

Arguments
$key
string
Output
string|array|null
The header value or null if it is not set
Details
visibility
public
final
false
static
false

getLastRequest

getLastRequest( ) : string

Get the last HTTP request as string

Output
string
Details
visibility
public
final
false
static
false

getLastResponse

getLastResponse( ) : 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

Output
Zend_Http_Response
or null if none
Details
visibility
public
final
false
static
false

getRedirectionsCount

getRedirectionsCount( ) : int

Get the number of redirections done on the last request

Output
int
Details
visibility
public
final
false
static
false

getStream

getStream( ) : boolean|string

Get status of streaming for received data

Output
boolean|string
Details
visibility
public
final
false
static
false

getUri

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

Get the URI for the next request

Arguments
$as_string
boolean
If true, will return the URI as a string
Output
Zend_Uri_Http|string
Details
visibility
public
final
false
static
false

request

request( string $method = null ) : Zend_Http_Response

Send the HTTP request and return an HTTP response object

Arguments
$method
string
Details
visibility
public
final
false
static
false
throws

resetParameters

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

Arguments
$clearAll
bool
Should all data be cleared?
Details
visibility
public
final
false
static
false

setAdapter

setAdapter( 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

Arguments
$adapter
Zend_Http_Client_Adapter_Interfacestring
Output
null
Details
visibility
public
final
false
static
false
throws

setAuth

setAuth( string|false $user, string $password, string $type = self ) : 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);

Arguments
$user
stringfalse
User name or false disable authentication
$password
string
Password
$type
string
Authentication type
Details
visibility
public
final
false
static
false
see
http://www.faqs.org/rfcs/rfc2617.html
throws

setConfig

setConfig( Zend_Config $config = array ) : Zend_Http_Client

Set configuration parameters for this HTTP client

Arguments
$config
Zend_Config
| array $config
Details
visibility
public
final
false
static
false
throws

setCookie

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

Arguments
$cookie
Zend_Http_Cookiestring
$value
stringnull
If "cookie" is a string, this is the cookie value.
Details
visibility
public
final
false
static
false
throws

setCookieJar

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

Arguments
$cookiejar
Zend_Http_CookieJarboolean
Existing cookiejar object, true to create a new one, false to disable
Details
visibility
public
final
false
static
false
throws

setEncType

setEncType( string $enctype = self ) : Zend_Http_Client

Set the encoding type for POST data

Arguments
$enctype
string
Details
visibility
public
final
false
static
false

setFileUpload

setFileUpload( 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.
Arguments
$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)
Details
visibility
public
final
false
static
false
throws

setHeaders

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

Arguments
$name
stringarray
Header name, full header string ('Header: value') or an array of headers
$value
mixed
Header value or null
Details
visibility
public
final
false
static
false
throws

setMethod

setMethod( string $method = self ) : 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.

Arguments
$method
string
Details
visibility
public
final
false
static
false
throws

setParameterGet

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

Set a GET parameter for the request. Wrapper around _setParameter

Arguments
$name
stringarray
$value
string
Details
visibility
public
final
false
static
false

setParameterPost

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

Set a POST parameter for the request. Wrapper around _setParameter

Arguments
$name
stringarray
$value
string
Details
visibility
public
final
false
static
false

setRawData

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

Arguments
$data
stringresource
$enctype
string
Details
visibility
public
final
false
static
false

setStream

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

Set streaming for received data

Arguments
$streamfile
stringboolean
Stream file, true for temp file, false/null for no streaming
Details
visibility
public
final
false
static
false

setUri

setUri( Zend_Uri_Http|string $uri ) : Zend_Http_Client

Set the URI for the next request

Arguments
$uri
Zend_Uri_Httpstring
Details
visibility
public
final
false
static
false
throws
Documentation was generated by DocBlox.