Microsoft_Http
[ class tree: Microsoft_Http ] [ index: Microsoft_Http ] [ all elements ]

Class: Microsoft_Http_Client

Source Location: /Http/Client.php

Class Overview


Microsoft_Http_Client is an implemetation of an HTTP client in PHP. The client


Author(s):

Copyright:

  • Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)

Variables

Constants

Methods



Class Details

[line 44]
Microsoft_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 Microsoft_Http_CookieJar object)




Tags:

copyright:  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
todo:  Implement proxy settings
throws:  Microsoft_Http_Client_Exception
license:  New BSD License


[ Top ]


Class Variables

static $_fileInfoDb =  null

[line 217]

Fileinfo magic database resource

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




Tags:

access:  protected

Type:   resource


[ Top ]

$adapter =  null

[line 106]

The adapter used to preform the actual connection to the server



Tags:

access:  protected



[ Top ]

$auth =

[line 169]

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.




Tags:

access:  protected

Type:   array|null


[ Top ]

$config = array(
        'maxredirects'    => 5,
        'strictredirects' => false,
        'useragent'       => 'Microsoft_Http_Client',
        'timeout'         => 10,
        'adapter'         => 'Microsoft_Http_Client_Adapter_Socket',
        'httpversion'     => self::HTTP_1,
        'keepalive'       => false,
        'storeresponse'   => true,
        'strict'          => true,
        'output_stream'     => false,
    )

[line 88]

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



Tags:

access:  protected

Type:   array


[ Top ]

$cookiejar =  null

[line 186]

The client's cookie jar



Tags:

access:  protected



[ Top ]

$enctype =  null

[line 148]

Request body content type (for POST requests)



Tags:

access:  protected

Type:   string


[ Top ]

$files = array()

[line 179]

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




Tags:

access:  protected

Type:   array


[ Top ]

$headers = array()

[line 120]

Associative array of request headers



Tags:

access:  protected

Type:   array


[ Top ]

$last_request =  null

[line 193]

The last HTTP request sent by the client, as string



Tags:

access:  protected

Type:   string


[ Top ]

$last_response =  null

[line 200]

The last HTTP response received by the client



Tags:

access:  protected



[ Top ]

$method =  self::GET

[line 127]

HTTP request method



Tags:

access:  protected

Type:   string


[ Top ]

$paramsGet = array()

[line 134]

Associative array of GET parameters



Tags:

access:  protected

Type:   array


[ Top ]

$paramsPost = array()

[line 141]

Assiciative array of POST parameters



Tags:

access:  protected

Type:   array


[ Top ]

$raw_post_data =  null

[line 155]

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



Tags:

access:  protected

Type:   string


[ Top ]

$redirectCounter =  0

[line 207]

Redirection counter



Tags:

access:  protected

Type:   int


[ Top ]

$uri =  null

[line 113]

Request URI



Tags:

access:  protected

Type:   Microsoft_Uri_Http


[ Top ]



Class Methods


static method encodeAuthHeader [line 1350]

static string encodeAuthHeader( string $user, string $password, [string $type = self::AUTH_BASIC])

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



Tags:

see:  http://www.faqs.org/rfcs/rfc2617.html
throws:  Microsoft_Http_Client_Exception
access:  public


Parameters:

string   $user  
string   $password  
string   $type  

[ Top ]

static method encodeFormData [line 1320]

static string encodeFormData( string $boundary, string $name, mixed $value, [string $filename = null], [array $headers = array()])

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



Tags:

access:  public


Parameters:

string   $boundary  
string   $name  
mixed   $value  
string   $filename  
array   $headers   Associative array of optional headers @example ("Content-Transfer-Encoding" => "binary")

[ Top ]

static method _flattenParametersArray [line 1395]

static array _flattenParametersArray( array $parray, [string $prefix = null])

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.




Tags:

since:  1.9
access:  protected


Parameters:

array   $parray  
string   $prefix  

[ Top ]

constructor __construct [line 226]

Microsoft_Http_Client __construct( [Microsoft_Uri_Http|string $uri = null], [array $config = null])

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



Tags:

access:  public


Parameters:

Microsoft_Uri_Http|string   $uri  
array   $config   Configuration key-value pairs.

[ Top ]

method getAdapter [line 841]

Microsoft_Http_Client_Adapter_Interface getAdapter( )

Load the connection adapter



Tags:

access:  public


[ Top ]

method getCookieJar [line 589]

Microsoft_Http_CookieJar|null getCookieJar( )

Return the current cookie jar or null if none.



Tags:

access:  public


[ Top ]

method getHeader [line 414]

string|array|null getHeader( string $key)

Get the value of a specific header

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




Tags:

return:  The header value or null if it is not set
access:  public


Parameters:

string   $key  

[ Top ]

method getLastRequest [line 786]

string getLastRequest( )

Get the last HTTP request as string



Tags:

access:  public


[ Top ]

method getLastResponse [line 799]

Microsoft_Http_Response getLastResponse( )

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




Tags:

return:  or null if none
access:  public


[ Top ]

method getRedirectionsCount [line 495]

int getRedirectionsCount( )

Get the number of redirections done on the last request



Tags:

access:  public


[ Top ]

method getStream [line 862]

boolean|string getStream( )

Get status of streaming for received data



Tags:

access:  public


[ Top ]

method getUri [line 276]

Microsoft_Uri_Http|string getUri( [boolean $as_string = false])

Get the URI for the next request



Tags:

access:  public


Parameters:

boolean   $as_string   If true, will return the URI as a string

[ Top ]

method request [line 898]

Microsoft_Http_Response request( [string $method = null])

Send the HTTP request and return an HTTP response object



Tags:

throws:  Microsoft_Http_Client_Exception
access:  public


Parameters:

string   $method  

[ Top ]

method resetParameters [line 756]

Microsoft_Http_Client resetParameters( [bool $clearAll = false])

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




Tags:

access:  public


Parameters:

bool   $clearAll   Should all data be cleared?

[ Top ]

method setAdapter [line 814]

null setAdapter( Microsoft_Http_Client_Adapter_Interface|string $adapter)

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




Tags:

throws:  Microsoft_Http_Client_Exception
access:  public


Parameters:

Microsoft_Http_Client_Adapter_Interface|string   $adapter  

[ Top ]

method setAuth [line 523]

Microsoft_Http_Client setAuth( string|false $user, [string $password = ''], [string $type = self::AUTH_BASIC])

Set HTTP authentication parameters

$type should be one of the supported types - see the self::AUTH_* constants.

To enable authentication:

  1.  $this->setAuth('shahar''secret'Microsoft_Http_Client::AUTH_BASIC);

To disable authentication:

  1.  $this->setAuth(false);




Tags:

see:  http://www.faqs.org/rfcs/rfc2617.html
throws:  Microsoft_Http_Client_Exception
access:  public


Parameters:

string|false   $user   User name or false disable authentication
string   $password   Password
string   $type   Authentication type

[ Top ]

method setConfig [line 292]

Microsoft_Http_Client setConfig( [Microsoft_Config $config = array()])

Set configuration parameters for this HTTP client



Tags:

throws:  Microsoft_Http_Client_Exception
access:  public


Parameters:

Microsoft_Config   $config   | array $config

[ Top ]

method setCookie [line 603]

Microsoft_Http_Client setCookie( Microsoft_Http_Cookie|string $cookie, [string|null $value = null])

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.



Tags:

throws:  Microsoft_Http_Client_Exception
access:  public


Parameters:

Microsoft_Http_Cookie|string   $cookie  
string|null   $value   If "cookie" is a string, this is the cookie value.

[ Top ]

method setCookieJar [line 563]

Microsoft_Http_Client setCookieJar( [Microsoft_Http_CookieJar|boolean $cookiejar = true])

Set the HTTP client's cookie jar.

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




Tags:

throws:  Microsoft_Http_Client_Exception
access:  public


Parameters:

Microsoft_Http_CookieJar|boolean   $cookiejar   Existing cookiejar object, true to create a new one, false to disable

[ Top ]

method setEncType [line 709]

Microsoft_Http_Client setEncType( [string $enctype = self::ENC_URLENCODED])

Set the encoding type for POST data



Tags:

access:  public


Parameters:

string   $enctype  

[ Top ]

method setFileUpload [line 676]

Microsoft_Http_Client setFileUpload( string $filename, string $formname, [string $data = null], [string $ctype = null])

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.




Tags:

throws:  Microsoft_Http_Client_Exception
access:  public


Parameters:

string   $filename   Name of file to upload, or name to save as
string   $formname   Name of form element to send as
string   $data   Data to send (if null, $filename is read and sent)
string   $ctype   Content type to use (if $data is set and $ctype is null, will be application/octet-stream)

[ Top ]

method setHeaders [line 362]

Microsoft_Http_Client setHeaders( string|array $name, [mixed $value = null])

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.




Tags:

throws:  Microsoft_Http_Client_Exception
access:  public


Parameters:

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

[ Top ]

method setMethod [line 326]

Microsoft_Http_Client setMethod( [string $method = self::GET])

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.




Tags:

throws:  Microsoft_Http_Client_Exception
access:  public


Parameters:

string   $method  

[ Top ]

method setParameterGet [line 431]

Microsoft_Http_Client setParameterGet( string|array $name, [string $value = null])

Set a GET parameter for the request. Wrapper around _setParameter



Tags:

access:  public


Parameters:

string|array   $name  
string   $value  

[ Top ]

method setParameterPost [line 450]

Microsoft_Http_Client setParameterPost( string|array $name, [string $value = null])

Set a POST parameter for the request. Wrapper around _setParameter



Tags:

access:  public


Parameters:

string|array   $name  
string   $value  

[ Top ]

method setRawData [line 730]

Microsoft_Http_Client setRawData( string|resource $data, [string $enctype = null])

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.




Tags:

access:  public


Parameters:

string|resource   $data  
string   $enctype  

[ Top ]

method setStream [line 852]

Microsoft_Http_Client setStream( [string|boolean $streamfile = true])

Set streaming for received data



Tags:

access:  public


Parameters:

string|boolean   $streamfile   Stream file, true for temp file, false/null for no streaming

[ Top ]

method setUri [line 243]

Microsoft_Http_Client setUri( Microsoft_Uri_Http|string $uri)

Set the URI for the next request



Tags:

throws:  Microsoft_Http_Client_Exception
access:  public


Parameters:

Microsoft_Uri_Http|string   $uri  

[ Top ]

method _detectFileMimeType [line 1284]

string _detectFileMimeType( string $file)

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




Tags:

return:  MIME type
access:  protected


Parameters:

string   $file   File path

[ Top ]

method _getParametersRecursive [line 1234]

array _getParametersRecursive( array $parray, [bool $urlencode = false])

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




Tags:

deprecated:  since 1.9
access:  protected


Parameters:

array   $parray   The parameters array
bool   $urlencode   Whether to urlencode the name and value

[ Top ]

method _openTempStream [line 872]

resource _openTempStream( )

Create temporary stream



Tags:

access:  protected


[ Top ]

method _prepareBody [line 1127]

string _prepareBody( )

Prepare the request body (for POST and PUT requests)



Tags:

throws:  Microsoft_Http_Client_Exception
access:  protected


[ Top ]

method _prepareHeaders [line 1046]

array _prepareHeaders( )

Prepare the request headers



Tags:

access:  protected


[ Top ]

method _setParameter [line 470]

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

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



Tags:

access:  protected


Parameters:

string   $type   GET or POST
string   $name  
string   $value  

[ Top ]


Class Constants

AUTH_BASIC =  'basic'

[line 62]

Supported HTTP Authentication methods


[ Top ]

CONNECT =  'CONNECT'

[line 56]


[ Top ]

CONTENT_LENGTH =  'Content-Length'

[line 75]


[ Top ]

CONTENT_TYPE =  'Content-Type'

[line 74]

Content attributes


[ Top ]

DELETE =  'DELETE'

[line 53]


[ Top ]

ENC_FORMDATA =  'multipart/form-data'

[line 81]


[ Top ]

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

[line 80]

POST data encoding methods


[ Top ]

GET =  'GET'

[line 49]

HTTP request methods


[ Top ]

HEAD =  'HEAD'

[line 52]


[ Top ]

HTTP_0 =  '1.0'

[line 69]


[ Top ]

HTTP_1 =  '1.1'

[line 68]

HTTP protocol versions


[ Top ]

MERGE =  'MERGE'

[line 57]


[ Top ]

OPTIONS =  'OPTIONS'

[line 55]


[ Top ]

POST =  'POST'

[line 50]


[ Top ]

PUT =  'PUT'

[line 51]


[ Top ]

TRACE =  'TRACE'

[line 54]


[ Top ]



Documentation generated on Wed, 18 May 2011 12:06:22 +0200 by phpDocumentor 1.4.3