XpressEngine Core  1.11.2
 All Classes Namespaces Files Functions Variables Pages
Public Member Functions | Public Attributes | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
HTTP_Request2 Class Reference
Inheritance diagram for HTTP_Request2:
HTTP_Request

Public Member Functions

 __construct ($url=null, $method=self::METHOD_GET, array $config=array())
 
 setUrl ($url)
 
 getUrl ()
 
 setMethod ($method)
 
 getMethod ()
 
 setConfig ($nameOrConfig, $value=null)
 
 getConfig ($name=null)
 
 setAuth ($user, $password= '', $scheme=self::AUTH_BASIC)
 
 getAuth ()
 
 setHeader ($name, $value=null, $replace=true)
 
 getHeaders ()
 
 addCookie ($name, $value)
 
 setBody ($body, $isFilename=false)
 
 getBody ()
 
 addUpload ($fieldName, $filename, $sendFilename=null, $contentType=null)
 
 addPostParameter ($name, $value=null)
 
 attach (SplObserver $observer)
 
 detach (SplObserver $observer)
 
 notify ()
 
 setLastEvent ($name, $data=null)
 
 getLastEvent ()
 
 setAdapter ($adapter)
 
 setCookieJar ($jar=true)
 
 getCookieJar ()
 
 send ()
 

Public Attributes

const METHOD_OPTIONS = 'OPTIONS'
 
const METHOD_GET = 'GET'
 
const METHOD_HEAD = 'HEAD'
 
const METHOD_POST = 'POST'
 
const METHOD_PUT = 'PUT'
 
const METHOD_DELETE = 'DELETE'
 
const METHOD_TRACE = 'TRACE'
 
const METHOD_CONNECT = 'CONNECT'
 
const AUTH_BASIC = 'basic'
 
const AUTH_DIGEST = 'digest'
 
const REGEXP_INVALID_TOKEN = '![\x00-\x1f\x7f-\xff()<>@,;:\\\\"/\[\]?={}\s]!'
 
const REGEXP_INVALID_COOKIE = '/[\s,;]/'
 

Protected Member Functions

 fopenWrapper ($file, $detectType=false)
 

Static Protected Member Functions

static detectMimeType ($filename)
 

Protected Attributes

 $observers = array()
 
 $url
 
 $method = self::METHOD_GET
 
 $auth
 
 $headers = array()
 
 $config
 
 $lastEvent
 
 $body = ''
 
 $postParams = array()
 
 $uploads = array()
 
 $adapter
 
 $cookieJar = null
 

Detailed Description

Definition at line 42 of file Request2.php.

Constructor & Destructor Documentation

HTTP_Request2::__construct (   $url = null,
  $method = self::METHOD_GET,
array  $config = array() 
)

Constructor. Can set request URL, method and configuration array.

Also sets a default value for User-Agent header.

Parameters
string | Net_Url2$urlRequest URL
string$methodRequest method
array$configConfiguration for this Request instance

Definition at line 205 of file Request2.php.

Member Function Documentation

HTTP_Request2::addCookie (   $name,
  $value 
)

Adds a cookie to the request

If the request does not have a CookieJar object set, this method simply appends a cookie to "Cookie:" header.

If a CookieJar object is available, the cookie is stored in that object. Data from request URL will be used for setting its 'domain' and 'path' parameters, 'expires' and 'secure' will be set to null and false, respectively. If you need further control, use CookieJar's methods.

Parameters
string$namecookie name
string$valuecookie value
Returns
HTTP_Request2
Exceptions
HTTP_Request2_LogicException
See Also
setCookieJar()

Definition at line 548 of file Request2.php.

HTTP_Request2::addPostParameter (   $name,
  $value = null 
)

Adds POST parameter(s) to the request.

Parameters
string | array$nameparameter name or array ('name' => 'value')
mixed$valueparameter value (can be an array)
Returns
HTTP_Request2

Definition at line 700 of file Request2.php.

HTTP_Request2::addUpload (   $fieldName,
  $filename,
  $sendFilename = null,
  $contentType = null 
)

Adds a file to form-based file upload

Used to emulate file upload via a HTML form. The method also sets Content-Type of HTTP request to 'multipart/form-data'.

If you just want to send the contents of a file as the body of HTTP request you should use setBody() method.

If you provide file pointers rather than file names, they should support fstat() and rewind() operations.

Parameters
string$fieldNamename of file-upload field
string | resource | array$filenamefull name of local file, pointer to open file or an array of files
string$sendFilenamefilename to send in the request
string$contentTypecontent-type of file being uploaded
Returns
HTTP_Request2
Exceptions
HTTP_Request2_LogicException

Definition at line 654 of file Request2.php.

HTTP_Request2::attach ( SplObserver  $observer)

Attaches a new observer

Parameters
SplObserver$observerany object implementing SplObserver

Definition at line 721 of file Request2.php.

HTTP_Request2::detach ( SplObserver  $observer)

Detaches an existing observer

Parameters
SplObserver$observerany object implementing SplObserver

Definition at line 736 of file Request2.php.

static HTTP_Request2::detectMimeType (   $filename)
staticprotected

Tries to detect MIME type of a file

The method will try to use fileinfo extension if it is available, deprecated mime_content_type() function in the other case. If neither works, default 'application/octet-stream' MIME type is returned

Parameters
string$filenamefile name
Returns
string file MIME type

Definition at line 1012 of file Request2.php.

HTTP_Request2::fopenWrapper (   $file,
  $detectType = false 
)
protected

Wrapper around fopen()/fstat() used by setBody() and addUpload()

Parameters
string | resource$filefile name or pointer to open file
bool$detectTypewhether to try autodetecting MIME type of file, will only work if $file is a filename, not pointer
Returns
array array('fp' => file pointer, 'size' => file size, 'type' => MIME type)
Exceptions
HTTP_Request2_LogicException

Definition at line 967 of file Request2.php.

HTTP_Request2::getAuth ( )

Returns the authentication data

The array has the keys 'user', 'password' and 'scheme', where 'scheme' is one of the HTTP_Request2::AUTH_* constants.

Returns
array

Definition at line 441 of file Request2.php.

HTTP_Request2::getBody ( )

Returns the request body

Returns
string|resource|HTTP_Request2_MultipartBody

Definition at line 610 of file Request2.php.

HTTP_Request2::getConfig (   $name = null)

Returns the value(s) of the configuration parameter(s)

Parameters
string$nameparameter name
Returns
mixed value of $name parameter, array of all configuration parameters if $name is not given
Exceptions
HTTP_Request2_LogicExceptionIf the parameter is unknown

Definition at line 396 of file Request2.php.

HTTP_Request2::getCookieJar ( )

Returns current CookieJar object or null if none

Returns
HTTP_Request2_CookieJar|null

Definition at line 897 of file Request2.php.

HTTP_Request2::getHeaders ( )

Returns the request headers

The array is of the form ('header name' => 'header value'), header names are lowercased

Returns
array

Definition at line 525 of file Request2.php.

HTTP_Request2::getLastEvent ( )

Returns the last event

Observers should use this method to access the last change in request. The following event names are possible:

  • 'connect' - after connection to remote server, data is the destination (string)
  • 'disconnect' - after disconnection from server
  • 'sentHeaders' - after sending the request headers, data is the headers sent (string)
  • 'sentBodyPart' - after sending a part of the request body, data is the length of that part (int)
  • 'sentBody' - after sending the whole request body, data is request body length (int)
  • 'receivedHeaders' - after receiving the response headers, data is HTTP_Request2_Response object
  • 'receivedBodyPart' - after receiving a part of the response body, data is that part (string)
  • 'receivedEncodedBodyPart' - as 'receivedBodyPart', but data is still encoded by Content-Encoding
  • 'receivedBody' - after receiving the complete response body, data is HTTP_Request2_Response object

Different adapters may not send all the event types. Mock adapter does not send any events to the observers.

Returns
array The array has two keys: 'name' and 'data'

Definition at line 803 of file Request2.php.

HTTP_Request2::getMethod ( )

Returns the request method

Returns
string

Definition at line 298 of file Request2.php.

HTTP_Request2::getUrl ( )

Returns the request URL

Returns
Net_URL2

Definition at line 266 of file Request2.php.

HTTP_Request2::notify ( )

Notifies all observers

Definition at line 749 of file Request2.php.

HTTP_Request2::send ( )

Sends the request and returns the response

Exceptions
HTTP_Request2_Exception
Returns
HTTP_Request2_Response

Definition at line 908 of file Request2.php.

HTTP_Request2::setAdapter (   $adapter)

Sets the adapter used to actually perform the request

You can pass either an instance of a class implementing HTTP_Request2_Adapter or a class name. The method will only try to include a file if the class name starts with HTTP_Request2_Adapter_, it will also try to prepend this prefix to the class name if it doesn't contain any underscores, so that $request->setAdapter('curl'); will work.

Parameters
string | HTTP_Request2_Adapter$adapterAdapter to use
Returns
HTTP_Request2
Exceptions
HTTP_Request2_LogicException

Definition at line 825 of file Request2.php.

HTTP_Request2::setAuth (   $user,
  $password = '',
  $scheme = self::AUTH_BASIC 
)

Sets the autentification data

Parameters
string$useruser name
string$passwordpassword
string$schemeauthentication scheme
Returns
HTTP_Request2

Definition at line 418 of file Request2.php.

HTTP_Request2::setBody (   $body,
  $isFilename = false 
)

Sets the request body

If you provide file pointer rather than file name, it should support fstat() and rewind() operations.

Parameters
string | resource | HTTP_Request2_MultipartBody$bodyEither a string with the body or filename containing body or pointer to an open file or object with multipart body data
bool$isFilenameWhether first parameter is a filename
Returns
HTTP_Request2
Exceptions
HTTP_Request2_LogicException

Definition at line 585 of file Request2.php.

HTTP_Request2::setConfig (   $nameOrConfig,
  $value = null 
)

Sets the configuration parameter(s)

The following parameters are available:

  • 'adapter' - adapter to use (string)
  • 'connect_timeout' - Connection timeout in seconds (integer)
  • 'timeout' - Total number of seconds a request can take. Use 0 for no limit, should be greater than 'connect_timeout' if set (integer)
  • 'use_brackets' - Whether to append [] to array variable names (bool)
  • 'protocol_version' - HTTP Version to use, '1.0' or '1.1' (string)
  • 'buffer_size' - Buffer size to use for reading and writing (int)
  • 'store_body' - Whether to store response body in response object. Set to false if receiving a huge response and using an Observer to save it (boolean)
  • 'local_ip' - Specifies the IP address that will be used for accessing the network (string)
  • 'proxy_type' - Proxy type, 'http' or 'socks5' (string)
  • 'proxy_host' - Proxy server host (string)
  • 'proxy_port' - Proxy server port (integer)
  • 'proxy_user' - Proxy auth username (string)
  • 'proxy_password' - Proxy auth password (string)
  • 'proxy_auth_scheme' - Proxy auth scheme, one of HTTP_Request2::AUTH_* constants (string)
  • 'proxy' - Shorthand for proxy_* parameters, proxy given as URL, e.g. 'socks5://localhost:1080/' (string)
  • 'ssl_verify_peer' - Whether to verify peer's SSL certificate (bool)
  • 'ssl_verify_host' - Whether to check that Common Name in SSL certificate matches host name (bool)
  • 'ssl_cafile' - Cerificate Authority file to verify the peer with (use with 'ssl_verify_peer') (string)
  • 'ssl_capath' - Directory holding multiple Certificate Authority files (string)
  • 'ssl_local_cert' - Name of a file containing local cerificate (string)
  • 'ssl_passphrase' - Passphrase with which local certificate was encoded (string)
  • 'digest_compat_ie' - Whether to imitate behaviour of MSIE 5 and 6 in using URL without query string in digest authentication (boolean)
  • 'follow_redirects' - Whether to automatically follow HTTP Redirects (boolean)
  • 'max_redirects' - Maximum number of redirects to follow (integer)
  • 'strict_redirects' - Whether to keep request method on redirects via status 301 and 302 (true, needed for compatibility with RFC 2616) or switch to GET (false, needed for compatibility with most browsers) (boolean)
Parameters
string | array$nameOrConfigconfiguration parameter name or array ('parameter name' => 'parameter value')
mixed$valueparameter value if $nameOrConfig is not an array
Returns
HTTP_Request2
Exceptions
HTTP_Request2_LogicExceptionIf the parameter is unknown

Definition at line 357 of file Request2.php.

HTTP_Request2::setCookieJar (   $jar = true)

Sets the cookie jar

A cookie jar is used to maintain cookies across HTTP requests and responses. Cookies from jar will be automatically added to the request headers based on request URL.

Parameters
HTTP_Request2_CookieJar | bool$jarExisting CookieJar object, true to create a new one, false to remove
Returns
HTTP_Request2
Exceptions
HTTP_Request2_LogicException

Definition at line 870 of file Request2.php.

HTTP_Request2::setHeader (   $name,
  $value = null,
  $replace = true 
)

Sets request header(s)

The first parameter may be either a full header string 'header: value' or header name. In the former case $value parameter is ignored, in the latter the header's value will either be set to $value or the header will be removed if $value is null. The first parameter can also be an array of headers, in that case method will be called recursively.

Note that headers are treated case insensitively as per RFC 2616.

$req->setHeader('Foo: Bar'); // sets the value of 'Foo' header to 'Bar' $req->setHeader('FoO', 'Baz'); // sets the value of 'Foo' header to 'Baz' $req->setHeader(array('foo' => 'Quux')); // sets the value of 'Foo' header to 'Quux' $req->setHeader('FOO'); // removes 'Foo' header from request

Parameters
string | array$nameheader name, header string ('Header: value') or an array of headers
string | array | null$valueheader value if $name is not an array, header will be removed if value is null
bool$replacewhether to replace previous header with the same name or append to its value
Returns
HTTP_Request2
Exceptions
HTTP_Request2_LogicException

Definition at line 474 of file Request2.php.

HTTP_Request2::setLastEvent (   $name,
  $data = null 
)

Sets the last event

Adapters should use this method to set the current state of the request and notify the observers.

Parameters
string$nameevent name
mixed$dataevent data

Definition at line 765 of file Request2.php.

HTTP_Request2::setMethod (   $method)

Sets the request method

Parameters
string$methodone of the methods defined in RFC 2616
Returns
HTTP_Request2
Exceptions
HTTP_Request2_LogicExceptionif the method name is invalid

Definition at line 279 of file Request2.php.

HTTP_Request2::setUrl (   $url)

Sets the URL for this request

If the URL has userinfo part (username & password) these will be removed and converted to auth data. If the URL does not have a path component, that will be set to '/'.

Parameters
string | Net_URL2$urlRequest URL
Returns
HTTP_Request2
Exceptions
HTTP_Request2_LogicException

Definition at line 233 of file Request2.php.

Member Data Documentation

HTTP_Request2::$adapter
protected

Definition at line 188 of file Request2.php.

HTTP_Request2::$auth
protected

Definition at line 111 of file Request2.php.

HTTP_Request2::$body = ''
protected

Definition at line 170 of file Request2.php.

HTTP_Request2::$config
protected
Initial value:
= array(
'adapter' => 'HTTP_Request2_Adapter_Socket',
'connect_timeout' => 10,
'timeout' => 0,
'use_brackets' => true,
'protocol_version' => '1.1',
'buffer_size' => 16384,
'store_body' => true,
'local_ip' => null,
'proxy_host' => '',
'proxy_port' => '',
'proxy_user' => '',
'proxy_password' => '',
'proxy_auth_scheme' => self::AUTH_BASIC,
'proxy_type' => 'http',
'ssl_verify_peer' => true,
'ssl_verify_host' => true,
'ssl_cafile' => null,
'ssl_capath' => null,
'ssl_local_cert' => null,
'ssl_passphrase' => null,
'digest_compat_ie' => false,
'follow_redirects' => false,
'max_redirects' => 5,
'strict_redirects' => false
)

Definition at line 124 of file Request2.php.

HTTP_Request2::$cookieJar = null
protected

Definition at line 194 of file Request2.php.

HTTP_Request2::$headers = array()
protected

Definition at line 117 of file Request2.php.

HTTP_Request2::$lastEvent
protected
Initial value:
= array(
'name' => 'start',
'data' => null
)

Definition at line 160 of file Request2.php.

HTTP_Request2::$method = self::METHOD_GET
protected

Definition at line 104 of file Request2.php.

HTTP_Request2::$observers = array()
protected

Definition at line 92 of file Request2.php.

HTTP_Request2::$postParams = array()
protected

Definition at line 176 of file Request2.php.

HTTP_Request2::$uploads = array()
protected

Definition at line 182 of file Request2.php.

HTTP_Request2::$url
protected

Definition at line 98 of file Request2.php.

const HTTP_Request2::AUTH_BASIC = 'basic'

#@- #@+ Constants for HTTP authentication schemes

http://tools.ietf.org/html/rfc2617

Definition at line 64 of file Request2.php.

const HTTP_Request2::AUTH_DIGEST = 'digest'

Definition at line 65 of file Request2.php.

const HTTP_Request2::METHOD_CONNECT = 'CONNECT'

Definition at line 56 of file Request2.php.

const HTTP_Request2::METHOD_DELETE = 'DELETE'

Definition at line 54 of file Request2.php.

const HTTP_Request2::METHOD_GET = 'GET'

Definition at line 50 of file Request2.php.

const HTTP_Request2::METHOD_HEAD = 'HEAD'

Definition at line 51 of file Request2.php.

const HTTP_Request2::METHOD_OPTIONS = 'OPTIONS'

#@+ Constants for HTTP request methods

http://tools.ietf.org/html/rfc2616#section-5.1.1

Definition at line 49 of file Request2.php.

const HTTP_Request2::METHOD_POST = 'POST'

Definition at line 52 of file Request2.php.

const HTTP_Request2::METHOD_PUT = 'PUT'

Definition at line 53 of file Request2.php.

const HTTP_Request2::METHOD_TRACE = 'TRACE'

Definition at line 55 of file Request2.php.

const HTTP_Request2::REGEXP_INVALID_COOKIE = '/[\s,;]/'

Regular expression used to check for invalid symbols in cookie strings http://web.archive.org/web/20080331104521/http://cgi.netscape.com/newsref/std/cookie_spec.html

Definition at line 79 of file Request2.php.

const HTTP_Request2::REGEXP_INVALID_TOKEN = '![\x00-\x1f\x7f-\xff()<>@,;:\\\\"/\[\]?={}\s]!'

#@- Regular expression used to check for invalid symbols in RFC 2616 tokens http://pear.php.net/bugs/bug.php?id=15630

Definition at line 72 of file Request2.php.


The documentation for this class was generated from the following file: